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

Since you are aliasing your symbols with globs you get circular calls. *

Try code refs instead

my $actual_sleep = \&CORE::GLOBAL::sleep; # and later $actual_sleep->($_[0])

Untested!

update

That's the normal way to install a wrapper, but I'm not sure if that's recommended with builtins. ..

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

*) see demo here Re^5: Copy a builtin sub to a different name and then override

Replies are listed 'Best First'.
Re^2: Copy a builtin sub to a different name and then override
by bliako (Abbot) on Jun 01, 2018 at 23:10 UTC

    still the same problem with this:

    #!/usr/bin/env perl use strict; use warnings; my $total_sleep_time = 0; BEGIN { my $actual_sleep = \&CORE::GLOBAL::sleep; *CORE::GLOBAL::sleep = sub { $total_sleep_time+=$_[0]; $actual_sleep-> +($_[0]) } } # use this module and the other # ... sleep(2); print "total sleep is $total_sleep_time\n";
    thanks
      > still the same problem with this:

      > ...

      > my $actual_sleep = \&CORE::GLOBAL::sleep;

      This should solve it in a robust manner:

      my $actual_sleep = exists &CORE::GLOBAL::sleep # already overridden + ? ? \&CORE::GLOBAL::sleep : \&CORE::sleep

      Point is you are changing sleep globally, but some other modules might be trying to do the same thing already. (well ...)

      If you install your override later, you'll be playing safe.

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

      update

      even more robust with defined instead of exists , see Haarg's comment in this thread for details.

      > still the same problem with this:

      Really?

      You still have Deep recursion on anonymous subroutine ?

      As I said that's the normal way to install a wrapper, but in the case of builtins it seems risky.

      calling CORE::sleep() should be the recommended way.

      ... but I seem to remember reading that overriding sleep used to be problematic.(?)

      I'll look tomorrow into it.

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

        No that's fine, your later suggestion CORE::sleep() worked great! thanks

          A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.