Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Copy a builtin sub to a different name and then override

by beech (Parson)
on Jun 01, 2018 at 22:31 UTC ( [id://1215676]=note: print w/replies, xml ) Need Help??


in reply to Copy a builtin sub to a different name and then override

Hi,

Which version of perl do you have?

See CORE and ex::override

#!/usr/bin/perl -- use strict; use warnings; use diagnostics; BEGIN { my $sleepcounter = 0; *CORE::GLOBAL::hex = sub { print qq{hex(@_)\n}; # goto &CORE::hex; ## FAIL CORE::hex(@_); }; *CORE::GLOBAL::sleep = sub(;@) { print qq{sleep(@_)\n}; $sleepcounter += $_[0]; CORE::sleep(@_); }; END { print "total sleep( $sleepcounter )\n"; } } print "\$]=$] \$^V=$^V\n"; sleep 1; print hex("0x50"),"\n"; sleep 2; print hex("0x50"),"\n"; __END__ $ perl core-global-sleep-override.pl $]=5.016001 $^V=v5.16.1 hex(0x50) 1 hex(0x50) 1 total sleep( 0 ) $ perl core-global-sleep-override.pl $]=5.020003 $^V=v5.20.3 sleep(1) hex(0x50) 1 sleep(2) hex(0x50) 1 total sleep( 3 )

Replies are listed 'Best First'.
Re^2: Copy a builtin sub to a different name and then override
by LanX (Saint) on Jun 02, 2018 at 00:52 UTC
    This works for me under 5.016003 even without the correct prototype:

    use strict; use warnings; warn "Version $]"; warn "Prototype", prototype "CORE::sleep"; my $sleepcounter; BEGIN { *CORE::GLOBAL::sleep = sub { warn "start wrapper"; $sleepcounter += $_[0]; CORE::sleep(@_); warn "stop wrapper"; }; } sleep 3; warn "total sleep( $sleepcounter )"
    C:/Perl_64/bin\perl.exe d:/Users/lanx/pm/core_sleep.pl Version 5.016003 at d:/Users/lanx/pm/core_sleep.pl line 4. Prototype;$ at d:/Users/lanx/pm/core_sleep.pl line 5. start wrapper at d:/Users/lanx/pm/core_sleep.pl line 13. stop wrapper at d:/Users/lanx/pm/core_sleep.pl line 16. total sleep( 3 ) at d:/Users/lanx/pm/core_sleep.pl line 26.

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

Re^2: Copy a builtin sub to a different name and then override
by bliako (Monsignor) on Jun 01, 2018 at 23:18 UTC

    that worked!

    #!/usr/bin/env perl use strict; use warnings; my $total_sleep_time = 0; BEGIN { *CORE::GLOBAL::sleep = sub(;@) { $total_sleep_time+=$_[0]; CORE::sleep +($_[0]) } } # use this module and the other # ... print "\$]=$] \$^V=$^V\n"; sleep(2); print "total sleep is $total_sleep_time\n";
    $]=5.026002       $^V=v5.26.2
    total sleep is 2
    
    thanks
      sleep sleeps integer seconds and returns the integer number of seconds actually slept.
      #!/usr/bin/perl use strict; use warnings; my $total_sleep_time = 0; BEGIN { *CORE::GLOBAL::sleep = sub(@) { $total_sleep_time += CORE::sleep($_[0]); } } print "\$]=$] \$^V=$^V\n"; sleep(2); sleep(1); sleep(1.3); print "total sleep is $total_sleep_time\n"; $ time -p ./sleep.pl $]=5.024001 $^V=v5.24.1 total sleep is 4 real 4.00 user 0.00 sys 0.00
        > returns the integer number of seconds actually slept.

        Excellent point, because any replacement has to mimic this behavior.

        Afaik all implementations shown so far are broken in this respect.

        (Yours too btw :)

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

      Very nice.

      Overriding CORE::GLOBAL::sleep but calling CORE::sleep makes total sense when you see it (as it avoids the recursion).

      This is of course what Lanx has been trying to say all along :-)

        You seem to think you've counted coup here but bliako said LanX's final suggestion did in fact solve the issue even if the original suggestion wasn't on point because it jumped into the semantics of the sample code too literally. Pointless drama over silly beef just gets in the way of communication and solving problems.

        Bla bla
Re^2: Copy a builtin sub to a different name and then override ( goto &SUB )
by LanX (Saint) on Jun 04, 2018 at 11:55 UTC
    Hi Beech,

    > # goto &CORE::hex; ## FAIL

    I can't reproduce any problems with goto &SUB here.

    Neither with hex nor with sleep .

    How does it "fail" for you?

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

    update

    use strict; use warnings; warn "Version $]"; warn "Prototype", prototype "CORE::sleep"; my $sleepcounter; BEGIN { *CORE::GLOBAL::sleep = sub { warn "start wrapper ($_[0])"; $sleepcounter += int $_[0]; goto &CORE::sleep; }; } warn "return: ", sleep 2.3; warn "total sleep( $sleepcounter )";
    Version 5.016003 at d:/Users/lanx/pm/core_sleep.pl line 4. Prototype;$ at d:/Users/lanx/pm/core_sleep.pl line 5. start wrapper (2.3) at d:/Users/lanx/pm/core_sleep.pl line 15. return: 2 at d:/Users/lanx/pm/core_sleep.pl line 23. total sleep( 2 ) at d:/Users/lanx/pm/core_sleep.pl line 25.

      # goto &CORE::hex; ## FAIL This works for me under 5.016003 even without the correct prototype:

      Hi,

      It turns out it works as designed, it merely returns different from CORE::hex(@_) , because of the prototype . I only checked for prototype on "hex" because it worked on "sleep", I forgot I preload Time::HiRes, I should have checked CORE::hex

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1215676]
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: (6)
As of 2024-04-19 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found