NAME
overload::open - Hooks the native open function
SYNOPSIS
use overload::open;
my %opened_files;
sub my_callback { return if @_ != 2 && @_ != 3; $opened_files{$_[-1]}++ }
overload::open->prehook_open(\&my_callback);
open my $fh, '>', "foo.txt";
DESCRIPTION
This module hooks the native open()
and/or sysopen()
functions and passes the arguments first to callback you provide. It then calls the native open/sysopen.
It does this using the XS API and replacing the OP_OPEN/OP_SYSOPEN opcode's with an XS function. This function will call your provided sub, then once that returns it will run the original OP.
FEATURES
This function will work fine if you call open
or sysopen
inside the callback due to it detecting recursive calls and not calling the callback for recursive calls.
You are not allowed to pass XS subs as the callback because then this could result in a recursive loop. If you need to do this, wrap the XS function in a native Perl function.
METHODS
- prehook_open
-
use overload::open overload::open->prehook_open(\&my_sub)
Runs a hook before
open
by hookingOP_OPEN
. The provided sub reference will be passed the same arguments as open. - prehook_sysopen
-
use overload::open; overload::open->prehook_sysopen(\&my_sub)
Runs a hook before
sysopen
by hookingOP_SYSOPEN
. Passes the same arguments to the provided sub reference as provided to sysopen. - suppress_warnings
-
overload::open->suppress_warnings(1)
Suppress runtime warnings
AUTHOR
Samantha McVey <samantham@posteo.net>
LICENSE
This module is available under the same licences as perl, the Artistic license and the GPL.