Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: RFC: Perl meta programming

by bennymack (Pilgrim)
on Oct 19, 2006 at 21:31 UTC ( #579472=note: print w/ replies, xml ) Need Help??


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

Thanks for the reply chromatic. I agree that isa() is a better way to validate object references.

I am confused in regards to better ways to validate references however. What's a better way to validate an array ref than "if( 'ARRAY' eq ref $foo )"? Thanks!


Comment on Re^2: RFC: Perl meta programming
Re^3: RFC: Perl meta programming
by Sidhekin (Priest) on Oct 19, 2006 at 21:44 UTC

    What's a better way to validate an array ref than "if( 'ARRAY' eq ref $foo )"? Thanks!

    There are caveats whichever way you do it, but currently I tend to default to just derefing it within an eval block:

    if (eval{ use strict 'refs'; @$foo; 1 }) { # it is, or pretends convincingly, to be an array ref }

    Caveat? Right. In the presence of ties or overloading, there may be side effects to derefing it. Pick your poison / buyer beware / etc.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re^3: RFC: Perl meta programming
by TGI (Vicar) on Oct 20, 2006 at 03:48 UTC

    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

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-08-29 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (277 votes), past polls