Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Tie subroutine to a built-in function call?

by stevieb (Canon)
on Aug 23, 2016 at 17:25 UTC ( [id://1170242]=note: print w/replies, xml ) Need Help??


in reply to Tie subroutine to a built-in function call?

Here's an example. You assign a reference to your own subroutine to the CORE::GLOBAL namespace for the function in question within a BEGIN block. In this case, the custom open() will only accept the 3-arg form of open.

use warnings; use strict; BEGIN { *CORE::GLOBAL::open = sub (*;$@) { print "in my open...\n"; # the following line calls the built-in open() function # directly with the params your sub received CORE::open($_[0], $_[1], $_[2]); } }; open my $fh, '<', 'blah' or die $!; print <$fh>;

Output:

in my open... file contents

Replies are listed 'Best First'.
Re^2: Tie subroutine to a built-in function call?
by bcarroll (Pilgrim) on Apr 26, 2017 at 20:22 UTC
    This is exactly what I was looking for!

Log In?
Username:
Password:

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

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

    No recent polls found