This should illustrate what is going on: use strict;
my $lTokens = [qw(2 * 2)]; print "1 before parse: ", $lTokens, "\n"; my $errmsg = parse($lTokens); print($errmsg ? "ERROR: $errmsg\n" : "OK\n"); $lTokens = [qw(2 * 2)]; print "2 before parse: ", $lTokens, "\n"; my $errmsg = parse($lTokens); print($errmsg ? "ERROR: $errmsg\n" : "OK\n"); # ------------------------------------------- sub parse { my($lTokens, $callback) = @_; print "1 parse: ", $lTokens, "\n"; sub gettoken { print "1 gettoken: ", $lTokens, "\n"; if (@$lTokens==0) { return undef; } my $token = shift(@$lTokens); return $token; } # gettoken() my $tok = gettoken(); print("TOKEN: '$tok'\n"); return("Empty Input") if !$tok; $tok = gettoken(); while ($tok) { print("TOKEN: $tok\n"); $tok = gettoken(); } } On 2/8/06, John Deighan <[EMAIL PROTECTED]> wrote: > I realize that most things that look like bugs in Perl are in reality > programmer bugs, but I can't figure out why the second call to the > function "parse()" in the code below fails. According to my debugger > (from ActiveState's Perl Development Kit), the function parse() gets > a correct list of 3 tokens in parameter $lTokens, but when the > embedded function gettoken() is called, it thinks that $lTokens is an > empty list. This only happens the second time that parse() is called, > not the first time. I'm just completely baffled. > > P.S. Please don't send me e-mails about how to get this working right > by modifying the code. I'm sure I can do that, and besides, the real > code is considerably more complicated than this. I want to know why > this code does not work as I expect. Anyway, here's the code and the > output I get (I'm running ActivePerl 5.8.7 Build 815 under Windows > 2000 Server): > > use strict; > > my $lTokens = [qw(2 * 2)]; > my $errmsg = parse($lTokens); > print($errmsg ? "ERROR: $errmsg\n" : "OK\n"); > > $lTokens = [qw(2 * 2)]; > my $errmsg = parse($lTokens); > print($errmsg ? "ERROR: $errmsg\n" : "OK\n"); > > # ------------------------------------------- > > sub parse { my($lTokens, $callback) = @_; > > sub gettoken { > > if (@$lTokens==0) { > return undef; > } > my $token = shift(@$lTokens); > return $token; > } # gettoken() > > my $tok = gettoken(); > print("TOKEN: '$tok'\n"); > return("Empty Input") if !$tok; > $tok = gettoken(); > while ($tok) { > print("TOKEN: $tok\n"); > $tok = gettoken(); > } > } > > > --------------- > OUTPUT: > --------------- > C:\Scripts>testGrammar2.pl > TOKEN: '2' > TOKEN: * > TOKEN: 2 > OK > TOKEN: '' > ERROR: Empty Input > > _______________________________________________ > Perl-Win32-Users mailing list > Perl-Win32-Users@listserv.ActiveState.com > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs > _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs