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

I want to do certain things if -e $info, but I'm getting uninitialized value errors on the code inside the if statements. The only reason I would get the error is if $info doesn't exist, but in that case the if statement shouldn't continue. Right?

I've tried this:
if ((-e $info) and ($query =~ /support/)) { open (FILE, $info) or die "Can't open file!"; print while <FILE>; close FILE; } if ((-e $info) and ($query ne /support/)) { print "<a href\=\"$webdir/cgi-bin/getprods.pl?$prodlisting\">"; print "Product Listing</a><br><br>"; open (FILE, $info) or die "Can't open file!"; print while <FILE>; close FILE; }
and this
if (-e $info) { if ((-e $info) and ($query =~ /support/)) { open (FILE, $info) or die "Can't open file!"; print while <FILE>; close FILE; } if ((-e $info) and ($query ne /support/)) { print "<a href\=\"$webdir/cgi-bin/getprods.pl?$prodlisting\">"; print "Product Listing</a><br><br>"; open (FILE, $info) or die "Can't open file!"; print while <FILE>; close FILE; } }
And, another problem:
If I use:
if ((-e $info) and ($query ne /support/))
it returns true no matter what (with ne)

but if I use an arithmatice operator it returns false, when it should, but gives me an error message about using the wrong type of operator.

Thank you

Replies are listed 'Best First'.
Re: if statement/uninitialized value
by broquaint (Abbot) on May 28, 2002 at 14:00 UTC
    If you get an uninitialized warning it means that the variable in question hasn't been assigned a value yet e.g
    my $var; print "\$var not defined" unless defined $var;
    Here the output will be "$var not defined" because $var hasn't been assigned a value yet so it defaults to undef when evaluated.

    Concerning your last problem

    if ((-e $info) and ($query ne /support/))
    The ne operator will compare two *strings*. So what is happening there is something like this
    if ((-e $info) and ($query ne ($_ =~ /support/)))
    Which is not what you want, so you should probably change the ne to a negated regex match (which is !~).
    HTH

    _________
    broquaint

Re: if statement/uninitialized value
by Joost (Canon) on May 28, 2002 at 14:03 UTC
    On which lines are your warnings? If it is on this line:
    if ((-e $info) and ($query =~ /support/))
    then either $info or $query are uninitialized

    On the other problem:

    if ((-e $info) and ($query ne /support/))
    is probably not what you would want: $query ne /support/ means:

    1. match $_ with /support/ 2. compare the result (which will be true or false) as a string with $query...

    You might take a look at

    perldoc perlre perldoc perlop (look at the section on m// )
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: if statement/uninitialized value
by ides (Deacon) on May 28, 2002 at 13:57 UTC
    Are you sure you are getting an uninitialized error or just a warning? Is there data in both $info and $query? Also if your might try:
    $query ne 'support'
    versus your current /support/.

    -----------------------------------
    Frank Wiles <frank@wiles.org>
    http://frank.wiles.org

Re: if statement/uninitialized value
by malaga (Pilgrim) on May 28, 2002 at 14:09 UTC
    This place is so cool. I love it. Thanks for the answers.

    I'm getting the unitialized error on whichever variable is uninitialized (depending on what I'm sending through the script). but my question is why is it getting past the if -e $info? if the file it doesn't exist it shouldn't run the if statement, right? and if it doesn't run the if statement i shouldn't get the error. what am i missing?
      $info must be undef:
      $ perl -w use strict; print -e undef; Use of uninitialized value in -e at - line 2. Use of uninitialized value in print at - line 2.
      The Use of uninitialized value message is only a warning[1] and since warnings are merely advisory your program shall continue on it's merry way.
      if the file doesn't exist it shouldn't run the if statement, right?
      Right. So the code to be executed if the given condition is true won't be executed because -e undef will always return false.
      if it doesn't run the if statement i shouldn't get the error
      But the condition still has to be tested, so $info still has to be evaluated and the warning will be raised if $info hasn't been initialised at that point.
      HTH

      _________
      broquaint

      [1] for more info on warnings in perl see the perllexwarn and perldiag manpages

Re: if statement/uninitialized value
by Aristotle (Chancellor) on May 28, 2002 at 16:29 UTC
    Try adding if(defined $info) (and cleaning up the logic flow) like so:
    if (defined($info) and (-e $info)) { unless ($query =~ /support/)) { print "<a href\=\"$webdir/cgi-bin/getprods.pl?$prodlisting\">" +; print "Product Listing</a><br><br>"; } open (FILE, $info) or die "Can't open file!"; print while <FILE>; close FILE; }

    (Note you can do away with that while loop also like so:
    { local $/ = undef; open (FILE, $info) or die "Can't open file!"; print <FILE>; close FILE; }
    See perlvar for an explanation of $/.)

    Makeshifts last the longest.

Re: if statement/uninitialized value
by malaga (Pilgrim) on May 28, 2002 at 19:09 UTC
    b e a u t i f u l, thanks.