1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bernhard@bernhard-Aspire-A515-57:/tmp$ cat t1.pl
use strict;
use warnings;
use Data::Dx;
my @ElementList;
push @ElementList,
map { "$_", 'X' }
( 1, 2 );
Dx( \@ElementList );
bernhard@bernhard-Aspire-A515-57:/tmp$ perl t.pl
Backslash found where operator expected at t.pl line 10, near "Dx \"
(Do you need to predeclare Dx?)
syntax error at t.pl line 9, near "( "
Execution of t.pl aborted due to compilation errors.
bernhard@bernhard-Aspire-A515-57:/tmp$
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bernhard@bernhard-Aspire-A515-57:/tmp$ cat t2.pl
use strict;
use warnings;
use Data::Dx;
my @ElementList;
push @ElementList,
map { $_, 'X' }
( 1, 2 );
Dx( \@ElementList );
bernhard@bernhard-Aspire-A515-57:/tmp$ perl t2.pl
#line 10 t2.pl
( \@ElementList ) = [1, "X", 2, "X"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bernhard@bernhard-Aspire-A515-57:/tmp$ cat t3.pl
use strict;
use warnings;
use Data::Dx;
my @ElementList;
push @ElementList,
map {; "$_", 'X' }
( 1, 2 );
Dx( \@ElementList );
bernhard@bernhard-Aspire-A515-57:/tmp$ perl t3.pl
#line 10 t3.pl
( \@ElementList ) = [1, "X", 2, "X"]
1 2 3 4 5 6 7 8 9 10
use strict; use warnings; use Data::Dx; my @ElementList; push @ElementList, map { "$_", 'X' }->%*, ( 1, 2 ); Dx( \@ElementList );
1 2 3 4 5 6 7 8 9 10 11 12 13 14
use strict; use warnings; use Data::Dumper; my @li = map { ("$_", 'X') } ( 1, 2 ); print Dumper \@li; $VAR1 = [ '1', 'X', '2', 'X' ];
1 2 3 4 5 6 7 8 9 10
my @li = map { ($_, 'X') } ( 1, 2 ); print Dumper \@li; $VAR1 = [ 1, 'X', 2, 'X' ];
2024-08-02T15:41:21 hajJa, aber das hat mit dem Thema von barney nichts zu tun. In dieser Schreibweise ist das Token nach der geschweiften Klammer die runde Klammer, damit wird erstere als Code-Block interpretiert. Der Rest ist normales Perl.
2024-08-02T15:45:47 rostiDas Token ist in allen 3 barney-Fällen eine runde Klammer.
my %found = map { $_ => 1 } @list;
Quotemy %found = map { $_ => 1 } @list;
2024-08-03T08:48:33 barneyDie Variante mit einer Expression als erstes Argument verwende ich nie. Ich habe auch nachgeforscht ob es eine Perl::Critic Policy gibt die die ungewöhnliche Verwendung anmeckert. Habe aber nichts gefunden.
https://perldoc.pl/functions/map{ starts both hash references and blocks, so map { ... could be either the start of map BLOCK LIST or map EXPR, LIST. Because Perl doesn't look ahead for the closing } it has to take a guess at which it's dealing with based on what it finds just after the {. Usually it gets it right, but if it doesn't it won't realize something is wrong until it gets to the } and encounters the missing (or unexpected) comma. The syntax error will be reported close to the }, but you'll need to change something near the { such as using a unary + or semicolon to give Perl some help:
my %hash = map { "\L$_" => 1 } @array # perl guesses EXPR. wrong
my %hash = map { +"\L$_" => 1 } @array # perl guesses BLOCK. right
my %hash = map {; "\L$_" => 1 } @array # this also works
my %hash = map { ("\L$_" => 1) } @array # as does this
my %hash = map { lc($_) => 1 } @array # and this.
my %hash = map +( lc($_) => 1 ), @array # this is EXPR and works!
my %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array)
or to force an anon hash constructor use +{:
my @hashes = map +{ lc($_) => 1 }, @array # EXPR, so needs
# comma at end
to get a list of anonymous hashes each with only one entry apiece.
map HASHREF
1 2 3 4 5 6 7 8 9
use strict; use warnings; use Data::Dumper; use FindBin qw($Bin); my @dirs = grep -d, ('foo','bar','.',$Bin,'c:/temp'); # valide directories my $hr = { map{ ("here", "$_") } @dirs }; my $ar = [ map{ ("here", "$_") } @dirs ]; print Dumper $hr, $ar;
my %hash = "here" => $_ for grep { -d $_ } qw{a b c .};
my %hash = ("here" => undef);
my %hash = ("here" => $_) for grep { -d $_ } qw{a b c .};
1
2
3
4
$ perl -MData::Dumper -E 'print Dumper %hash = "here" => $_ for grep { -d $_ } qw{a b c .}'
$VAR1 = 'here';
$VAR2 = undef;
$VAR3 = '.';
1
2
3
4
$ perl -MO=Deparse,-p -E '%hash = "here" => $_ for grep { -d $_ } qw{. a b c eg}'
use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'postderef_qq', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
((%hash = 'here'), $_) foreach (grep({(-d $_);} '.', 'a', 'b', 'c', 'eg'));
-e syntax OK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# Zugriff auf nicht existierende Schlüssel in ENV # gibt einen definierten Leerstring zurück package TieENV; use strict; use warnings; use Tie::Hash; use base ('Tie::StdHash'); sub TIEHASH{ my $class = shift; my $self = { map{ $_ => $ENV{$_} } keys %ENV }; return bless $self, $class; } sub FETCH{ my $self = shift; my $key = shift; return exists $self->{$key} ? $self->{$key} : ''; } tie %ENV, __PACKAGE__; 1;
1 2 3 4 5 6
use strict; use warnings; use TieENV; $, = "\n"; print $ENV{foo}, $ENV{PATH};
1 2
my $self = { map{ $_ => $ENV{$_} } keys %ENV }; my $self = do{ my %h = %ENV; \%h };