Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

CORE::GLOBAL magic?

by morgon (Priest)
on Jun 02, 2018 at 01:53 UTC ( [id://1215713]=perlquestion: print w/replies, xml ) Need Help??

morgon has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

can someone explain to me what is happening in CORE::GLOBAL for mere curiosity...

If I do e.g.

perl -e 'print \&CORE::GLOBAL::sleep' CODE(0x563c8adc73e0)
it seems CORE::GLOBAL::sleep is a code-ref, yet
perl -e '&CORE::GLOBAL::sleep(1)' Undefined subroutine &CORE::GLOBAL::sleep called at -e line 1.
I cannot call it directly. Even weirder is
perl -e 'print \&CORE::GLOBAL::hubbabubba' CODE(0x561b6472f3e0)/
so it seems every arbitary code-slot in CORE::GLOBAL is (virtually) filled with the same code-ref that probably points to some interception logic but I am surprised that there are code-refs that cannot be called...

I am aware that CORE::GLOBAL is for allowing built-ins to be redefined but this surprises me...

Can anybody with some knowlege of the internals shed some light on this?

Many thanks!

Replies are listed 'Best First'.
Re: CORE::GLOBAL magic?
by NetWallah (Canon) on Jun 02, 2018 at 05:07 UTC
    Here is some speculation:
    $ perl -e '$x=\&fff;print $x->(2)' Undefined subroutine &main::fff called at -e line 1.
    Looks like when you reference \&<Something>, it pre-declares a coderef/subref named <Something>

    However, since the Name <Something> is declared without any content, it will complain when called.

    If you happen to provide the content of the name <Something>, it works:

    $ perl -e '$x=\&fff; sub fff{44};print $x->(2)' 44

                    Memory fault   --   brain fried

      I believe that it is close to the notion of "auto-vivification." An object is created within the interpreter to reflect the fact that you have referred-to this thing, and you see the (dummy) object that it made, but it has no actual definition and therefore no content. CORE::GLOBAL like all of CORE is a pseudo-namespace that exists because a namespace is needed not because it is actually real in the sense of other namespaces which appear in source code.
        Yes auto-vivification is close, the analogy becomes clearer since there is a hash like syntax to access the CODE slot of a typeglob. (like *CORE::sleep{CODE} )

        anyway the important question is to know if a real coderef exists.

        That's why you can check this with exists &Func; (sic)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re: CORE::GLOBAL magic?
by kcott (Archbishop) on Jun 02, 2018 at 02:39 UTC

    G'day morgon,

    I believe you're making incorrect assumptions about CORE::GLOBAL. Here's a repeat of your examples along with equivalents not involving CORE::GLOBAL.

    $ perl -wle 'print \&CORE::GLOBAL::sleep' CODE(0x224fcb8) $ perl -wle 'print \&CORE::GLOBAL::hubbabubba' CODE(0x1d4acb8) $ perl -wle 'print \&hubbabubba' CODE(0x255bcb8) $ perl -wle '&CORE::GLOBAL::sleep(1)' Undefined subroutine &CORE::GLOBAL::sleep called at -e line 1. $ perl -wle '&CORE::GLOBAL::hubbabubba(1)' Undefined subroutine &CORE::GLOBAL::hubbabubba called at -e line 1. $ perl -wle '&hubbabubba(1)' Undefined subroutine &main::hubbabubba called at -e line 1.

    As you can see, the outcomes are the same with or without CORE::GLOBAL.

    I can't really comment on internals: I'm not sufficiently versed in that area. There are, however, plenty of monks who can help with that.

    — Ken

Re: CORE::GLOBAL magic?
by Veltro (Hermit) on Jun 02, 2018 at 13:56 UTC
    -

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found