2015-07-03 21 views
13

Non capisco il segno + zucchero in questo esempio tratto da qualche parte, mentre goggling:

sub bar { +{$_[1] => $_[2]} } 

ho scritto questo e non vedo alcuna differenza qui:

use Data::Dumper; 

# Not any differences here 
my $foo = {value => 55}; 
my $bar = +{value => 55}; 

print Dumper $foo; 
print Dumper $bar; 

# Oh ! Here there is something... 
sub foo { {$_[1] => $_[2]} }; 
sub bar { +{$_[1] => $_[2]} }; 

print Dumper foo('value', 55);  
print Dumper bar('value', 55);  

foo restituisce

$VAR1 = 55; 
$VAR2 = undef; 

bar rendimenti

$VAR1 = { 
      '55' => undef 
     }; 
+2

Si sta già utilizzando Data :: Dumper, così ho semplificato e ne ha fatto eseguibile da più persone – ikegami

risposta

16

Aiuta il parser a distinguere tra un hash anonimo e un blocco di codice.

Citando Learning Perl Objects, References & Modules

perché blocchi e costruttori di hash anonimi entrambi utilizzano le parentesi graffe in circa stessi luoghi nella struttura di sintassi, il compilatore deve fare determinazioni ad hoc su quale dei due vuoi dire. Se il compilatore decide in modo errato, potrebbe essere necessario fornire un suggerimento per ottenere ciò che desideri. Per mostrare al compilatore che si desidera un costruttore di hash anonimo, inserire un segno più prima della parentesi graffa di apertura: + {...}. Per essere sicuro di ottenere un blocco di codice, basta mettere un punto e virgola (che rappresenta un'istruzione vuota) all'inizio del blocco: {; ...}.

O dalla documentazione sulla funzione 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: 

    %hash = map { "\L$_" => 1 } @array # perl guesses EXPR. wrong 
    %hash = map { +"\L$_" => 1 } @array # perl guesses BLOCK. right 
    %hash = map {; "\L$_" => 1 } @array # this also works 
    %hash = map { ("\L$_" => 1) } @array # as does this 
    %hash = map { lc($_) => 1 } @array # and this. 
    %hash = map +(lc($_) => 1), @array # this is EXPR and works! 

    %hash = map (lc($_), 1), @array # evaluates to (1, @array) 

or to force an anon hash constructor use "+{": 

    @hashes = map +{ lc($_) => 1 }, @array # EXPR, so needs 
              # comma at end 

to get a list of anonymous hashes each with only one entry apiece. 
+0

Qual è il metodo da utilizzare per trovare questo documentazione? Ho provato goggling 'perl" + {} "e' perl "+ {...}" 'o anche' perl plus sign hash'. Non ho trovato nulla di rilevante. Qual è il tuo trucco magico? – nowox

+2

Lo conosco leggendo la documentazione su 'map' http://perldoc.perl.org/functions/map.html – Sobrique

+0

Ho cercato su google" perl hash plus sign "e ho ricevuto il link come primo hit. – Matteo