Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by pme (Prior)
on Jun 02, 2018 at 09:23 UTC ( #1215723=note: print w/replies, xml ) Need Help??


in reply to Re^2: Copy a builtin sub to a different name and then override
in thread Copy a builtin sub to a different name and then override

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

Replies are listed 'Best First'.
Re^4: Copy a builtin sub to a different name and then override (API compatibility)
by LanX (Archbishop) on Jun 03, 2018 at 12:14 UTC
    > 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

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

      That depends on scope. If I write my watchdog.pl program which runs on my system, I might want a sleep override which returns the number of green bubbles appearing in my room at night whilst being asleep (ghosts, bengal fires), or a tupel having [ $seconds_slept, @things_missed ] monitoring the chatterbox, or anything else.

      Things are different if I want to convince all perl programmers to use my sleep subroutine as a drop-in replacement for the builtin.

      Update: in the context of this thread, you are right of course (make all imported modules use the overriden sleep()), so this comment is rather pointless. Well, at least it carries an idea for yet another silly program.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2019-11-17 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (86 votes). Check out past polls.

    Notices?