Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Examing local's stack

by Ovid (Cardinal)
on Mar 21, 2006 at 18:20 UTC ( #538250=perlquestion: print w/replies, xml ) Need Help??

Ovid has asked for the wisdom of the Perl Monks concerning the following question:

I have some code which relies heavily on the fact that I can localize variables in recursive functions. However, sometimes I need to examine the top of the local stack:

#!/usr/bin/perl -l use strict; use warnings; our $x = 3; foo(7); sub foo { local $x = shift; print $x; # is there any way to get the "3" value? }

The obvious solution is for me to cache the top-level values of the variables in question and that's probably what I will do, but I was wondering if there is any way to examine the stack created by local? I'd prefer to do this without C, but if I must ...

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: Examing local's stack
by chromatic (Archbishop) on Mar 21, 2006 at 19:00 UTC

    If you only care about arguments passed to your recursive subroutine, you can use the uplevel_args() trick alluded to in caller:

    package DB; sub uplevel_args { my $level = shift; my @foo = caller( $level ); return @DB::args; }

    Then just call this from your code to get the argument list passed to your code... most of the time.

Re: Examing local's stack
by codeacrobat (Chaplain) on Mar 21, 2006 at 19:21 UTC
    Don't know if this counts ;-)
    $ perl -l use strict; use warnings; our $x = 3; foo(7); sub foo { my $oldx=$x; local $x = shift; print $x; print $oldx; }
      This won't work with recursive functions:
      #!/usr/bin/perl use strict; use warnings; our $x = 3; foo(7); sub foo { my $oldx=$x; local $x = shift; print "localised x = $x\n"; print " old x = $oldx\n"; foo(0) if $x; # recursive call, only 1 time } __END__ localised x = 7 old x = 3 localised x = 0 old x = 7

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
Re: Examing local's stack
by Anonymous Monk on Mar 21, 2006 at 20:34 UTC
    The obvious solution is for me to cache the top-level values of the variables in question and that's probably what I will do, but I was wondering if there is any way to examine the stack created by local?

    Create your own stack without bothering with local()? It doesn't sound like it's doing what you want it to do, so, performance issues aside, why bother with it?

    { # start of block of variables private to function foo() my @stack; sub foo { my ($var) = shift; push(@stack, $x); $x=$var; # code involving foo() and $x goes here... $x=pop(@stack); } # end foo } # end block of variables private to function foo()

    Just a thought...

    --
    Ytrew

      Create your own stack without bothering with local()?

      Or localise your stack.

      our @x = 3; foo(7); sub foo { local @x = ( shift, @x ); print $x[0], "\n"; }

      Not the most efficient thing in the world though... I miss languages where I can apply expressions to dynamic scope entry/exit :-)

Re: Examing local's stack
by Elian (Parson) on Mar 24, 2006 at 02:26 UTC
    Do you not want C involved, or just don't want to write it? It seems a useful feature to have, albeit one that'll require some internals grovelling, and I've been hankering to grovel the internals again recently...

      I don't mind C being involved, but my C is weak and my knowledge of Perl's internals is even weaker. I wouldn't know where to start.

      Cheers,
      Ovid

      New address of my CGI Course.

        Gotcha. Okay, I think I may go fiddle then, and see what happens. That's always fun, if not a bit dangerous. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://538250]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2021-05-17 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (160 votes). Check out past polls.

    Notices?