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

Re: Overriding "-s", "-f" and other -X functions

by Athanasius (Archbishop)
on Jul 07, 2018 at 07:53 UTC ( [id://1218083]=note: print w/replies, xml ) Need Help??


in reply to Overriding "-s", "-f" and other -X functions

Hello Doctrin,

I haven’t tried it, but here is an excerpt from the documentation for the core overload pragma, under “Overloadable Operations”:

  • File tests

    The key '-X' is used to specify a subroutine to handle all the filetest operators (-f, -x, and so on: see -X for the full list); it is not possible to overload any filetest operator individually. To distinguish them, the letter following the '-' is passed as the second argument (that is, in the slot that for binary operators is used to pass the second operand).

    Calling an overloaded filetest operator does not affect the stat value associated with the special filehandle _. It still refers to the result of the last stat, lstat or unoverloaded filetest.

    This overload was introduced in Perl 5.12.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Overriding "-s", "-f" and other -X functions
by haukex (Archbishop) on Jul 07, 2018 at 08:00 UTC

    Note that this only works if the thing that you're calling the -X on is an object of the overloaded class.

    use warnings; use strict; { package Foo; use Data::Dump; use overload fallback=>0, '-X' => sub { dd @_; return 1 }; } my $foo = bless {}, 'Foo'; print -e $foo ? 'yes' : 'no', "\n"; my $bar = "doesnotexist"; print -e $bar ? 'yes' : 'no', "\n"; __END__ (bless({}, "Foo"), "e", "") yes no

Log In?
Username:
Password:

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

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

    No recent polls found