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

Re^2: Truthfulness of references

by njcodewarrior (Pilgrim)
on Mar 13, 2007 at 11:08 UTC ( [id://604507]=note: print w/replies, xml ) Need Help??


in reply to Re: Truthfulness of references
in thread Truthfulness of references

Thanks GrandFather. That's what I thought the test was actually doing

So what's a good way to test for truth here?
The reason for the post is that I've written a few subroutines that either return a reference to a complex data structure OR filter entries out of the previously created data structure. The problem I noticed was that if I ended up filtering out all of the entries and returning a reference to the filtered data structure, my test for truth returned unexpected results ( ie: it returned true eventhough the data structure was empty.

3 follow up questions:
1. Should I be returning references from these types of sub-routines?
2. If so, what's the best way to return them?
3. What's a good way to test for truthfulness:

if ( %$ref ) { ...do something }

or...

if ( %$ref && ref $ref eq 'HASH' ) { ...do something }

Thanks again for your help.
njcodewarrior

Replies are listed 'Best First'.
Re^3: Truthfulness of references
by Anno (Deacon) on Mar 13, 2007 at 13:11 UTC
    You wrote:

    3. What's a good way to test for truthfulness:

    "Truthfulness" is an unusual term for the boolean value of something. "Truth value" is more common.

    if ( %$ref ) { ...do something }
    That code is fine if you know that $ref holds a hash ref and you need to decide whether the hash is empty.

    or...

    if ( %$ref && ref $ref eq 'HASH' ) { ...do something }
    Here, it seems, you want to check whether the reference you have is indeed a hash ref, but put this way the condition is pretty much useless. Since you try a hash dereference first, if the ref isn't a hash ref your code will die before it gets to the test.

    You'd have to swap the parts for them to make sense:

    if ( ref $ref eq 'HASH' && %$ref ) {
    Anno

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-23 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found