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

Re: RFC: Perl meta programming

by chromatic (Archbishop)
on Oct 19, 2006 at 21:09 UTC ( #579466=note: print w/ replies, xml ) Need Help??


in reply to RFC: Perl meta programming

Using ref is almost always wrong, especially for objects. I almost can't think of a case where it's right -- but serialization code might need to use it.


Comment on Re: RFC: Perl meta programming
Re^2: RFC: Perl meta programming
by TGI (Vicar) on Oct 19, 2006 at 21:29 UTC

    Please expand on this observation or point to some resources that can shed some light on your reasoning?


    TGI says moo

      ref gives you a class name. It's easy to fool; bless something into the wrong class name (or worse, a class name such as SCALAR. It's also easy to get wrong -- why do you have to know if something is a hash reference explicitly if the important thing is that you can dereference it as a hash (as in, it's a tied hash or has hash overloading)?

      ref ties you to a very specific representation of data, and it's extremely fragile. Fragile code breaks easily. It's a bad thing.

      With Scalar::Util's reftype() for the times when I really need to break encapsulation, I can't think of a good reason to keep ref in the language itself.

        Some related discussion in this FWP thread.

        We're building the house of the future together.
Re^2: RFC: Perl meta programming
by bennymack (Pilgrim) on Oct 19, 2006 at 21:31 UTC

    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!

      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!

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2014-12-22 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (121 votes), past polls