in reply to eval do {} -- magical variable assignment?

There's a difference between do BLOCK and do FILE (and require).

do BLOCK just returns the last statement calculated inside BLOCK, and eval happily connects the evaluated code to the current scope. In contrast, do FILE and require "bequeath an unrelated lexical scope" on the included code according to perlfaq8. So, changing your code slightly

#!/usr/bin/perl use strict; use Data::Dumper; my $file = q{alpha.txt}; my $class; #my $fh; #eval do { # open $fh, "<", $file or die "Unable to open: $!"; # local $/; # <$fh> #}; #die $@ if $@; #close $fh or die "Unable to close: $!"; do $file; print Dumper $class;

results in

$VAR1 = undef;

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: eval do {} -- magical variable assignment?
by aufflick (Deacon) on Jan 02, 2007 at 07:20 UTC
    Yes wow. And this is why it's funky - because my brain is parsing something like this:

    eval -> skip ahead to see if there is a " or a { nearby. read eval as "string eval" or "block eval" accordingly.
    Additionally, I suspect my brain was parsing out the "do" entirely since in many situations it is used simply because a block by itself won't work (ie. in the do/while construct).

    Yet another little nuance to add to my mental Perl parser. Which makes me wonder if the common saying:

    Nothing can parse Perl except perl
    Shouldn't be re-phrased to something like:
    Nothing can parse Perl except perl or Larry Wall's brain
    :)