Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Using perl's runtime options with -Mstrict

by deprecated (Priest)
on Jan 27, 2001 at 03:16 UTC ( [id://54671]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings, Monks.

I was recently turned on to perl -s as a way to parse command line options (please, I dont want to know about any more command line options modules, its way overkill).

So, without further ado, from perldoc perlrun:

-s enables rudimentary switch parsing for switches on the command line after the program name but before any filename arguments (or before a --). Any switch found there is removed from @ARGV and sets the corresponding variable in the Perl program. The following program prints "1" if the program is invoked with a -xyz switch, and "abc" if it is invoked with -xyz=abc. #!/usr/bin/perl -s if ($xyz) { print "$xyz\n" }
So I am taken to understanding that I can then use an option from the command line to have $xyz (or $switch, as used in the example below) defined.

Here's a relevant example:

#!/usr/bin/perl -ws # minus-ess.pl use strict; # if we run it ./minus-ess.pl -switch "foo" it should be defined. my $switch = "hooray" unless (defined $switch); print $switch;

which yields us:

Global symbol "$switch" requires explicit package name at minus-ess.pl + line 7.
I guess then what I need to do is "turn off" strict while I am parsing the command-line options. My idea was to use an anonymous code block ( { ... } ) turn off strict in there, and then just close it.

thanks,
dep.
--
i am not cool enough to have a signature.

Replies are listed 'Best First'.
(tye)Re: Using perl's runtime options with -Mstrict
by tye (Sage) on Jan 27, 2001 at 03:29 UTC

    -s is for one-liners (and even then isn't very useful). -Mstrict is not for one-liners. The two are pretty much completely incompatible. However, you could just write:

    #!/usr/bin/perl -ws use strict; # My supported command-line switches: use vars qw( $switch $help $debug $foo ); $switch= "hooray" unless $switch;
    I see no reason to have a global $switch that Perl creates and then have a different, same-named lexical that you create with my.

    Note that anyone using your script can accidentally type

    minus-ess -snitch -helf -debung -food
    and your program will silently act just like they hadn't included any command-line switches (that is, unless your program also defines a global named, for example, $food, and then relies on it to be initially uninitialized).

    In other words, I don't consider this even close to a good design decision. ):

            - tye (but my friends call me "Tye")
Re (tilly) 1: Using perl's runtime options with -Mstrict
by tilly (Archbishop) on Jan 27, 2001 at 04:27 UTC
    Expanding on what tye said, you are wandering in very dangerous territory with your construct. First of all your construct is wandering into the my $foo if 0 bug. Secondly if your user mistypes the command line ever so slightly then neither you nor they get a chance to catch that fact. Thirdly you do not have any chance to choose to have certain options take arguments.

    I generally rectify all of these with Getopt::Std. If you want longer option names, you can use Getopt::Long instead. (It has a significantly more complex interface though.) Both are part of the standard distribution..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54671]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-25 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found