Syntax::Feature::Qwa - qwa(), qwh() and qwk() quote-like operators to create arrayrefs and hashrefs
use syntax qw/qwa/; use Data::Dumper; print Dumper qwa(foo bar baz quux); # [ # 'foo', # 'bar', # 'baz', # 'quux', # ] print Dumper qwh(foo bar baz quux); # { # 'foo' => 'bar', # 'baz' => 'quux', # } print Dumper qwk(foo bar baz quux); # { # 'foo' => 1, # 'bar' => 2, # 'baz' => 3, # 'quux' => 4, # }
Perl's word list operator (qw()) is really nice. It allows you to build simple lists without needing much punctuation. But it's quite common to see it wrapped by additional punctuation in the form of:
qw()
my $array = [qw(foo bar baz)];
It would be quite nice to have a version of the word list operator which returned an arrayref instead of a list. That's where this module comes in. It provides a "word list arrayref" operator:
my $array = qwa(foo bar baz);
It also provides companion "word list hashref" and "word list hashref keys" operators.
This module is intended to be used with the syntax module. This allows you to switch on multiple syntax extensions in one line:
use syntax 'ql', 'qwa', 'io';
It is also possible to use this module without syntax.pm:
use Syntax::Feature::Qwa;
If you want to rewrite code using this module to remove its dependency on it, or if you just want to better understand how it works, here are some equivalents between this module's operators, and how they'd be expressed without this module.
my $arrayref = qwa(Foo Bar Baz); my $arrayref = [ qw(Foo Bar Bar) ];
my $hashref = qwh(Foo Bar Baz); my $hashref = +{ qw(Foo Bar Bar) };
my $hashref = qwk(Foo Bar Baz); my $hashref = +{ do { my $i = 0; map { $_, ++$i } qw(Foo Bar Bar) } };
# Create a lookup table my $days = qwk(Mon Tue Wed Thu Fri Sat Sun); # The task is to sort these into their weekly order my @list = qw(Fri Tue Wed); # Easy! my @sorted_list = sort { $days->{$a} <=> $days->{$b} } @list;
my $admins = qwk(alice bob carol); my $login = get_current_user(); if ($login ~~ $admins) { ... }
The example above also works using arrayrefs. For smaller lists, arrayrefs might be faster; for larger lists hashrefs probably will be.
my $admins = qwa(alice bob carol); my $login = get_current_user(); if ($login ~~ $admins) { ... }
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Syntax-Feature-Qwa.
syntax, Syntax::Feature::Ql, PerlX::QuoteOperator.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2012 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
To install Syntax::Feature::Qwa, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Syntax::Feature::Qwa
CPAN shell
perl -MCPAN -e shell install Syntax::Feature::Qwa
For more information on module installation, please visit the detailed CPAN module installation guide.