Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^4: RFC: Perl meta programming

by chromatic (Archbishop)
on Oct 20, 2006 at 09:17 UTC ( [id://579537]=note: print w/replies, xml ) Need Help??


in reply to Re^3: RFC: Perl meta programming
in thread RFC: Perl meta programming

Beware.

The problem I have with isa() is that I either need to use the verbose UNIVERSAL::isa( $foo, 'Object::Foo' ) syntax...

Unfortunately, it's completely wrong. See Acme::UNIVERSAL::new if you think it's a good idea.

Wrapping every isa() check in an eval{} seems cumbersome, too.

Tough. If you care about getting the right answer, that's what you need to do. Yes, the syntax is a little clunky, but it will give you the right answer. Nothing else will.

I suppose I could just import isa(), as well.

Not if you care about getting the right answer.

Replies are listed 'Best First'.
Re^5: RFC: Perl meta programming
by TGI (Parson) on Oct 20, 2006 at 18:50 UTC

    There's some real food for thought in your replies, thanks.

    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.

    If I want to verify membership in a class, I'd need to do something like:

    # Option 1. if ( eval { $foo->isa('Some::Class') } ) { # Do stuff with Some::Class } # or Option 2. use Scalar::Util; if ( blessed $foo and $foo->isa('Some::Class') ) { # Do stuff with Some::Class }

    Are there any big ugly problems with either option? I've tested both and they seem OK, but there's no point trying to learn a new habbit, if it's a bad one!


    TGI says moo

      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, '%{}' ) ); }

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        perl -wle 'sub x{ return eval{die} or 42 } print x'

      If I want to verify membership in a class, I'd need to do something like: eval { $foo->isa( 'Some::Class' ) }
      Right. That ->isa call might throw an exception because $foo isn't an object, it might resolve to UNIVERSAL::isa, or $foo might have a different ->isa method. Using UNIVERSAL::isa on all objects and classes breaks objects and classes which implement their own ->isa methods. That's much of the interesting code being written these days so you'd best not lock yourself off from the future by hardcoding UNIVERSAL::isa in.

      Here's one additional note. Try to remember to localize $@ when you're using eval. It protects your caller's view of $@ from your eval blocks.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Those are the best two options I know; they're also in the documentation (somewhere) as of a few months ago.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found