SPUG: do vs. eval with Data::Dumper
Stuart Poulin
stuart_poulin at yahoo.com
Wed Jan 26 11:50:17 CST 2000
--- Tim Maher/CONSULTIX <tim at consultix-inc.com> wrote:
> SPUGsters (and E-SPUGsters),
> ...
> I found a solution, but it shouldn't work, from what I read! 8-}
> Specifically, although the Camel says that "do file" is the same as
> "eval `cat file`" (p. 158), except for a few minor details, I'm finding
> evidence that there's a fundamental difference.
>
> It looks like "do" is executing the code in its own scope, but eval is
> using the surrounding scope.
> ...
I think I ran into the same thing a while back. Charles DeRykus pointed out
that the documentation has been changed.
-Stu
__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com
-------------- next part --------------
> I was looking for a cheap rc type file behavior. I thought I could just use a
> "do" file with variables in it - but I wanted "use strict" to be enforced. The
> perlfunc man page says:
> do 'stat.pl';
> is just like
> scalar eval `cat stat.pl`;
> This doesn't appear to be true. This is on win98 so s/cat/type/ but "type" is
> expensive as far as run time. Is there a way to force a "use strict" on do
> files?
Stu,
I noticed now that Tom Christiansen has amended the docs
slightly to say:
do $file is like eval `cat $file`, except the former:
1.1 searchs @INC and updates @INC
1.2 bequeaths an unrelated "lexical" scope on the eval'ed code
(See: http://x32.deja.com/[ST_rn=ps]/getdoc.xp?AN=503977526&CONTEXT=933300222.1535180873&hitnum=1)
That's rather confusing though and doesn't account for instance
why 'use constant' is visible whereas 'use strict' pragma isn't
in the case below:
package main;
...
package doit:
use strict; # doesn't propagate to 'do file'
use constant CONS => 3 # does " " "
package main;
package doit;
do "doit.pl";
#--------------------------
# doit.pl
$whoops = "toss_an_error";
print "CONS = ", CONS, "\n"; # prints CONS = 3
So, you'll probably have to insert a 'use strict' in doit.pl
unless I'm missing something.
Regards,
--
Charles DeRykus
More information about the spug-list
mailing list