Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: A vexing list vs. scalar context question.

by japhy (Canon)
on Feb 01, 2002 at 00:21 UTC ( [id://142552]=note: print w/replies, xml ) Need Help??


in reply to A vexing list vs. scalar context question.

To answer your bolded question, it's because you explicitly turned off strict references! And you might need to rethink your wantarray return value, since @{ $self->{foo} } won't dereference a hash reference...

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

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

Replies are listed 'Best First'.
Re: Re: A vexing list vs. scalar context question.
by theguvnor (Chaplain) on Feb 01, 2002 at 00:26 UTC
    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.

      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")

Log In?
Username:
Password:

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

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

    No recent polls found