SPUG: do vs. eval with Data::Dumper
Tim Maher/CONSULTIX
tim at consultix-inc.com
Wed Jan 26 11:21:50 CST 2000
SPUGsters (and E-SPUGsters),
I'm working on a complicated Data::Dumper example for my "Intermediate
Perl" course, which worked just fine until I enabled "use strict" and
rendered some previously global variables private.
(I know, been there, done that, got the T-shirt . . .)
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.
Or is there another explanation for the output of this test program?
#! /usr/bin/perl -w
use strict;
my $size=0;
open FILE, '> /tmp/dofile' or die;
print FILE ' $size=42; ';
close FILE or die;
print "File contains: ", `cat /tmp/dofile`, "\n";
do '/tmp/dofile';
print "\nDo yields:\t$size\n"; # prints 0
eval `cat /tmp/dofile`;
print "\nEval yields:\t$size\n"; # prints 42
OUTPUT:
File contains: $size=42;
Do yields: 0
Eval yields: 42
Also, I'm trying to make an alias for Data::Dumper::Dump (unexported!),
using variations including:
*::Dump = \&Data::Dumper::Dump;
and
*Dump = *Data::Dumper::Dump;
but when I hit the line below, I get;
Can't coerce array into hash at
/usr/lib/Perl5/5.00502/i586-linux/Data/Dumper.pm line 161.
print Dump([$size, \%pals, $hosts, \@::words],
["size", "*${prefix}pals", "${prefix}hosts", "*${prefix}words"]);
(I set $prefix to :: to reload variables as globals, with "strict"
in effect)
What's wrong here? And why doesn't Data::Dumper export Dump()
in the first place? 8-}
TIA,
*========================================================================*
| Tim Maher, PhD Consultix & (206) 781-UNIX/8649 |
| Pacific Software Gurus, Inc Email: tim at consultix-inc.com |
| UNIX/Linux & Perl Training http://www.consultix-inc.com |
| 2/22: UNIX 2/28: Perl Modules 2/29: Int. Perl 3/3: Pattern Matching |
*========================================================================*
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
Email to majordomo at pm.org: ACTION spug-list your_address
More information about the spug-list
mailing list