Ref::Explicit - Keywords to create arrayrefs and hashrefs
version 0.001
use Ref::Explicit qw(arrayref hashref); my $arrayref = arrayref map { ... } @values; my @array_of_hashrefs = map { hashref $key => $value, ... } @values;
This module exports keywords that provide an explicit syntax for creation of arrayrefs and hashrefs in contexts where the ordinary perl syntax leads to a punctuation overdose.
arrayref
Return an array reference containing the values passed as arguments. Useful if you have long map-like expressions whose result you need as am array reference rather than as an ordinary list. Consider the following example:
map
sub search { ... my @result = grep { ... # complex multiline ... # calculations } @values; return \@result; }
You need to introduce an extra variable (@result) in order to return a reference. This is a very common scenario, e.g. in Moose attribute builders. You could avoid the extra variable by using square brackets:
@result
return [ grep { ... } @values ];
But this makes the syntax ugly and the intent unclear. With arrayref the above code becomes:
return arrayref grep { ... } @values;
hashref
Return a hash reference containing the values passed as arguments. Useful within map-like expressions that return a list of hashrefs. Consider the following example:
my @names = ("Steven Spielberg", "George Lucas"); my @persons = map { +{ name => $_, industry => 'Movies' } } @names;
The + (plus) sign tells the parser to evaluate the code in curly brackets as an anonymous hashref rather than as a block. With hashref this can be written more elegantly as:
+
my @persons = map { hashref name => $_, industry => 'Movies' } @names;
These functions provide clarity, not speed. Use the core syntax if speed is of the essence.
Peter Shangov <pshangov@yahoo.com>
This software is copyright (c) 2011 by Peter Shangov.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Ref::Explicit, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Ref::Explicit
CPAN shell
perl -MCPAN -e shell install Ref::Explicit
For more information on module installation, please visit the detailed CPAN module installation guide.