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?


In reply to Why does this code pass a syntax check? by dwm042

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.