Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
On the other hand, it looks to me that map is somewhat more magical than the examples in perlsub; if you call
map (sub { "1" x $_ }, (1..10));
it returns an array of coderefs (references to subroutines) which is not at all what is needed here.

I personally believe that indeed map and its sibling grep are more magical: precisely because as I wrote in my reply to the OP they accept an expression to be evaluated for each of the other parameters. But I don't understand what you mean with "which is not at all what is needed here" because your example does exaclty what I mean: I do expect it to return a list (not an array!) of coderefs. Only, it probably does not do what you expect in that those coderefs are... all the same coderef:

C:\temp>perl -E "say for map sub {1 x $_} => 1..3" CODE(0x182a944) CODE(0x182a944) CODE(0x182a944)

This is because $_ is a package variable and those anonymous subs are not closures: when you will use one of them, its $_ will be that in scope at the moment, not the one passed to map() when it was created. To obtain that you have to close over a lexical:

C:\temp>perl -E "say for map { my $x=$_; sub {1 x $x} } 1..3" CODE(0x23aa7c) CODE(0x182aef4) CODE(0x182ad74)

Alternatively, and this is the interesting point to be noted here, Perl 5.10 and highter support lexical $_:

C:\temp>perl -E "my $_; say for map sub {1 x $_} => 1..3" CODE(0x23a97c) CODE(0x182a9bc) CODE(0x183bbbc)
--
If you can't understand the incipit, then please check the IPB Campaign.

In reply to Re^4: Generate the perl sequence 1, 11, 111, .... by blazar
in thread Generate the perl sequence 1, 11, 111, .... by alih110

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found