For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2005 > references









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author references
The Ghost

2005-11-24, 6:56 pm

Is there a way I can know if a variable ($hash{someKey}) is an array
or an arrayref or hashref or scalar?
I could use eval, but I thought I remember there was some other
function for doing this.

Ryan
John Doe

2005-11-24, 6:56 pm

The Ghost am Donnerstag, 24. November 2005 18.18:
> Is there a way I can know if a variable ($hash{someKey}) is an array
> or an arrayref or hashref or scalar?
> I could use eval, but I thought I remember there was some other
> function for doing this.


Yes,
perldoc -f ref
:-)

greetings, joe
Eric Pretorious

2005-11-24, 6:56 pm

On Thu, 2005-11-24 at 11:18 -0600, The Ghost wrote:
> Is there a way I can know if a variable ($hash{someKey}) is an array
> or an arrayref or hashref or scalar?
> I could use eval, but I thought I remember there was some other
> function for doing this.


>From the third edition of "Programming Perl"

(http://www.unix.org.ua/orelly/perl/prog3/ch29_02.htm):

> ref EXPR
> ref
> The ref operator returns a true value if EXPR is a reference, false
> otherwise. The value returned depends on the type of thing the
> reference refers to. Built-in types include:
> SCALAR
> ARRAY
> HASH
> CODE
> GLOB
> REF
> LVALUE
> IO::Handle
> If the referenced object has been blessed into a package, then that
> package name is returned instead. You can think of ref as a "typeof"
> operator.
> if (ref($r) eq "HASH") {
> print "r is a reference to a hash.\n";
> }
> elsif (ref($r) eq "Hump") { # Naughty--see below.
> print "r is a reference to a Hump object.\n";
> }
> elsif (not ref $r) {
> print "r is not a reference at all.\n";
> }


Eric P.
Sunnyvale, CA

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com