Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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.

Replies are listed 'Best First'.
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/ IO/ IO/ File/Spec/ File/ IO/ File/Spec/

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2016-10-27 16:22 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (366 votes). Check out past polls.