in reply to goto &sub and local question

Is it not the case that goto &sub constitutes a call preserving the call stack to the called sub whereas the local definition does not modify the call stack, hence is not seen by the called sub, but instead instantiates a global variable locally ??

Or am I, once again, missing something fundamentally obvious ??

Update

Or as both ccn & ikegami both so eloquently put it when they were quicker to the draw ...

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: goto &sub and local question
by rovf (Priest) on Nov 24, 2008 at 09:21 UTC

    That's what I thought too, but as it was pointed out above, it is not correct, and if I had read the docs instead of trusting my memory, I wouldn't have posted this question. After all, the docs clearly say that localization is undone.

    Whether this is a nice concept or not, is another question. It seems that when you want to pass control to another sub without creating a new stack frame, the only possibility to pass additional information accross the goto &... boundary is by using global (non-localized) variables.

    OTOH, I think the occasions to use such a construct are so rare that it's not so bad to use a non-localized global variable for this task. </c>

    -- 
    Ronald Fischer <ynnor@mm.st>
      the only possibility to pass additional information accross the goto &... boundary is by using global (non-localized) variables.

      Depending on the use case you might be able to pass the data via @_:

      sub s1 { my $data = pop; print Dumper $data; } sub s0 { push @_, [45, 'D']; goto &s1; } s0;

      The details of how to do it without interfering with any existing arguments largely depend on the specific use case.

        Great idea, thanks a lot! In my case, it would be unshift instead of push, and it would do exactly what I wanted.

        -- 
        Ronald Fischer <ynnor@mm.st>
      Mmm, I wasn't questioning the localisation per-se, but the fact that your use of local (pun unavoidable:-) doesn't localise a global variable - which the docs say it does/should - rather, it purports to declare a global variable, which AFAICT, you can't from within non-global scope.

      All that being said, it does rather assume that global is seen as a true global i.e. a package global variable c/w a variable declared to be accessible from the point of declaration down...

      There now, you should be as confused as I am ;-)

      A user level that continues to overstate my experience :-))
      What about this: It definitely isn't global, although it is shared between the two functions.
      { my %familySecrets; sub s1 { # Read family secrets } sub s2 { #write family secret #call s1 } }

        If you plan on initializing %familySecrets, you need to use a BEGIN block if you are in main. In a module, this isn't necessary. Technically, you can use any one of the other special named blocks that run before your code: BEGIN, UNITCHECK, CHECK, or INIT. See perlmod for more details on these blocks.

        { my %familySecrets; BEGIN { %familySecrets = ( closet => 'skeleton', herbs_and_spices => 11, coke_recipe => 'sugar, water, shoe polish', ); } sub s1 { # Read family secrets } sub s2 { #write family secret #call s1 } }


        TGI says moo