develooper Front page | perl.perl5.porters | Postings from November 2003

Ignorant wumpus? Or scoping problem?

Thread Next
Glenn Linderman
November 25, 2003 14:14
Ignorant wumpus? Or scoping problem?
Message ID:
I asked this question in the forum, but 
apparently there isn't the right level of expertise there to provide a 
definitive answer, so I'm trying here... if there is a better forum for 
this, please inform me, and I'll go there.


Using Perl 5.8.0 ActiveState build 805 on Windows 2000 SP4

I have this script that is 1.2 MB long.... rather than post the whole
thing, I've made a test case... surprisingly I was able to get it down
to 60 lines.  I guess it could have even been a little smaller...

So I'm writing an interpreter, in Perl, for another language (no, this
is a real project, not an attempt to bring a Pentium 4 to its knees).
The other language is not available on Windows, and my collection of
machines that run it is dying... being about 15 years old and orphaned
about 12 years.  The basic idea is to provide the building blocks of the
other language inside perl as perl functions, and then to translate each
syntactical construct of the other language into perl syntax, thus
building up a perl function that is functionally equivalent to the
function in the other language.  The perl functions are first built into
strings, and then "eval"ed into existance.  Of course, as with all
automated translation, the resultant Perl is not particularly beautiful
to behold, but it is somewhat decipherable for debugging, and with the
source of the other language available, it should be an OK environment.

I have the translator mostly finished, and was starting to attempt to
execute some code from the other language (and it is the source code in
the other language that is the bulk of the Perl script).

Questions, problems, and misunderstandings:

1) Do the initial "use strict" and "use warnings" apply to the eval'd
code, or do I need to include them in each eval'd string as shown?
Initially, I didn't generate the "use strict" and "use warnings" into
each eval'd string, but after I ran into some problems, I went back and
added them.  Didn't change the problem.  And from what I can read, I
think I don't really need to do that, that the eval's should inherit the
"use strict" and "use warnings" from the lexical scope in which the eval

2) The variables of interest are all lexicals in the scope of the
closure where function foo is defined... $scaler, @array, and %hash.
The functions, with strict and warnings enabled, seem to compile fine.
But when function eval_printall runs, the variables seem to all be
undefined, even though they all have been given values in the code
immediately after their creation, and even though those values are
visible to function printall.  I would expect that either the variables
would not be visable at all, and function eval_printall would fail to
compile OR (more likely, from my reading of the Camel book) that the
function would compile successfully, the variables would be visible, and
the values of the variables available.  This last seems not to be the
case, however.

Does anyone have a clue what is going on here?  Is this a perl bug?  Do
I just need some additional magic option?  or what?

Interestingly, if you comment out the line in function eval_printall
which prints the scaler, you don't even get a warning message... the
variables apparently all appear to be undef, and the way the access to
@array and %hash are written, they simply don't have values, so nothing
gets printed... without warning.  This really confused me last night,
and until I made this test case this morning.

Not that I'm not still confused, but if all the variables appear undef,
it explains why I got some of the symptoms I got.

3) If this is a bug, or a technical question no one here can answer,
what forum should I turn to next?

Pared down test case code:
(you should be able to pipe this message to perl -x to execute the test

#!perl -w
use strict;
use warnings;

   my ( $scaler, @array, %hash );
   $scaler = 33;
   push @array, 1, 22, 333, 4444;
   $hash{'key'} = 'value';
   $hash{'value'} = 'key';
   $hash{'unique'} = 'plain';

   sub printall
   { print "scaler: $scaler\n";
     foreach my $ix ( 0 .. $#array )
     { print "array: $ix $array[ $ix ]\n"
     foreach my $key ( keys %hash )
     { print "hash key/value: $key/$hash{ $key }\n";

   my $outerstring = <<'END_OF_OUTER_STRING';
   use strict;
   use warnings;
   my $innerstring = <<'END_OF_INNER_STRING';
   use strict;
   use warnings;
   sub eval_printall
   { print "eval_printall coming up!  Watch this space!\n";
     print "eval scaler: $scaler\n";
     foreach my $ix ( 0 .. $#array )
     { print "eval array: $ix $array[ $ix ]\n"
     foreach my $key ( keys %hash )
     { print "eval hash key/value: $key/$hash{ $key }\n";

   print "before eval\n";
   & printall();

   eval $innerstring;

   print "after eval\n";
   & printall();
   & eval_printall();
   print "after eval_printall\n";
   & printall();

   sub foo
   { eval $outerstring;

& foo ();

Glenn --
Like almost everyone, I receive a lot of spam every day, much of it
offering to help me get out of debt or get rich quick.  It's ridiculous.
-- Bill Gates

And here is why it is ridiculous:
  The division that includes Windows posted an operating profit of $2.26 
  billion on revenue of $2.81 billion.
  --from Reuters via

So that's profit of over 400% of investment... with a bit more 
investment in Windows technology, particularly in the area of 
reliability, the profit percentage might go down, but so might the bugs 
and security problems?  Seems like it would be a reasonable tradeoff. 
WalMart earnings are 3.4% of investment.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About