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

Does anyone know why this snippet returns:
Use of uninitialized value in string eq at G:\output.pl line 6.
The error is returned when $ARGV[0] isn't passed anything
#!/usr/bin/perl use warnings; use strict; if ($ARGV[0] eq 'TEST') { print "$ARGV[0]\n"; }
Thanks!

Replies are listed 'Best First'.
Re: using $ARGV without arguments
by hardburn (Abbot) on Jun 24, 2003 at 14:01 UTC

    When you don't pass anything in, $ARGV[0] won't be defined. Under use warnings, trying to compare an undefined value gives you that (non-fatal) error message. To stop it, first check if $ARGV[0] is defined, then do your test:

    if( defined($ARGV[0]) && ($ARGV[0] eq 'TEST') ) {

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: using $ARGV without arguments
by broquaint (Abbot) on Jun 24, 2003 at 14:01 UTC
    Because the 0th element of @ARGV is empty and therefore uninitialized. To supress the warning just check if @ARGV has been populated e.g
    if(@ARGV and $ARGV[0] eq 'TEST') { print "$ARGV[0]\n"; }
    See. perlvar for more info on @ARGV.
    HTH

    _________
    broquaint

    update: 'pritn' ne 'print', thanks to particle

Re: using $ARGV without arguments
by Bilbo (Pilgrim) on Jun 24, 2003 at 14:07 UTC

    Well, if you don't put any arguments in then $ARGV[0] is undefined. What do you want to do if no arguments are given? Do you want it to cause an error:

    unless (@ARGV == 1) { die "You must specify one argument\n"; }

    Or do you want to set a default value?

    my $argument = $ARGV[0] || 'default' ; # Put your default # value here if ($argument eq 'TEST') {....

    Or do you just want it to ignore the argument?

    if (defined $ARGV[0] && $ARGV[0] eq 'TEST') {...
      Question on acceptable practice.

      Subject, warning on Use of uninitialized value in string eq at ...

      When this happens, I go back and check my code to make sure it is OK the value is sometimes unitialized. If it OK, I move on and dont worry about it.

      Is this a bad habit???
        Yes. Lots of Famous Really Good Programmers (tm) will tell you to always initialize vars---but since I'm not one of them, I'll tell you to at least try and make it a habit since it will save you grief later on. My rule is that if there is a chance that I'll check the value before I stuff something in it, I'll usually set it to a known value...

        --hsm

        "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: using $ARGV without arguments
by tcf22 (Priest) on Jun 24, 2003 at 14:05 UTC
    You could set it to a blank string if not defined like this
    #!/usr/bin/perl use warnings; use strict; $ARGV[0] ||= ''; if ($ARGV[0] eq 'TEST') { print "$ARGV[0]\n"; }
    or check for it being defined like this
    #!/usr/bin/perl use warnings; use strict; if (defined($ARGV[0]) && $ARGV[0] eq 'TEST') { print "$ARGV[0]\n"; }
    I prefer the second option.