Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Getting commandline params

by Amoe (Friar)
on Sep 01, 2001 at 21:49 UTC ( #109658=perlquestion: print w/replies, xml ) Need Help??

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

I'm having trouble with reading command line params. I wanna have a switch "-p", if there's an argument an LWP::UserAgent's proxy is set to that, if there's the switch and no argument the proxy is got from the environment variables, and if there's no switch, no proxy. I got this code to do it:
use strict; use warnings; use Getopt::Std; use LWP::UserAgent; our($opt_p); getopt('p'); my $agent = LWP::UserAgent->new(); ($opt_p == 1 ? $agent->env_proxy() : $agent->proxy(http => $opt_p)) if + (defined($opt_p));
But it gives the warning "Argument "x" isn't numeric in numeric eq (==)". Now, obviously this is the fault of $opt_p == 1. That's there because if the switch has no argument, $opt_p is 1. But doesn't that expression return 0 anyway if the argument is a string? And if it does, why isn't the proxy getting set when I run it in debug mode with params?
--
sub version { print "I cuss your capitalist pig tendencies bad!"; }

Replies are listed 'Best First'.
Re: Getting commandline params
by tachyon (Chancellor) on Sep 01, 2001 at 22:07 UTC

    There is a difference between the getopt() method and the getopts() method which is the boolean one you want. Read the docs for details. With getopt() if you supply the command line argument -p12 then $opt_p = '12'. If you go -p then you just get an undefined value. With getopts() if you go -p you get then $opt_p = '1' which is what I imagine you want. If you don't supply the -p command line arg then you get an undefined value. This is presumably fixed version. 1 lousy 's' is all that was missing!

    use strict; use warnings; use Getopt::Std; use LWP::UserAgent; our($opt_p); getopts('p'); # getopts() not getopt() print "\nThe value in \$opt_p is "; print defined($opt_p) ? "'$opt_p'" : "undefined"; my $agent = LWP::UserAgent->new(); ($opt_p == 1 ? $agent->env_proxy() : $agent->proxy(http => $opt_p)) if + (defined($opt_p));
    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Getting commandline params
by tachyon (Chancellor) on Sep 02, 2001 at 01:50 UTC

    Here is the difference between the getopt() method and the getopts() method.

    getopt()

    C:\>type getopt.pl use Getopt::Std; our($opt_p); getopt('p'); print "Using the getopt() method \$opt_p is "; print defined $opt_p ? "'$opt_p'" : "undefined"; C:\>perl getopt.pl Using the getopt() method $opt_p is undefined C:\>perl getopt.pl -p Using the getopt() method $opt_p is undefined C:\>perl getopt.pl -p123 Using the getopt() method $opt_p is '123'

    getopts() with an s!

    C:\>type getopts.pl use Getopt::Std; our($opt_p); getopts('p'); print "Using the getopts() method \$opt_p is "; print defined $opt_p ? "'$opt_p'" : "undefined"; C:\>perl getopts.pl Using the getopts() method $opt_p is undefined C:\>perl getopts.pl -p Using the getopts() method $opt_p is '1' C:\>perl getopts.pl -p123 Unknown option: 1 Unknown option: 2 Unknown option: 3 Using the getopts() method $opt_p is '1' C:\>

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Okay, thanks for all the help. I'm halfway there now, I think. So if I wanted to get $switch to equal 1 if the switch was on its own and $arg to equal the argument, I should be able to this...
      use Getopt::Std; our($opt_p); getopt('p'); my $arg = $opt_p; getopts('p'); my $switch = $opt_p; print "Switch is $switch and arg is $arg\n";
      Sadly, when run:
      >perl stuff.pl Switch is and arg is # Okay so far... >perl stuff.pl -p Switch is and arg is # No, switch should equal 1... >perl stuff.pl -p http://www.proxy.com Switch is http://www.proxy.com and arg is http://www.proxy.com # Shou +ldn't switch equal 1 here too?
      So, any input on how to get the desired behaviour, or maybe I shouldn't be going about it like this anyway?

        I don't generaly use the Getopt module. I parse stuff myself to get the behaviour I want. You get the command line args in @ARGV. Here is an example with minimal flexibility and error checking. It takes a -p flag and and optional proxy arg.

        C:\>type test.pl my ($switch, $proxy) = @ARGV; $switch = ($switch eq '-p') ? 1 : 0; $proxy = defined $proxy ? $proxy : 'undef'; print "Switch is '$switch', and proxy is '$proxy'\n"; C:\>perl test.pl Switch is '0', and proxy is 'undef' C:\>perl test.pl -p Switch is '1', and proxy is 'undef' C:\>perl test.pl -p http://proxy.foo.com/proxy.pac Switch is '1', and proxy is 'http://proxy.foo.com/proxy.pac' C:\>perl test.pl http://proxy.foo.com/proxy.pac Switch is '0', and proxy is 'undef' C:\>

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Getting commandline params
by tachyon (Chancellor) on Sep 02, 2001 at 17:27 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2021-12-06 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (32 votes). Check out past polls.

    Notices?