Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

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


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

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

Replies are listed 'Best First'.
(tye)Re: A vexing list vs. scalar context question.
by tye (Sage) on Feb 01, 2002 at 00:51 UTC

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

        Perhaps ref() should return an overloaded string that returns true when compared to both the underlying reference type and the class owning the object?
        Why? That's what UNIVERSAL::isa() is for. Or should the return value from ref() be an overloaded string that returns true when compared to the underlying reference type, or the class owning the object, or any of the super classes of the class owning the object? That seems an unnecessarily complicated solution to a problem that has already been solved.

        No, ref() should completely ignore blessedness and there should be a blessed() function that returns false if the reference is not blessed, and the package it is blessed into if it is blessed.

        I've seen too many cases of practical code that wants to ask "is this blessed" and is force to jump through some pretty stupid hoops. The overloading of ref() to combine both reference type and blessedness state was a mistake from the start.

                - 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://142556]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found