Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

how do i pass a parameter to a perl script from the command line?

I want a script to write to a file, but the file needs to
be named in the command line, similar to :
perl myperlprogram.pl "writefile.doc".
so that in my program i would have a line such as
my $FILE = $[];

Also, are there any differences between Windows and Unix
when it comes to passing parameters?
Is this possible? I would be grateful for any help on this matter.

A practicing Monk

p.s. Do perl monks have to stay celibate? Something i wonder about...

Replies are listed 'Best First'.
Re: Passing parameters to scripts
by moxliukas (Curate) on Aug 12, 2002 at 11:16 UTC

    All command line parameters are stored in @ARGV array. So you can access it directly like:

    my $file = $ARGV[0];

    or even like this (modifying the array):

    my $file = shift @ARGV;

    However if you need only to pass a file name as a parameter for opening it at a later stage, you can say

    while(<>) { }

    straight away and this will read from the file on the command line.

    If you want to pass complex things on the command line (like options, etc), have a look at a module Getopt::Long

    And no, I don't think there is any difference in Unix and Windows in this respect.

    Just my 2 Lithuanian cents.

      There are differences in how the various shells interpret certain symbols, so what you may need to use for one shell might not work in another. For instance in *nix arguments are normally globbed by the OS, wheras in Win32 they are normally globbed by the program. Other difference would be quoting characters and escape rules.

      Yves / DeMerphq
      ---
      Software Engineering is Programming when you can't. -- E. W. Dijkstra (RIP)