Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

how to access the commandline arguments?

by Anonymous Monk
on May 01, 2004 at 03:02 UTC ( #349562=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone show me how to use arguements for the commandline? Ie: perl 25 . How do you accept arguments and store them as variables from the same line you run from?

I've used chomp(my $var = <STDIN>) a few times but that doesn't take it directly from that line and I was wondering how people do that. And is there a way to know how many arguements were supplied? Like if you needed exactly 1, or maybe exactly 2 arguements?


20040501 Edit by jdporter: Changed title from 'arguements'

  • Comment on how to access the commandline arguments?

Replies are listed 'Best First'.
Re: how to access the commandline arguments?
by dragonchild (Archbishop) on May 01, 2004 at 03:12 UTC
    If you read Learning Perl (by merlyn), you would find that the @ARGV array contains the arguments from the commandline. Additionally, Getopt::Long is part of the Perl core. I would strongly recommend learning it, if you plan on doing shell programs.

    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: how to access the commandline arguments?
by davido (Archbishop) on May 01, 2004 at 04:03 UTC
    As the saying goes, "It depends."

    @ARGV contains a list of command line arguments that the user included when invoking your script.

    Getopt::Long is the core module most commonly (and wisely) used for handling command line arguments.

    The <> IO operator (see perlop), known as the "null filehandle" allows your script to take input either from STDIN, or from the list of files the user enumerated on the command line while invoking your script. Internally it gets this information from @ARGV.

    And then there's always command line indirection.

    For example, if you have a script designed to read from STDIN, you could instead invoke it with command line indirection like this: <filename.txt

    That method also works for output. If you want your script to print to a file instead of to the screen, you can type: >outfile.txt

    Those last two examples are operating system dependant, so you'll need to check your OS's documentation to gain info on specifics.

    Hope this helps...


Re: how to access the commandline arguments?
by crabbdean (Pilgrim) on May 01, 2004 at 09:42 UTC
    Arguments passed to the script are stored in the array @ARGV. To know how many arguments are in @ARGV there are two ways:
    my $count = @ARGV; print $count;
    OR you can use the special variable $#ARGV which contains the index number of the last array element. That is, if you have 4 elements in your array the last index number is 3.

    To turn @ARGV this into variables try something like the below at the beginning of your script:
    if (@ARGV < 3) { die "use: script var1 var2 var3\n"; } else { my ($var1, $var2, $var3) = @ARGV; } ## then use $var1, $var2 and $var3 in your program
    This has the effect of ensuring your program is passed enough variables.

    Alternatively you could do something like the below if you want to set defaults to variables if they aren't passed to the script:
    my ($stream, $path, $rturn, $depth, $display) = @ARGV; $stream = \*STDOUT unless @_ > 0; $path = cwd unless @_ > 1; $rturn = 'all' unless @_ > 2; $depth = -1 unless @_ > 3; $display = 'rel_file' unless @_ > 4;
    This means if someone calls your program without arguments then a default value is used.

    Its also common to use "shift" to grab the next element in @ARGV.
    my $nextvar = shift @ARGV;
    Or you can loop through @ARGV like any other array.
    for my $this (@ARGV) { print "$this\n"; }
    In fact you can treat @ARGV pretty much like any array and "push", "shift", "unshift" or any other array function as you please.

    Someone also mentioned using <> the "null filehandle". Usually you can use <FILEHANDLE> to input the contents of an open filehandle. Using just <> means if you pass filenames on the commandline when calling your scripts then these are taken as files to open and read one by one until they are exhausted. If you don't pass any filenames then <> just defaults to reading the STDIN. For example, if you call the below code with: file1 file2 file3
    while (<>) { print "$_\n"; }
    ... then you see the entire contents of each file output to the screen. If you didn't pass any files then anything passed to STDIN is used.

    On top of all this you can get a lot more functionality by using the "Getopt::Long" module as others suggested. Its worth taking the time to get to know this module. If you need an example of how to use this let me know and I'll include one.

    The Funkster of Mirth
    Programming these days takes more than a lone avenger with a compiler. - sam
    RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
Re: arguments
by TomDLux (Vicar) on May 01, 2004 at 13:34 UTC

    This question shows a total lack of effort on your part. Did you know Perl comes with extensive documetantion, installed on your computer? Start by typing perldoc perl.

    If you can't find simple answers that are right there in front of you, how will you handle more challenging problems?


      This pdf perldoc* is easier to read (and search for anything). It comes all in one pdf. Get it and goodbye doubts!.


      _`(___)' __________________________

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://349562]
Approved by Paladin
[Discipulus]: just after earthquake mobile phones never work: but they could evacuate to open schoolyard and then call or go back to school
[LanX]: "evacuate to open schoolyard" is standard procedure here
[LanX]: Italy has a problem with Africa, very pushy continent ;-)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2017-01-18 12:08 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (161 votes). Check out past polls.