Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: RFC: Perl meta programming

by TGI (Vicar)
on Oct 20, 2006 at 03:48 UTC ( #579504=note: print w/ replies, xml ) Need Help??


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

The problem I have with isa() is that I either need to use the verbose UNIVERSAL::isa( $foo, 'Object::Foo' ) syntax, or check the return of ref() to be sure that I can do $foo->isa('Object::Foo') without dying.

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

After a bit of poking, Scalar::Util's  blessed() and reftype() look like pretty good options. I suppose I could just import isa(), as well.


TGI says moo


Comment on Re^3: RFC: Perl meta programming
Select or Download Code
Re^4: RFC: Perl meta programming
by chromatic (Archbishop) on Oct 20, 2006 at 09:17 UTC

    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.

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

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        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
Node Status?
node history
Node Type: note [id://579504]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2014-08-28 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls