On Wed, Jul 15, 2009 at 19:19, Steve Bertrand<st...@ibctech.ca> wrote:
> Hi all,
>
> While writing some tests, I ran into something that took me quite a
> while to troubleshoot. Although I figured out the problem, I don't
> understand why the problem is occurring.
>
> Can someone point out the importance of the brackets in which '2'
> prints, but '1' does not? I've always thought that the brackets could be
> omitted:
>
> print "1" if ref $href  =~ /HASH/;
> print "2" if ref($href) =~ /HASH/;
>
> Steve
>

A side note to the main discussion: never use ref to check if a
reference is to a specific type of variable; use reftype from
Scalar::Util instead.  Only use ref to determine if a scalar is a
reference or to find out its class.  Here is why:

#!/usr/bin/perl

use strict;
use warnings;
use Scalar::Util qw/reftype/;

my $h = bless {}, "foo";

print "ref:     ", ref $h, "\nreftype: ", reftype $h, "\n";

If you are on a version of Perl prior to 5.8 (when Scalar::Util became
part of the core) and do not want to install Scalar::Util from CPAN,
you can say

if (ref $aref and UNIVERSAL::isa($aref, "ARRAY")) {
        print "\$aref is an array reference\n";
}

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to