dwm042 has asked for the wisdom of the Perl Monks concerning the following question:

I was writing code, fast and dirty, and I needed a test for values in a hash that would tell me if they existed and had content that wasn't white space. So I rapidly threw together a test that looked a bit like this:

if ( defined($hash{$key}) && hash{$key} =~ m/\w+/ ) { $hash{$key} = "Numbers: " . $hash{$key}; }
It tested fine using perl -cw but of course fails when it is run. The second 'hash' statement in the "if" should be $hash. Taking this kind of construct out and putting it into a small test program:

#!/usr/bin/perl use warnings; use strict; my %hash = (); $hash{a} = "123"; $hash{b} = "456"; $hash{c} = "789"; my $key = "c"; if ( defined($hash{$key}) && hash{$key} =~ m/\w+/ ) { $hash{$key} = "Numbers: " . $hash{$key}; } print $hash{$key}, "\n";
And running this code using Active State Perl (my servers all have older Perls than the Active State version), I again get the syntax check success and the run time failure.

C:\>perl -cw bareword.pl bareword.pl syntax OK C:\>perl bareword.pl Can't locate object method "hash" via package "c" (perhaps you forgot +to load "c"?) at bareword.pl line 13.
And knowing nothing about all permutations of object methods, shouldn't the method bar() of an object $foo be accessed by $foo->bar()? What am I missing here?

Replies are listed 'Best First'.
Re: Why does this code pass a syntax check?
by Fletch (Bishop) on Mar 26, 2008 at 20:22 UTC

    You've been bit by the dread "indirect object" syntax. Basically bareword $object is another way to write $object->bareword (I think the original impetus was to allow things like my $instance = new Classname foo => "bar"; to be written). It's similar to being able to use print BLOCK LIST where the block's value is the filehandle to use. In your case it works that hash{$key} behaves like $key->hash and then $key had a value of "c" (and you then didn't have a package named "c" with a "hash" method and things blew up).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Why does this code pass a syntax check?
by lodin (Hermit) on Mar 26, 2008 at 20:51 UTC

    B::Deparse is a great tool for this kind of debugging. Running your snippet with the switch -MO=Deparse gives

    if (defined $hash{$key} and do { $key }->hash =~ /\w+/) { $hash{$key} = 'Numbers: ' . $hash{$key}; }
    As you see, and as Fletch has already told you, method BLOCK is valid and an indirect method invocation.

    lodin

A reply falls below the community's threshold of quality. You may see it by logging in.