views:

44

answers:

2

I have googled a way to do this kind of thing.

I use this shell command:

perl -I/p2 maxconnect4.p1 arg1 arg2 arg3 arg4

My pl files and pm files are all in "p2" directory.

However, it gives me an error:

Can't open perl script "maxconnect4.pl": No such file or directory

Can anyone explain why?

+3  A: 

perl needs a real file to be executed. You have to call it through its absolute or relative path:

perl -I p2 p2/maxconnect4.pl arg1 arg2 arg3 arg4
eumiro
Thanks very much. I have tried your way. It gives me a new error:
FihopZz
The error : Can't locate AiPlayer.pm in @INC
FihopZz
If I go the directory p2, and use command: perl maxconnect4.pl arg1 arg2 arg3 arg4. It works well. I guess this is the problem of the search path. I'm totally new to Perl. Really appreciate your help.
FihopZz
Where is the file AiPlayer.pm physically on your harddrive?
eumiro
They are on the p2 directory
FihopZz
See my edited answer: remove the slash '/' from "-I p2"
eumiro
+5  A: 

You have two problems:

  1. -I doesn't apply to files specified on the command line, only to modules loaded with require or use.

  2. You shouldn't use relative directories with -I. If your script does a chdir, then that directory is now relative to the new current directory, not the one that was current when the script started. (In many cases, you can get away with breaking this rule, because most of the time, modules are loaded as the script starts, before it has changed directories.)

    And you aren't actually using a relative directory anyway, because you said -I/p2, which means to look in a directory named /p2, not in a subdirectory named p2 in the current directory (which is what you wanted to say).

Try:

perl -I$PWD/p2 p2/maxconnect4.pl arg1 arg2 arg3 arg4

(assuming you're using a Unix-type shell).

In many cases, a better solution is to have the script set up @INC automatically. At the top of p2/maxconnect4.pl (before you load any modules from p2, put):

use FindBin;
use lib $FindBin::Bin;

use lib is the programmatic version of the -I switch, and FindBin tells your script the pathname of the directory in which it is located. Then you can just say:

perl p2/maxconnect4.pl arg1 arg2 arg3 arg4

and not have to bother with -I at all.

cjm
Yes, I should absolute directory. Thanks very much. It works.
FihopZz