in reply to my Scope Pop Quiz

This came up recently, so I know that:

1) strict doesn't complain. $rv is in a foreach loop, but it's not in a block (so it's file scoped).

2) I have no idea why, but after the loop, $rv will be set to the null string ("") and not 42 or undef. Therefore, the output will be a blank line.

my ... foreach ... is to be avoided.

Update: While I don't get any warnings under -w or use warnings, print(defined($rv)?1:0, "\n"); prints 0, indicating $rv is undefined. ActivePerl v5.6.1

Replies are listed 'Best First'.
Re^2: my Scope Pop Quiz
by Mr. Muskrat (Canon) on Apr 09, 2005 at 01:36 UTC

    $rv is clearly undefined. An undefined value is represented by an empty string when printed (whether it is in an interpolated string or not).

    #!/usr/bin/perl use strict; my $rv = 42 for (1); print "rv unquoted: '", $rv, "'\n"; print "rv quoted: '$rv'\n";
      An undefined value is represented by an empty string when printed

      yes, but an undefined value should also print a warning under -w when one attempts to print it, and I'm not getting any warnings when I specify -w. Very odd.

        $ perl -we 'use strict; my $rv = 42 for (1); print $rv,$/' Use of uninitialized value in print at -e line 1.
        Seems to be printing a warning for me. The original code didn't include "use warnings" in it, so I added some and the warning expected did appear.
Re^2: my Scope Pop Quiz
by Random_Walk (Prior) on Apr 09, 2005 at 16:33 UTC
    >perl -v This is perl, v5.8.3 built for i386-linux-thread-multi Copyright 1987-2003, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.com/, the Perl Home Pa +ge. >cat rv #!/usr/bin/perl -w use strict; my $rv = 42 for (1); print "$rv\n"; >./rv Use of uninitialized value in concatenation (.) or string at ./rv line + 4. >perl -Mstrict -wle'my $rv=42 for (1); print ">$rv<"' Use of uninitialized value in concatenation (.) or string at -e line 1 +. >< >

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!