I am playing with List::MoreUtils::pairwise and noticed some funny
behaviour. I'm trying to merge two lists of hashes into one list of
hashes:

p...@tui:~/tmp$ cat bar.pl
use strict;
use warnings;

use List::MoreUtils qw(pairwise);
use Data::Dumper;

my @aoh_a = ( { foo => 1, bar => 2 }, {foo => 3, bar => 4} );
my @aoh_b = ( { fred => 5, jim => 23}, {fred => 1089, jim => 1729} );

my @aoh = pairwise { return { %{$a}, %{$b} } } @aoh_a, @aoh_b;

print Dumper(\...@aoh);
p...@tui:~/tmp$ perl bar.pl
Name "main::b" used only once: possible typo at bar.pl line 10.
Name "main::a" used only once: possible typo at bar.pl line 10.
$VAR1 = [
          {
            'bar' => 2,
            'jim' => 23,
            'foo' => 1,
            'fred' => 5
          },
          {
            'bar' => 4,
            'jim' => 1729,
            'foo' => 3,
            'fred' => 1089
          }
        ];

This works fine. But I noticed that if I remove the explicit "return"
in the pairwise block, I get different results:

p...@tui:~/tmp$ cat bar.pl
use strict;
use warnings;

use List::MoreUtils qw(pairwise);
use Data::Dumper;

my @aoh_a = ( { foo => 1, bar => 2 }, {foo => 3, bar => 4} );
my @aoh_b = ( { fred => 5, jim => 23}, {fred => 1089, jim => 1729} );

my @aoh = pairwise { { %{$a}, %{$b} } } @aoh_a, @aoh_b;

print Dumper(\...@aoh);
p...@tui:~/tmp$ perl bar.pl
Name "main::b" used only once: possible typo at bar.pl line 10.
Name "main::a" used only once: possible typo at bar.pl line 10.
$VAR1 = [
          'bar',
          2,
          'foo',
          1,
          'jim',
          23,
          'fred',
          5,
          'bar',
          4,
          'foo',
          3,
          'jim',
          1729,
          'fred',
          1089
        ];

Is this because the curlies are being interpreted as a block rather
than an anonymous hash constructor? If so, when are they anonymous
hash constructors and when are they blocks? How might I avoid this
issue in future? And secondary question: how can I avoid the warnings
about $main::a and $main::b?

Philip

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to