The map abuser likes to pick up the pipe
Sometimes it is enjoyable to abuse map. For example, it is possible to assign
one scalar to another while using map statements as an intermediate pass through 'filter'.
This is a convienient typographical convention because you can 'chain' results together
in a manner similar to 'pipes' in the Unix OS shell. This is useful because
each step in the chain acts *in parallel* as an incremental and mutually substitutable
filter. It is also useful because it allows you to both assign and modify a scalar
in one statement.
The ugly side of map abuse
By its design, map is intended for use with arrays, not scalars. This
design characteristic results in some fairly ugly artefacts in our code when
we try to force map to do what we want.
- ugly0: parenthesis around the scalar used to invoke list context
- ugly1: special $_ variable tacked on just to get what we want out of map
- ugly2: scalar enclosed in a CODEREF to prevent it from being modified by map
my $sBegin = "hello world";
my $sEnd = "";
($sEnd) = ### ugly0
map{uc($_);}
map{s/world/todo el mundo/;$_;} ### ugly1
map{s/hello/goodbye/g;$_;} ### ugly1
(sub{$sBegin}->()); ### ugly0, ugly2
print $sEnd;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.