Obvious and annoying

Ovid on 2004-08-04T21:08:38

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} }


Argh

vsergu on 2004-08-04T22:11:01

It took me a while. Too used to doing that sort of thing in modules, where it works.

Not that obvious

phillup on 2004-08-05T00:39:34

I give... what is it supposed to do?

TIA

Re:Not that obvious

grantm on 2004-08-05T00:48:48

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.

Re:Not that obvious

phillup on 2004-08-05T00:51:41

Thanks.

I was actually getting hung up on the +shift part... didn't realize that the + was a unary operator.

I thought it would choke because it didn't have a left hand value...

Re:Not that obvious

grantm on 2004-08-05T08:43:24

Yes, the + is there to stop
$hash{shift}
being interpreted as
$hash{'shift'}

Re:Not that obvious

phillup on 2004-08-05T00:49:33

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

Is this thing supposed to print out 'bar'?

Re:Not that obvious

Ovid on 2004-08-05T19:04:51

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.

Re:Not that obvious

phillup on 2004-08-05T20:40:08

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 entry involves a button... and opens in the same tab. So, using a nice browser doesn't help either...

Is this something I can change via a preference setting on this site, or is it a "feature"?

TIA

Re:Not that obvious

Ovid on 2004-08-05T21:55:01

I'm not aware that it's changeable, but I've also not paid much attention to the customization features.