Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: how to declare a local *subname=sub{}?

by perl-diddler (Chaplain)
on Oct 30, 2016 at 22:59 UTC ( [id://1174973]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^3: how to declare a local *subname=sub{}?
by LanX (Saint) on Oct 30, 2016 at 23:12 UTC
    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...
Re^3: how to declare a local *subname=sub{}?
by LanX (Saint) on Oct 30, 2016 at 23:23 UTC
    > 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?
        You are accessing a global symbol of the stash

        you don't need to declare hash elements of an existing hash either, do you?

        update

        You seem to think that local is declaring variables.

        That's totally wrong! Please read documentation and provided links.

        meta:

        This whole discussion is too XY for my taste, I'm out now. :)

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

Re^3: how to declare a local *subname=sub{}?
by LanX (Saint) on Oct 30, 2016 at 23:05 UTC
    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.

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

        Because you still haven't read any of the doc links provided.

        local is a totally different beast compared to my and our .

        > Now I think I see one source of your frustration.

        well "frustration" is exaggerated ... I'll just stop talking to you until you do your homework. :-)

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

Re^3: how to declare a local *subname=sub{}?
by LanX (Saint) on Oct 30, 2016 at 23:37 UTC
    > 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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174973]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-24 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found