For::Else - Enable else blocks with foreach blocks
use For::Else; foreach my $item ( @items ) { do_something( $item ); } else { die 'no items'; }
We iterate over a list like this:
foreach my $item ( @items ) { do_something( $item ); }
However I find myself needing to accommodate for the exceptional case when the list is empty:
if ( @items ) { foreach my $item ( @items ) { do_something( $item ); } } else { die 'no items'; }
Since we don't enter the foreach block when there are no items, I find the if to be rather redundant. Wouldn't it be nice to get rid of it? Well now you can :)
foreach
if
For::Else is a source filter and doesn't contain any functions.
Fur::Elise by Ludwig van Beethoven
The latest version can be found at:
https://github.com/alfie/For-Else
Watch the repository and keep up with the latest changes:
https://github.com/alfie/For-Else/subscription
Please report any bugs or feature requests at:
https://github.com/alfie/For-Else/issues
Feel free to fork the repository and submit pull requests :)
To install this module type the following:
perl Makefile.PL make make test make install
Alfie John <alfiej@opera.com>
IT COMES WITHOUT WARRANTY OF ANY KIND.
Copyright (C) 2012 by Alfie John
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.
To install For::Else, copy and paste the appropriate command in to your terminal.
cpanm
cpanm For::Else
CPAN shell
perl -MCPAN -e shell install For::Else
For more information on module installation, please visit the detailed CPAN module installation guide.