in reply to Re^5: RFC: Perl meta programming
in thread RFC: Perl meta programming
So if get the point, there is no easy way to test whether something can be treated as a hash ref (for instance) short of trying to do so. So the best way to test is to try and catch any exceptions that indicate failure.The alternative is that you use B to ask whether you *really* has a hash reference and overload to ask whether your object of some other type has %{} dereferencing magic. This exposes you to more magic than you want to have to know about.
use B 'svref_2object'; use overload (); sub bad_idea { my $thing = shift @_; local $@; # Protect against $@ stomping return # Is a real hash reference eval { svref_2object( $thing )->isa( 'B::HV' ) } # Pretends to be a hash reference or ( overload::Overloaded( $thing ) and overload::Method( $thing, '%{}' ) ); }
⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: RFC: Perl meta programming
by druud (Sexton) on Sep 13, 2009 at 11:20 UTC |