Mailing List Archive

Identifying Hashes
Is there a safe way to find out if a REF is to a hash-like object,
ie it is either a HASH or a HASH that has been blessed into objecthood?

Obviously one can check all the possible types returned from
ref ($item) and assume that anything else is a blessed hash. But then
if Larry adds a new standard type my code will break... And it's a bit
long to have a switch statement when all I want is

if (LookAndSmellsLikeAHashRef ($item)) {GladWeDidntStepInIt;}
Re: Identifying Hashes [ In reply to ]
Well, there ways of doing this, but I'm not sure that it's a good idea.
If you're writing a data dumper than you have to know, but in general
you don't want to look too deeply inside it.

If you stringify the reference and try

"$ref" =~ /HASH/

but I don't think that folks will like that much.

--tom
Re: Identifying Hashes [ In reply to ]
>>>>> "Tom" == Tom Christiansen <tchrist@mox.perl.com> writes:

Tom> Well, there ways of doing this, but I'm not sure that it's a good idea.
Tom> If you're writing a data dumper than you have to know, but in general
Tom> you don't want to look too deeply inside it.

Tom> If you stringify the reference and try

Tom> "$ref" =~ /HASH/

Tom> but I don't think that folks will like that much.

Warning... you probably want:

"$ref" =~ /=HASH\(\w+\)$/

to be safe. For example:

$ref = bless [], "NOT_A_HASH_REALLY";
print "fooled you" if "$ref" =~ /HASH/;

If you're gonna do something universal, it's best to do it right.

Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@ora.com)
Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me
Re: Identifying Hashes [ In reply to ]
On Fri, 24 Nov 1995 08:24:14 PST, "Randal L. Schwartz" wrote:
>
>Warning... you probably want:
>
> "$ref" =~ /=HASH\(\w+\)$/

Here's what I use (works irrespective of blessed or not):

($pack, $type, $id) = ("$val" =~ /^(?:(.*)\=)?([^=]*)\(([^(]*)\)$/o);


- Sarathy.
gsar@engin.umich.edu
Re: Identifying Hashes [ In reply to ]
Gurusamy Sarathy writes:
>
> On Fri, 24 Nov 1995 08:24:14 PST, "Randal L. Schwartz" wrote:
> >
> >Warning... you probably want:
> >
> > "$ref" =~ /=HASH\(\w+\)$/
>
> Here's what I use (works irrespective of blessed or not):
>
> ($pack, $type, $id) = ("$val" =~ /^(?:(.*)\=)?([^=]*)\(([^(]*)\)$/o);
>

Note also overload::StrVal.

Ilya
Re: Identifying Hashes [ In reply to ]
> From: Ilya Zakharevich <ilya@math.ohio-state.edu>
>
> Gurusamy Sarathy writes:
> >
> > On Fri, 24 Nov 1995 08:24:14 PST, "Randal L. Schwartz" wrote:
> > >
> > >Warning... you probably want:
> > >
> > > "$ref" =~ /=HASH\(\w+\)$/
> >
> > Here's what I use (works irrespective of blessed or not):
> >
> > ($pack, $type, $id) = ("$val" =~ /^(?:(.*)\=)?([^=]*)\(([^(]*)\)$/o);
>
> Note also overload::StrVal.

We will soon need what the ToDo file calls

Vague possibilities
ref function in list context

Once module developers start using overloading and application
developers start using data dumpers of various forms they'll
have problems.

I don't think ref and "$ref" are enough to build robust dumpers in perl.

Tim.