in reply to how to declare a local *subname=sub{}?

> So why does the "local *bsub" have no effect and why does the inclusion of "local *asub" prevent it from being callable in "stuff", below?

local is a runtime command which restores at the end of the scope, given by the block markers here.

And the declaration of subs is a compile-time effect.

Obviously you are calling stuff after the restoration.

HTH your code and intentions are not very clear, this smells like a XY problem

Update:

If you want to manipulate (so called monkey patching ) which routines are called within a sub, you have to manipulate the symbol table right before calling the surrounding sub.

That is

stuff(); #original { local *asub = $code_ref; stuff(); # new behavior local (sic) only } stuff(); #original

update

But you don't need local if you don't need it to be restored.

I'd rather suspect you might want to look into closures and how to call lexical coderefs

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: how to declare a local *subname=sub{}?
by perl-diddler (Chaplain) on Oct 30, 2016 at 22:59 UTC
    Possibly because 1 question expanded into 2?

    1) Why does including the "local *asub" prevent it from being called in "stuff"? I.e. if you uncomment the "local *asub" line, it can't be called in "stuff" -- this is the main question.

    2) Why does including the "local *bsub" (of an interior function -- different animal) appear to be ignored in "sub stuff".

    In playing w/the test case, I noted that I could put the "local *'subname', outside of an interior sub where it is implemented -- but that doing so was "ignored", from-the-standpoint of trying to call it from "outside" its containing sub (i.e. the attempted call to &bsub in "stuff()". That curiosity created question#2...

    Note, I wouldn't expect calling an interior function (as stuff tries to call bsub) to necessarily work -- just that I found that trying to pre-declare it with local didn't even change the error message when "stuff" tried to call it. I thought that was strange.

    Main Q is why or how might I _declare_ asub, or is
    *asub = sub{...} equivalent to
    sub asub {}?

    It's that I'm not used to seeing an undeclared name on the LeftHandSide without it being flagged by "strict" as it not being declared.

      2) already answered, sub declarations are done at compile time, any local commands prior in text have no effect, they are run time, ie happen later.

      Additionally you are explicitly setting the content of *bsub whenever you call the internal function.

      update

      Please read perlsub#Temporary-Values-via-local() and following

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        What was already answered? ... um... since the issues w/"bsub" were only a curiosity that grew out of implementing a test case for the "asub" case, let's just ignore the 'bsub' case declared within a sub. That seems to have generated way too much confusion for what was a "side", curiosity...
      > Main Q is why or how might I declare asub, or is  *asub = sub{...} equivalent to  sub asub {}

      Almost equivalent, the prior has a runtime effect the latter only compile time.

      See perlmod for phases of execution and do some experiments checking the availability of subs.

      But I'm repeating myself now. ...

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Ok, got that... but why doesn't the 'lvalue' "asub" need to be declared under strict?
      1) because local restores the (here undef) value of the code slot of *asub at the end of the scope, which is reached before stuff() is called.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Now I think I see one source of your frustration.

        The call to "stuff" is within the scope of the "local *asub", which has a value assigned to "asub" that is also within the same scope as "sub stuff".

        i.e. the "undef" value preserved outside of the scope of the "local" ends past "sub stuff".

        That's why it is a curiosity. I couldn't see (and still don't) why adding the "local" statement (who's scope includes "sub stuff") should make a difference.

      > Possibly because 1 question expanded into 2?

      Please see  How (Not) To Ask A Question

      Though my answers include many valuable informations, this thread will be of no help for the majority, because your intentions are unclear and your code mangled

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!