I'm trying to automate building a dispatch table - or at least, to minimize keystrokes.
Please suggest ways to achieve the desired result:
use strict;
use warnings;
my %dispatch = (
normal => sub{"$_[0] Normal dispatch"},
map( {$_ => sub{"$_[0] Sub returns $_"} }
qw| Uno Dos tres|),
);
print $dispatch{"normal"}->(0),"\n"; # 0 Normal dispatch (as expected)
+
print $dispatch{"Uno"}->(1),"\n"; # WANT: "1 Sub returns Uno"
print $dispatch{"Dos"}->(2),"\n"; # WANT: "2 Sub returns Dos"
This currently prints:
0 Normal dispatch
Use of uninitialized value $_ in concatenation (.) or string at .\test
+dispatch.pl line 5.
1 Sub returns
Use of uninitialized value $_ in concatenation (.) or string at .\test
+dispatch.pl line 5.
2 Sub returns
Also - is there a NAME for what I am attempting ? "Early evaluation of $_" ?
"Avoid strange women and temporary variables."
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.