Perl5 -> Perl 6: about the topic variable
The topic variable
$_
is a well known variable that is used as a global variable when nothing is provided.
In Perl 5 the rule of thumb is that a lot of operators can modify/act the topic varaible when nothing more is provided.
As an example, consider the following piece of code:
my @bands = ( 'Foo Fighters', 'Soundgarden', 'Puddle of Mudd', 'Depeche Mode' );
for ( @bands ){
say "I like $_ !";
}
my %albums_of = map { $_ => int( rand( 10 ) ) } @bands;
for ( keys %albums_of ){
say "I've got $albums_of{ $_ } albums of $_ !";
}
I like Foo Fighters !
I like Soundgarden !
I like Puddle of Mudd !
I like Depeche Mode !
I've got 2 albums of Puddle of Mudd !
I've got 1 albums of Foo Fighters !
I've got 1 albums of Depeche Mode !
I've got 1 albums of Soundgarden !
$_
(topic variable) by default.
Let’s see this in Perl 6:
my @bands = 'Foo Fighters', 'Soundgarden', 'Puddle of Mudd', 'Depeche Mode';
for @bands { "I like $_".say; }
my %albums_of = @bands.map: { $_ => Int( 10.rand ); }
for %albums_of.keys { say "I've got %albums_of{ $_ } albums of $_ !"; }
for @bands { .say; }
# it is the same as
# for @bands { $_.say; }
my $when = Date.today( formatter => { "%02d / %02d / %04d".sprintf: .day, .month, .year ; } );
$when.say;
$_
since none has been specified as prototype.
You can name the topic variable as you want by either place it explicitly or use a named variable (signature):
my $when = Date.today( formatter => -> $o { "%02d / %02d / %04d".sprintf: $o.day, $o.month, $o.year ; } );