Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: 'Dynamic scoping' of capture variables ($1, $2, etc.)

by AnomalousMonk (Chancellor)
on Dec 16, 2012 at 22:13 UTC ( #1009097=note: print w/replies, xml ) Need Help??

in reply to Re: 'Dynamic scoping' of capture variables ($1, $2, etc.)
in thread 'Dynamic scoping' of capture variables ($1, $2, etc.)

[Emphases added.]
As to your actual question:
However, $1, set to '1' by the last successful match at the lowest-but-one level of recursion, is propagated upward unchanged through several levels of subroutine 'blocks'
I see no evidence for that. After $1 is set to '1', exactly one more recursive call happens, and there it is printed out. Then the recursion ends, and you don't print $1 anymore.

I don't print $1, but it must be '1' at all higher recursion levels because only that value will result in a sum total of 4.

However, I have not had (and will not immediately have) a chance to ponder your link and other responses.

I suppose my chief confusion stems from the fact that $1 starts out undefined, takes on a bunch of other values, then winds up as it started. If it had finished up as '1', my mind would rest a bit easier, but at some point, and only one point mind you, it's leaving some kind of scope and being restored to the value it had upon entry. I could understand all scopes, I could understand none, but I can't (yet) understand just one!

  • Comment on Re^2: 'Dynamic scoping' of capture variables ($1, $2, etc.)

Replies are listed 'Best First'.
Re^3: 'Dynamic scoping' of capture variables ($1, $2, etc.)
by LanX (Bishop) on Dec 16, 2012 at 22:48 UTC
    You are right be worried, your example code is just a bit to complex to make it evident at first glance.

    There is no logical reason why nested calls of the same function (i.e. recursions) should act differently to nested calls of different functions.

    see updated code, especially the second paragraph contrasting the bug.

    Cheers Rolf


    to be sure to avoid any side effects from eval within the debugger here a standalone file for testing:

    use warnings; use strict; use 5.10.0; my $x; sub delchar { $x =~ s/(\w)// ? $1 . delchar() . $1 : "x" } $x='abc'; say delchar(); # => "cccxccc" sub del1 { $x =~ s/(\w)// ? $1 . del2() . $1 : "x" } sub del2 { $x =~ s/(\w)// ? $1 . del3() . $1 : "x" } sub del3 { $x =~ s/(\w)// ? $1 . del4() . $1 : "x" } sub del4 { $x =~ s/(\w)// ? $1 . del5() . $1 : "x" } $x='abc'; say del1(); # => "abcxcba"


    Best practice is to copy captures like $1 ASAP! (not only in recursions)

    DB<105> sub delchar { my $m; $m = $1 if $x =~ s/(\w)//; $m ? $m . +delchar() . $m : "-" } => 0 DB<106> $x='abc'; delchar() => "abc-cba" DB<107> sub delchar { local $m; $m = $1 if $x =~ s/(\w)//; $m ? $m + . delchar() . $m : "-" } => 0 DB<108> $x='abc'; delchar() => "abc-cba"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009097]
[ambrus]: Presentations come in many different shapes, and so slides do as well.
[Corion]: Doing that in Powerpoint or Ooxml would be nice(r) but I'm much quicker doing the outline of a presentation and the code as Pod
[Corion]: Hurrr - on a machine that is behaving weirdly, I have two processes CROND running. I guess that is the source of unattended jobs sometimes not finding their files anymore...
[ambrus]: Most of the time if I make slides, they're just a formatted document with a medium level of formality (between a well written article and an informal draft), with usually the page breaks chosen carefully and possibly some content repeated between pages.
[Corion]: ambrus: My slides are mostly a list of things I want to talk about. But sometimes I want to point out interesting parts, or build up a larger image from small components. This is where animations/ highlights would come in well
[choroba]: what I like about slides in HTML + CSS is I can use templates to produce them, which means all code in slides is tested, and I can version them in git
[ambrus]: Btw, a request to everyone who organizes international conferences where presenters will show slides.
[ambrus]: In the document where you tell people how long the presentations are supposed to be, please also tell the aspect ratio of your projectors, because that's not obvious in advance.
[Corion]: ambrus: A more important point is that when constructing slides, you should respect a margin around your slide content of maybe 10% where no important information should be ;)
[ambrus]: If you don't do that, some people will be able to use only two thirds of the available screen area, or have to prepare three versions of their slides, each of which is a waste that you could avoid with one simple phrase in the call for papers.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2017-09-26 10:08 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.