perl scrip feeded by filepro

Fairlight fairlite at fairlite.com
Fri Jan 12 18:00:25 PST 2007


Simon--er, no...it was Enrique Arredondo--said:
> For the perl gurus out there, Is there a way to run a perl script using 
> arguments like a normal shell does ?

Yes.

> I have a perl script that I need to run with some arguments coming from 
> filepro running on a system noredraw command in order to get data back.
> 
> If it was ksh , I'll do something like this:
> 
> system noredraw "script argument1 argument2 > /tmp/file"
> 
> and then I'll import the file and get the information.
> 
> But it seems that a perl script won't work with $1 $2 ,etc. Or is there a 
> way ?

$1 and $2 won't work.  Perl isn't meant to be Bourne-compliant [in fact,
inasmuch as it borrows from any shell, it tends to do so from csh, not
sh--witness the presence of foreach()].  $0 is [scriptname], but that's the
-only- pneumonic that works in that fashion for anything on the command
line.

Arguments for perl programs (I really dislike "scripts" as it seems to
belittle the programming for not being compiled, when the coding can be
almost as complex as C code sometimes...personal gripe)...anyway, arguments
for the programs are stored in the array @ARGV.

You can access the various elements a few ways:

# Take them off the front of the stack in FIFO fashion.
# NOTE:  This effectively "eats" @ARGV's contents, and it also means
# you must make the assignments in the order expected the first time
# because you get no second chances.
my $first_arg = shift;
my $second_arg = shift;

# OR...

# Take them as subscripts of @ARGV explicitly.
my $first_arg = $ARGV[0];
my $seconnd_arg = $ARGV[1];

# OR...

# Take them as subscripts of @_ and depend on the default lexical scope.
my $first_arg = $_[0];
my $seconnd_arg = $_[1];

# OR...

You could use real option handling.
`perldoc Getopt::Long`

Read the docs.  Anything not explicitly defined as usable by your Getopt
definition that is -not- a dash or double-dash option (ie., -a,
--append, etc.) will be left in @ARGV and you can access those as above.
However, you would be able to assign things into specific variables
from the start this way:

my ($first_arg,$second_arg);
use Getopt::Long;
Getopt::Long::Configure('default');
Getopt::Long::Configure('bundling');
my $cli_opt_return = Getopt::Long::GetOptions(
     "f|first=s" => \$first_arg,
     "s|second=s" => \$second_arg
);

# In this case, you could use:  program -f arg1 -s arg2
# or alternately:  program --first arg1 --second arg2

I personally prefer dealing with Getopt::Long, as I really, really,
-really- dislike depending on order of arguments unless there is only a
single argument, and Getopt eliminates that dependance altogether.  I
personally think depending on order of arguments when there are >1
arguments is poor design.  Always have.

Now we get to your specific example...

> $weight = "";  ## THIS IS WHERE I WANT THE ARGUMENT TO SHOW UP <<<<<<<<<
>                 ## CAN I USE  $weight = $1 ?

My advice for something this light would be:

$weight = $ARGV[0];

I prefer explicit over implicit (ie., prefer subscripts over shifting).

Please note that $ARGV[0] and $_[0] are -not- equal to $0.  $0 is a special
case that is analogous to argv[0] in C, but in perl it would really be
analogous to $ARGV[-1] if that was actually a valid subscript (it's not).
The program name is -not- stored in the @ARGV array, it's stored in $0.

HTH.

mark->


More information about the Filepro-list mailing list