Thread Block vs. Hashref (25 answers)
Opened by barney at 2024-08-02 13:17

barney
 2024-08-02 13:17
#196612 #196612
User since
2008-08-31
161 Artikel
BenutzerIn
[Homepage] [default_avatar]
Also manchmal erstaunt mich Perl wieder aufs Neue. Es ist mir klar, dass man die Unterscheidung zwischen Kodeblöcken und Hashreferenzen manchmal eindeutig machen muss. Die konkreten Regeln sind aber etwas undurchsichtig. Hier drei Beispiele:


Code: (dl )
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$



Code: (dl )
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"]


Code: (dl )
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"]


t1.pl und t3.pl sind nachvollziehbar. Dass t2.pl, mit einem einfachen $_, tut ist IMHO nicht wirklich intuitiv.

View full thread Block vs. Hashref