For better readability of the code, how can I rename the $_ variable.

Well, one way might be with English:

my @x = map {$_*2} 1..3; # --becomes--> use English; my @y = map { $ARG * 2 } 1..3;

But I think the point of map and $_ is to be relatively short, so if you want to improve readability by being a bit more verbose, the general pattern for that is:

my @x = map {sprintf("%02x",$_)} 45..48; # --becomes--> my @y; for (45..48) { push @y, sprintf("%02x",$_); } # --becomes--> my @z; for my $val ( 45 .. 48 ) { push @z, sprintf "%02x", $val; }
I would rather know step by step exactly what the code is doing ... I do not see the above code having a list passed to it.

Here are two debugging techniques to help with that: First, Data::Dump (like Data::Dumper but with a bit nicer output), for example:

use Data::Dump; dd( unpack "CCCCCC", "\xab\xcd\x00\x1d\x94\x56" ); __END__ (171, 205, 0, 29, 148, 86)

So you see that unpack is returning a list. Second, if you're unsure what context a function is being called in, like localtime(time) in the following example, then to test it you can replace the function call with the whatcontext function I show below, which makes use of wantarray.

my $q = localtime(time); my ($r) = localtime(time); sub whatcontext { print wantarray ? "list" : defined wantarray ? "scalar" : "void", "\n"; return; } my $s = whatcontext; my ($t) = whatcontext; whatcontext; __END__ scalar list void

In reply to Re: Help me decipher code containing map function by haukex
in thread Help me decipher code containing map function by adamZ88

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.