Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

SOLVED: CORE::stat() doesn't appear to be the same as stat

by Bloodnok (Vicar)
on Sep 10, 2018 at 14:01 UTC ( #1222046=perlquestion: print w/replies, xml ) Need Help??

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

Hey brethren,

Long time, no speak ... I've been away on Python (;-(() & Ruby (:-))) sabbaticals.

Anyways, my question is, I think, a simple one - why does running the command

perl -e 'print \&stat, \&CORE::stat; package CORE; print \&stat'

return

CODE(0x563599d2b1e0)CODE(0x563599d547d0)CODE(0x563599d547d0)

i.e. given that stat() is ostensibly a part of the CORE package (as partially shown above), what are the/any sound reason(s) for the differences between them (\&stat & \&CORE::stat) (using perl 5.26.1 on Ubuntu 18.04) ??

Many thanx in advance for your insights.

Very many thanx for your replies - so the upshot is RTFM (but more closely).

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

Replies are listed 'Best First'.
Re: CORE::stat() doesn't appear to be the same as stat
by Lotus1 (Vicar) on Sep 10, 2018 at 14:41 UTC

    Welcome back Bloodnok.

    I noticed a couple of things in the CORE documentation:

    Calling with ampersand syntax and through references does not work for the following functions, as they have special syntax that cannot always be translated into a simple list (e.g., eof vs eof()):

    chdir, chomp, chop, defined, delete, eof, exec, exists, lstat, split, stat, system, truncate, unlink

    I'm still learning about this but my understanding is that you can override things in Perl but calling the CORE version is always the CORE version. So the function is a reference that starts off pointing to the CORE version. You can change glob() for example but CORE::glob won't change. This means that glob() is a different reference that starts out pointing to CORE::glob. References to them will always be different even when they are pointing to the same code since glob() refers to CORE::glob().

    Edit: The second thing I noticed was this in the documentation:

    For all Perl keywords, a CORE:: prefix will force the built-in function to be used, even if it has been overridden or would normally require the feature pragma. Despite appearances, this has nothing to do with the CORE package, but is part of Perl's syntax.

    I'm not sure if this supports or contradicts my understanding.

Re: CORE::stat() doesn't appear to be the same as stat
by Eily (Monsignor) on Sep 10, 2018 at 14:52 UTC

    As explained by Lotus1 &stat() isn't the same thing as stat(). &stat actually is the function in the *main::stat glob, which by default does not exist:

    use v5.14; use strict; use warnings; open my $fh, '>', "tmp_1222046.txt"; say 'stat($fh): ', join " ", stat($fh); say '*::stat{CODE} is ', *::stat{CODE} // 'undef'; say '$sub = \&stat;'; my $sub = \&stat; say '$sub: ', $sub; say '*::stat{CODE} is ', *::stat{CODE} // 'undef'; say 'stat($fh): ', join " ", stat($fh); say '$sub->($fh): ', join " ", $sub->($fh);
    stat($fh): 0 0 33206 1 0 0 0 0 1536590875 1536590875 1536590555 *::stat{CODE} is undef $sub = \&stat; $sub: CODE(0x340be0) *::stat{CODE} is CODE(0x340be0) stat($fh): 0 0 33206 1 0 0 0 0 1536590875 1536590875 1536590555 Undefined subroutine &main::stat called line 18.
    So accessing \&stat actually creates an entry in the symbols table, which is different from the function called with stat()

Re: CORE::stat() doesn't appear to be the same as stat
by ikegami (Pope) on Sep 10, 2018 at 22:50 UTC

    You get two difference addresses because you take create references to two different subs, &main::stat and &CORE::stat.


    First, you take the address of &main::stat. It's never defined.

    $ perl -e'my $sub = \&stat; $sub->("a")' Undefined subroutine &main::stat called at -e line 1.

    Then, you take the address of &CORE::stat twice. The subs in CORE:: are automatically-generated wrappers for the operators of the same name, so you get a wrapper to the stat operator.

    $ perl -e'my $sub = \&CORE::stat; $sub->("a")' &CORE::stat cannot be called directly at -e line 1.

    Well, it would be if stat had a calling convention that could be duplicated by subroutine prototypes. Instead, it's a wrapper for an error message.

Re: CORE::stat() doesn't appear to be the same as stat
by Anonymous Monk on Sep 10, 2018 at 17:00 UTC
    So, you have three addresses for what ought to be the same function. One of two things is true, either there are three functions or more likely, there are three pointers to the same function. I could be wrong but it seems silly to assume there would be three versions of stat() so my best guess is that you're taking the address of 3 pointers that all point at the same function. Of course, I don't actually know, Larry may very well have good reasons for having 3 versions of stat, all different, just to confuse us monks ;^)
      CODE(0x563599d2b1e0) CODE(0x563599d547d0) CODE(0x563599d547d0)
      either there are three functions or more likely, there are three pointers to the same function.

      Sorry, but if you take a look at the addresses above, you'll see neither is true.

      I could be wrong ... Of course, I don't actually know

      gee thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1222046]
Front-paged by Eily
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2020-10-24 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (242 votes). Check out past polls.

    Notices?