in reply to Re^2: restore overridden XS methods
in thread restore overridden XS methods

local works if you use glob assignment syntax:

{ local *Time::HiRes::gettimeofday = sub { print 42; }; print Time::HiRes::gettimeofday(); };; Subroutine Time::HiRes::gettimeofday redefined at (eval 4) line 1, <ST +DIN> line 2. 42 1 print Time::HiRes::gettimeofday();; 1172754765 437500

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^4: restore overridden XS methods
by bart (Canon) on Mar 01, 2007 at 13:26 UTC
    Clever. The only problem I see is that if there are other variable types attached to the name Time::HiRes::gettimeofday, such as $Time::HiRes::gettimeofday or @Time::HiRes::gettimeofday, that those will be overridden too.

    I don't think it matters for this particular case — or even, in most cases.

      I'm not sure I understand what you mean?

      ## Assign appropriate values to 3 different slots in the glob *t = sub{ print 42 }; *t = \'fred'; *t = [ 1,2,3];; ## And print them print join "\n", t(), $t, "@t";; 42 1 fred 1 2 3 ## Locally override the code slot and print them--the others are untou +ched. { local *t = sub { print 'changed' }; print join "\n", t(), $t, "@t"; };; Subroutine main::t redefined at (eval 5) line 1, <STDIN> line 3. changed 1 fred 1 2 3 ## print them again having exited the block, ## and they all retain their original values print join "\n", t(), $t, "@t";; 42 1 fred 1 2 3

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Something has been changed, new magic in the localization of typeglobs. Because, even though I though get this pleasant result in 5.8.3, it's not at all what I get in 5.6.1:
        $\ = "\n"; *t = sub{ print 42 }; *t = \'fred'; *t = [ 1,2,3]; print $]; { local *t = sub { print 'changed' }; print join "\n", t(), $t, "@t"; };
        results for 5.6.1 in
        Possible unintended interpolation of @t in string at test.pl line 7. Use of uninitialized value in join or string at test.pl line 7. 5.006001 changed 1
        Thus: only the sub is set.

        Compare that to the result for 5.8.3:

        Possible unintended interpolation of @t in string at test.pl line 7. Subroutine main::t redefined at test.pl line 6. 5.008003 changed 1 fred 1 2 3