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:??;