Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Can I ask Perl if an object will stringify?

by LanX (Saint)
on Mar 29, 2015 at 22:10 UTC ( [id://1121736]=note: print w/replies, xml ) Need Help??


in reply to Can I ask Perl if an object will stringify?

If I were you I'd reject simple refs and allow all objects without testing.

Don't start fiddling with internals.

In any case you can only do runtime checks in Perl.

So if you get the default stringification from an object (IIRC that's what ref returns) you are free to warn, that something looks strange.

Don't try to overengineer your function!

Sometimes "it's easier to ask for forgiveness than it is to ask for permission. " (stolen from Python ;)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)

PS: Je suis Charlie!

  • Comment on Re: Can I ask Perl if an object will stringify?

Replies are listed 'Best First'.
Re^2: Can I ask Perl if an object will stringify?
by Your Mother (Archbishop) on Mar 30, 2015 at 21:52 UTC

    And to add, it’s terrible, terrible advice in legal matters. And in personal matters… seems like terrible advice too. :P (Some day I’ll tell a directly related multi-million dollar lawsuit work story.)

Re^2: Can I ask Perl if an object will stringify?
by RonW (Parson) on Mar 30, 2015 at 21:00 UTC
    "it's easier to ask for forgiveness than it is to ask for permission. " (stolen from Python ;)

    I guess Python stole it from Admiral Hopper, who doubtless stole it from someone else.

      Yes you're probably right...

      ...I beg for forgiveness! ;-P

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

Re^2: Can I ask Perl if an object will stringify?
by Dumu (Monk) on Apr 01, 2015 at 09:45 UTC
    Tag Rolf

    What is 'stolen from Python'? Do you mean the phrase "it's easier to ask for forgiveness than it is to ask for permission"?
    That's a quotation from the great Rear-Admiral Grace Hopper.

    Grace Hopper - Wikiquote
      Moin Dumu,

      already admitted that Grace was earlier but ...

      EAFP is a very common coding style in Python, such that it became part of their glossary.

      Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

      IMHO they rely on it much more than Perl hackers do. I'm often surprised how often try/catch constructs are used there to handle fuzzy situations.

      "Stolen" because we don't catch any exceptions in my suggestion.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

        Thanks Rolf.

        I deliberately know very litte about Python apart from that it doesn't use semicola, so I miss out on what's good about it as well as everything else!
        Of course, Perl has plenty of programming philosophy, but I never imagined that Python did.
        Rolf...

        So you did!

        I think / trust / hope that Adm. Hopper would be pleased that her maxim has become an acronym and has been applied directly to the craft she did so much to advance. (I don't think she used it in that way).
Re^2: Can I ask Perl if an object will stringify?
by haukex (Archbishop) on May 25, 2015 at 10:17 UTC

    Hi Rolf,

    Thanks for your thoughts, and I agree that my solution is probably in the gray zone of being a little bit overengineered ;-) Instead of my current implementation (I've posted it below) getting any more complex, I think I would remove the check entirely, or at least simplify it as you suggest. This particular module already spends a significant amount of code on trying to be helpful to its user - as far as I know, that's only me right now :-)

    Thanks and Regards,
    -- Hauke D

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found