Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Wednesday August 04, 2004
04:08 PM

Obvious and annoying

[ #20236 ]

Obvious in retrospect, but it was annoying when I stumbled on it. This was fairly obscure in a much larger program.

use warnings;
use strict;

foo('bar');

my %hash = (bar => 'value');
sub foo { print $hash{+shift} }

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • It took me a while. Too used to doing that sort of thing in modules, where it works.
  • I give... what is it supposed to do?

    TIA
    • what is it supposed to do?

      The function foo takes the name of a key and prints out the corresponding value from %hash.

      You might expect it would print 'bar' but the call to foo is executed before the line that initialises %hash so instead Perl complains about an uninitialised value and exits.

    • hm... didn't realize that the + was a unary operator.

      Is this thing supposed to print out 'bar'?
      • One might think that it should print out 'value', but the crux of the problem, as noted by grantm, is that the hash is declared at compile time (so strict doesn't complain) but the line that assigns data to the hash is not executed by the time &foo is called. this means that the hash is empty, even though it's been declared. I can only remember being bitten by this twice, but both times it's confused the heck out of me.

        • One might think that it should print out 'value'

          Of course... I meant to say that. ;-)

          Which reminds me.

          If I reply to someone's comment, I see the message I'm replying to on the page I type my response in.

          But, if I reply to a journal entry, I don't see the message and I'm basically "flying blind" and going from memory.

          To make matters worse, replying to a comment involves clicking on a link. So, I can open the page in another tab and have easy access to the original comment. But, replying to a journal en