http://www.perlmonks.org?node_id=103972

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

OK... so here I am teaching my self Perl. I am learning in the proper way (taking others scripts and learning from them, and converting my bash scripts to perl as a way to learn while doing.)

So here is my current snag... can i use command line options when running a perl program??

For example, in bash, I can set it up so that:

command opt1 opt1

translates opt1 into $1 and opt2 into $2.

but I havent gotten this to work in perl yet. I can do it using stdin variable declarations ($variable=<stdin>) but that makes the scripts a little more intensive than I want.

I simply want to run a script like this:

sync fromhost
 and not like this:
sync
ENTER DIRECTION: fromhost

any wisdom?

Replies are listed 'Best First'.
Re: command line options
by boo_radley (Parson) on Aug 10, 2001 at 22:19 UTC
    Hello, and welcome to the wonderful world of perl!
    There's a few ways to do this, here's two.
    You can scan through the values in @ARGV, one of perl's special variables. It contains all of the arguments following the script's name. From the description you give, this is probably good enough.
    There's also the module Getopt::Std. If you want to expand on the meaning and kinds of options, like script.pl -c "Foo.txt" :e -o=n, getopt is your man.

    Since you're teaching yourself, here's some basic ideas :
    Anything within 'core perl' is available in the core documentation. If you think you haven't found it, look again. :)
    Anything not in the core distribution can usually be found in CPAN. Almost any kind of module can be found here; quality varies occasionally, but most are very strong.
    Within the monastery, you can often pull up more detailed searches with super search than with search alone.
    Also, check out outside links to see what we enjoy and endorse.
    Welcome to perlmonks!

Re: command line options
by suaveant (Parson) on Aug 10, 2001 at 22:09 UTC
    In perl these show up in the @ARGV array... in your example opt1 would be in $ARGV[0] and opt2 would be in $ARGV[1] there are also packages like the ones in Getopt that can be very useful :)

                    - Ant
                    - Some of my best work - Fish Dinner

Re: (elbie) command line options
by elbie (Curate) on Aug 12, 2001 at 07:43 UTC
    One of the beautiful switches in perl is the -s option.

    At the top of your perl script, if you include the -s switch along with any other switches you need (like -w and possibly -T), i.e. #!/usr/bin/perl -sw then any command line options you use will be set as variables in your script.

    The nice thing about this is that you no longer need to force users to put opt1 first, then opt2. Taken from the Camel book:

    The following script prints "true" only when the script is invoked with a -foo switch.

    #!/usr/bin/perl -s if ($foo) { print "true\n" }

    If the switch is of the form -xxx=yyy, the $xxx variable is set to whatever follows the equals sign in that argument ("yyy" in this case). The following script prints "true" if and only if the script is invoked with a -foo=bar switch:

    #!/usr/bin/perl -s if ($foo eq 'bar') { print "true\n" }

    (Me again) So, you can now invoke your script with scriptname -opt2=value2 -opt1=value1 and the variables $opt1 and $opt2 would still be correctly set. If you left one of the values out, then that variable simply wouldn't be defined.

    elbieelbieelbie