Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Re: A vexing list vs. scalar context question.

by theguvnor (Chaplain)
on Feb 01, 2002 at 00:26 UTC ( [id://142553]=note: print w/replies, xml ) Need Help??


in reply to Re: A vexing list vs. scalar context question.
in thread A vexing list vs. scalar context question.

Shoot.. right there in front of me... thanks japhy. Here's my return volley though - so how would one go about returning a hash (by dereferencing a hashref) if one is called for - there is no wanthash function! Or do I simply test for wantarray still, but do my lookup for what type of variable should be stored in that slot from my %properties hash?

Any hints appreciated.

  • Comment on Re: Re: A vexing list vs. scalar context question.

Replies are listed 'Best First'.
Re: Re: Re: A vexing list vs. scalar context question.
by japhy (Canon) on Feb 01, 2002 at 00:33 UTC
    wantarray is a misnomer -- it should be wantlist. And my response is:
    if (wantarray) { # handle scalars too! return $self->{$x} if not ref $self->{$x}; # handle arrays return @{ $self->{$x} } if ref $self->{$x} eq 'ARRAY'; # handle hashes return %{ $self->{$x} } if ref $self->{$x} eq 'HASH'; # panic! die "unknown reference type (", ref($self->{$x}), ")"; } else { return $self->{$x} }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      I hate 'HASH' eq ref(...) since it doesn't consider blessed hashes to be hashes, and there are sometimes valid reasons for treating blessed hashes as hashes that don't even violate object encapsulation (for example, from inside the object's own methods).

      I'd prefer:

      my $ref= $self->{x}; return %{$ref} if UNIVERSAL::isa($ref,'HASH');

      See also Want.pm.

              - tye (but my friends call me "Tye")
        Perhaps ref() should return an overloaded string that returns true when compared to both the underlying reference type and the class owning the object?

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found