Here's a simple example that illustrates the problem I've run into:

perl -le'
show();
{   my @fibs = (0,1,1); my ($x, $y) = (1,2);
    sub show
    {   print "x=$x\ty=$y\t\$#fibs=$#fibs\tfibs=@fibs\tscalar \@fibs = ",
            scalar @fibs;
    };
    $fibs[$#fibs+1] = 2;
}
show();
'
x=      y=      $#fibs=-1       fibs=   scalar @fibs = 0
x=1     y=2     $#fibs=3        fibs=0 1 1 2    scalar @fibs = 4

The first time show() runs it acts as if it can't see $x, $y or @fibs.
The second time it does yet the interpreter had to have seen $x, $y
and @fibs in order to find the definition of show().

    I've grown accustomed to writing 'C' style code in Perl with main
before the subs rather than shell or Forth style but here it seems to
fail.

    I know I can use memoize but where I ran across this problem was
in trying to write a fibonacci routine that would take advantage of
any previously calculated values rather than starting at 1 again as 
a memoized function would be required to do.

    I've searched 'Programming|Learning|Intermediate Perl' books as
well as Google, PerlMonks and this list but haven't found an explanation
for what I'm seeing and explanation is what I'm interested in though lacking
that a work-around would help. BTW, this is Perl 5.8.8.

    Any hints will be appreciated.
Thanks,
Mike McClain
-- 
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to