in reply to Re: goto &sub and local question
in thread goto &sub and local question

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>

Replies are listed 'Best First'.
Re^3: goto &sub and local question
by almut (Canon) on Nov 24, 2008 at 09:41 UTC
    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>
Re^3: goto &sub and local question
by Bloodnok (Vicar) on Nov 24, 2008 at 09:46 UTC
    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 :-))
Re^3: goto &sub and local question
by SuicideJunkie (Vicar) on Nov 24, 2008 at 17:39 UTC
    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

        No need to introduce a new block. You can just add BEGIN in front of the existing curlies.

        BEGIN { my %familySecrets = ( ... ); sub s1 { ... } sub s2 { ... } }

        By the way, BEGIN is probably not needed in the main script either, but there's no harm in using it.