[pm-h] when is a anonymous hash reference is not...
G. Wade Johnson
gwadej at anomaly.org
Mon Apr 20 07:24:17 PDT 2015
Good catch and worth reminding people.
Perl usually does the right thing. But in that specific case, the
syntax could be either a naked block or a hash and Perl guessed wrong.
An alternative would be
sub c {+{@list}}
The '+' would only be syntactically valid before a scalar,
disambiguating the syntax.
G. Wade
On Mon, 20 Apr 2015 13:34:05 +0000 (UTC)
"Michael R. Davis via Houston" <houston at pm.org> wrote:
> Perl Folks,Here's an interesting Perl-ism that bit me yesterday.
> I've been starting to drop my sub "return" but I'm going back to
> being explicit as the performance hit has been fixed in later Perls
> and it just doesn't pay for itself in debugging. Moral of the story:
> be explicit. I hope someone finds this helpful.Mike perl -e ' use
> strict; use warnings; use Data::Dumper qw{Dumper}; our @list=(a=>1,
> b=>2); sub a {{a=>1, b=>2}}; #{} is anon hash ref sub b
> {[@list]}; #{} is anon array ref sub c {{@list}}; #{}
> is block returns list sub d {return {@list}}; #{} is anon hash
> refprint Dumper({a=>a(), b=>b(), c=>c(), d=>d()}); '
> Odd number of elements in anonymous hash at -e line 11.
> $VAR1 = {
> '1' => 'b',
> 'c' => 'a',
> 'a' => {
> 'a' => 1,
> 'b' => 2
> },
> 'b' => [
> 'a',
> 1,
> 'b',
> 2
> ],
> 'HASH(0x10b8b70)' => undef,
> '2' => 'd'
> };
>
--
You forgot the first rule of the fanatic: when you become obsessed with
the enemy, you become the enemy. -- Jeffrey Sinclair in "Infection"
More information about the Houston
mailing list