Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Is there any way to override "file test operator"?

by kejohm (Hermit)
on Aug 09, 2011 at 02:38 UTC ( #919362=note: print w/ replies, xml ) Need Help??


in reply to Is there any way to override "file test operator"?

You can overload the operator with the overload pragma, although this only works if your package is being used as a class. Also only works on Perl v5.12 or later. Example:

package MyFileTest; use overload ( '-X' => \&myfiletest, ); sub new { ... } ... package main; my $file = MyFileTest->new($filename); if(-f $file){ ... }

See also the filetest pragma.


Comment on Re: Is there any way to override "file test operator"?
Download Code
Re^2: Is there any way to override "file test operator"?
by Anonymous Monk on Aug 09, 2011 at 07:55 UTC
    Hmm, then this should also work, but it doesn't
    #!/usr/bin/perl -- BEGIN { use IO::File; package IO::Handle; use overload '-X' => sub { warn 'abracadabra'; stat@_; }; *IO::Handle::stat = sub { warn "hi walter"; stat(@_); }; } open my($fh), '<', __FILE__; print '-e ', -e $fh, "\n"; print '-f ', -f $fh, "\n"; print '-d ', -d $fh, "\n"; print 'stat ', stat $fh, "\n"; print $fh, "\n"; close $fh; print "$_\n" for keys %INC; __END__ -e 1 -f 1 -d stat 00332061000745131287651313128765131312876169 GLOB(0x3f8adc) warnings/register.pm XSLoader.pm IO/Handle.pm SelectSaver.pm IO/Seekable.pm warnings.pm Fcntl.pm IO.pm Symbol.pm Carp.pm File/Spec/Unix.pm strict.pm Exporter.pm vars.pm File/Spec.pm IO/File.pm overload.pm File/Spec/Win32.pm

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-07-24 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls