Steve Bertrand wrote:
Steve Bertrand 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/;

... is it because ref() is trying to work like this?:

ref ($href =~ /HASH/);

In a word, yes.

$ perl -MO=Deparse,-p -e'ref $href  =~ /HASH/'
ref(($href =~ /HASH/));
-e syntax OK
$ perl -MO=Deparse,-p -e'ref( $href ) =~ /HASH/'
(ref($href) =~ /HASH/);
-e syntax OK

Perhaps you meant to use the higher precedence operator eq:

$ perl -MO=Deparse,-p -e'ref $href eq "HASH"'
(ref($href) eq 'HASH');
-e syntax OK




John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to