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

I don't know if it's just late or I'm actually losing my mind.

I am trying to verify that the user is actually passing a parameter to a script.

the script would be called traditionally: "./scriptNAME parameter"

in the script I was then attempting to check and see if there was anythnig in @_ (hence a parameter was passed). I was doing this with:

if($#_) { do stuff } else { print "Bad user. Input parameter.\n"; }

The thing is that no matter what I call (ie: "./scriptNAME" or "./scriptNAME parameter") $#_ is always -1. I am not using shift before the test, merely a few global definitions (mysql login information, but it's all explicitly defined, none is being generated by the script).

Thanks for the support, sorry if I am just having a mental fart.

~~Guildencrantz

Replies are listed 'Best First'.
Re: Test for parameters?
by chromatic (Archbishop) on Mar 27, 2003 at 07:59 UTC

    You probably mean @ARGV instead.

    Checking the index of the last element of the array is rarely the best option, too. In scalar context, the array gives you the number of elements -- that works very well in the boolean context used here:

    die "Exiting without parameter.\n" unless @ARGV;

      ARG!!!!!!!

      It is absolutely insane how freaking stupid things get. Yes, @ARGV. @_ is when passing parameters to FUNCTIONS, not into the script itself. See, I know this.

      I think I need sleep.

      Thank you so much.

      Maybe I just need caffein?

      Anyway, I really appreciate that. Every now and then I just need somebody to take a 2x4 and smack me back into reality.

      ~~Guildencrantz

        Guildencrantz said:

        ARG!!!!!!!

        Silly, silly, silly.... it's not @ARG, is @ARGV :P

        ( Sorry folks, I just had to throw that in! )


        If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Re: Test for parameters?
by robartes (Priest) on Mar 27, 2003 at 08:07 UTC
    @_ gets set to the parameters passed to a function call, not for the command line parameters. For that you use @ARGV:
    $ perl -e 'print @_;print "::\n";print @ARGV;' camel :: camel
    To get the number of parameters, you use @ARGV in scalar context:
    $ perl -e ''print scalar @ARGV;' my camel 2

    CU
    Robartes-