Jeremiah Foster wrote:
Hi there!
Hello,
This may or may not be a beginners question. If not, please let me
know where I ought to post. :-)
I have a data structure, a simple array. It is made up of sections of
files I have slurped;
sub _build_packages {
use Perl6::Slurp;
Do you really need to use this module?
my @pkgs;
# iterate over the packages slurping them into one
map { push @pkgs, (slurp $_, {irs => qr/\n\n/xms}) } @packages;
You shouldn't use map() in void context:
return [ map { slurp $_, { irs => qr/\n\n/ } } @packages ];
return \...@pkgs;
}
(The above code is in the class declaration)
Now in my program which subclasses that array ref, after
de-referencing I have this idiom;
my %versions;
map {
my $package = $_;
# autovivfy a hash with versions of packages
$versions{$package} = [ ] unless exists $versions{$package};
push @{ $versions{$package} = $version
} @packages
Again, you shouldn't use map() in void context:
foreach my $package ( @packages ) {
# autovivfy a hash with versions of packages
push @{ $versions{ $package } }, $version
}
So my questions are:
Is this an efficient way to do this? Am I using the idiom correctly?
Could I make it more readable? Is my predilection for map over foreach
making this less readable? Or is that only a question of style?
It depends on which version of Perl this will run on. In older versions
map() in void context would create a list that would be discarded.
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/