hate perl5 if you want … but please use good reasons !
ok … i just read a coworker claiming he « hates perl » because of this line:
my @subfields=map{ ($$_[0],$$_[1]) if 22 > $subfcodes{$$_[0]}) } $field->subfields();Ok … I have to admit that it’s not really readable when you come from php and C paradigms but i guess it wouldn’t be more readable in another featured dynamic langage. According to me: there are 2 points that makes it unreadable:
First: it uses a functionnal approach to treat the return of the $field->subfields list throught the map function … It’s called List comprehension. If you blame perl for it, you’ll soon blame every widely used dynamic langages because they all support this cool feature (including python). If you are not familiar with it, please pay attention to the fact that functionnal syntaxes are more and more present around us (new langages adoption as well as influence).
so please concider learn map and grep now!
Second: can you blame perl because of its usage? According to me, the same code can be written in a cleaner way in perl by using
a) variable names
b) line feeds
c) array syntax
So now, the code rewrite
my @subfields=map{ ($$_[0],$$_[1]) if 22 > $subfcodes{$$_[0]}) } $field->subfields();I’ll rewrite this code
- without using list comprehension
- with PBP unroll @_ in mind
my @subfields;
for my $s ( $field->subfields ) {
my ( $code, $value ) = @$s; # unroll the array refered by $s
if ( $subfcodes{ $code } < 22 ) {
push @subfields, ($code,$value);
}
}now … back to the list comprehension using 2 ways to write it. I would use the first one in a code that i’ll have to maintain in long term and with coworkers ( such in koha code) and the second in a one shot script.
my @subfields = map {
my ($code, $value) = @$_;
if ( $subfcodes{ $code } < 22 ) { ($code,$value) }
else { () }
} $field->subfields;my @subfields = map { $subfcodes{ $$_[0] } < 22 ? @$_ : () } $field->subfields;Related posts: