Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by bliako (Monsignor)
on Jun 02, 2018 at 14:33 UTC ( [id://1215738]=note: print w/replies, xml ) Need Help??


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

thanks Bill, but i need it to affect ALL imported modules too

  • Comment on Re^2: Copy a builtin sub to a different name and then override

Replies are listed 'Best First'.
Re^3: Copy a builtin sub to a different name and then override
by BillKSmith (Monsignor) on Jun 02, 2018 at 18:07 UTC
    As I now understand your problem, we still do not have a solution. You use one (or more) existing module(s) which call sleep(). You wish to include the module sleep time(s) in your total. I devised the following test:
    C:\Users\Bill\forums\monks>type Existing_module.pm use strict; use warnings; package Existing_Module; sub Do_Something { print "Doing something.\n"; sleep(3); } 1 C:\Users\Bill\forums\monks>type bliako.t use strict; use warnings; use lib '.'; use Test::More tests => 2; use Existing_Module; our $total_sleep_time; BEGIN { *actual_sleep = *CORE::sleep; *CORE::GLOBAL::sleep = sub (;$) { $total_sleep_time+=$_[0]; actual_sleep($_[0]) } } sleep(2); is($total_sleep_time, 2, 'Local'); Existing_Module::Do_Something(); # Sleeps for 3 sec. is($total_sleep_time, 2+3, 'Module'); C:\Users\Bill\forums\monks>perl bliako.t 1..2 ok 1 - Local Doing something. not ok 2 - Module # Failed test 'Module' # at bliako.t line 21. # got: '2' # expected: '5' # Looks like you failed 1 test of 2.

    Several proposed solutions (including my previous post) can pass the first test. I have not found any that can pass the second. I have learned not to say that anything is impossible, but it does seem that this is intended to be.

    Bill
      Hi Bill,

      That's most likely a compile time effect.

      What happens if you override sleep before using Existing_Module ?

      Just move the use after the BEGIN block.

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

      update

      To demonstrate my point:

      The first sleep is compiled before hitting the BEGIN block, only the second after is compiled to bind to the override.

      Keep in mind that use is executed at compile time.

      use strict; use warnings; warn "Version $]"; warn "Prototype", prototype "CORE::sleep"; my $sleepcounter; sleep 2; BEGIN { my $old = \&CORE::sleep; *CORE::GLOBAL::sleep = sub { warn "start wrapper $_[0]"; $sleepcounter += $_[0]; $old->(@_); }; } sleep 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 3 at d:/Users/lanx/pm/core_sleep.pl line 16. total sleep( 3 ) at d:/Users/lanx/pm/core_sleep.pl line 26.

        Perfect! I hope the OP learned as much as I have from this thread.
        Bill

      Yep I agree with LanX, you must override/alias your sleep() BEFORE use statements for any modules which will use it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found