Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

$ARGV dilemma

by dusk (Friar)
on May 27, 2001 at 06:01 UTC ( #83561=perlquestion: print w/replies, xml ) Need Help??

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

Fellow monks, I am having a problem using $ARGV as I intend, here is the problem:

I am trying to force that 3 mandatory arguments are included at execution. for example:

die "Usage: $0 [arg] [arg] [arg]\n" unless ($ARGV[0], $ARGV[1], $ARGV[2]);

However, this doesn't work... Any ideas what will?

Replies are listed 'Best First'.
Re: $ARGV dilemma
by blue_cowdawg (Monsignor) on May 27, 2001 at 15:42 UTC

    Check out the following:

    --$ cat argvtest.pl #!/usr/bin/perl -w ################################################################### use strict; print "too few$/" if scalar(@ARGV) < 3 ; print "just right$/" if scalar(@ARGV) == 3; print "too many$/" if scalar(@ARGV) > 3; --$ perl argvtest.pl 1 2 too few --$ perl argvtest.pl 1 2 3 just right --$ perl argvtest.pl 1 2 3 4 too many --$

    scalar is your friend! ;-)
    HTH


    Peter L. BergholdSchooner Technology Consulting, Inc.
    Peter@Berghold.Netwww.berghold.net
      Doesn't numeric comparison already put it in scalar context? i.e. isn't
      print "too few$/" if scalar(@ARGV) < 3;
      Equivalent to:
      print "too few$/" if @ARGV < 3;

        While I can't show you an example: I have seen numeric comparisons not work. Wish I could remember where it happened but it was a while ago...


        Peter L. BergholdSchooner Technology Consulting, Inc.
        Peter@Berghold.Netwww.berghold.net
Re: $ARGV dilemma
by dusk (Friar) on May 27, 2001 at 06:11 UTC
    UPDATE: Err, I answered my own question right after submission; I apologize for asking such a silly question, and jumping to ask, before giving it any thought.
    The solution is, of course, to just use unless $ARGV[2].

    UPDATE 2: I guess I should have been more precise in both my question and update. Originally, the last argument was 0; but changing it to a string was the solution I came up with..

        unless $ARGV[2] works until someone passes a zero. You could amend it to   unless defined $ARGV[2] though   if 3 > @ARGV reads better, at least in my opinion.

      That won't always work.

      What if @ARGV = ("0","0","0"). Then $ARGV[2] is false and you get an error despite having three arguments.

      You probably like something like unless @ARGV == 3.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2021-12-09 02:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?