in reply to Scope Issue

If you're using Perl 5.10 or greater, you can also use the defined or operator:

my $f1missing = $t->waitfor(String => 'That file is unavailable', errmode => 'return') // 0;

What the code above does is similar to what Corion suggested. It tests whether the result is defined, and will substitute with 0 if it is not.

Replies are listed 'Best First'.
Re^2: Scope Issue
by fidesachates (Monk) on Mar 15, 2011 at 13:12 UTC
    Am I wrong or did you mean || instead of //?

      // is the defined or operator. || is the or operator. // and //= were added in in 5.10 as a shortcut for something along the lines of $var = defined $var ? $var : $default_value;

      $var //= $default_value; is much prettier IMHO. I'm fairly certain // and || short-circuit in the same exact way. It's easy enough to test.

        I tried || and // and they both worked. Thanks to everyone for your contribution!

      || is the regular or operator ("true, or ..." you might say).

      // is the defined or operator ("defined, or ...").

        ah. good deal.
Re^2: Scope Issue
by MKJ747 (Acolyte) on Mar 15, 2011 at 13:46 UTC

    Thanks, AR. That worked - the warning went away.

      I have another question on this topic. I'm also getting a similar warning in another part of the code: "Use of uninitialized value $\ in concatenation (.) or string at C:\scripts\DailyComp2.8.pl line 208 (#1)". This warning comes from the following line of code:

      my $match_live = "\/LIVE>.*$\/";

      I am defining a pattern match for Net::Telnet to use in various places in the code. The back slash in $\ is there to escape the forward slash which is the end of the pattern match, but it looks like PERL is interpreting it as variable. Any suggestions? Thanks

        If you are constructing regular expressions, the qr operator is much more convenient:

        my $match_live = qr/LIVE>.*$/;

        If Net::Telnet does not want qr-quoted regular expressions, use single quotes to construct your string and not interpolate variables:

        my $match_live = q'/LIVE>.*$/';