Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

For me, the critical thing to realize about a statement like
    my @array = map { $_ =~ s/^[\*\+-] (.+)/$1/; ... } @another_array;
is that it is operating on the elements of @another_array at the same time it's generating a new array to assign to @array. So the function

sub fancy_map { my ($opt, $list) = @_; map { if (ref($_)) { fancy_map($opt, $_); } else { my $before = $opt->{'before'} ? $opt->{'before'}.' ' : ''; my $after = $opt->{'after'} ? ' '.$opt->{'after'} : ''; $_ = $before.$_.$after; } } @{$list}; }
which is passed an array by the reference $list is also operating on the referent of $list!


c:\@Work\Perl\monks>perl -wMstrict -le "my @list = qw(foo bar baz boff quux); print qq{original: @list}; ;; my @spookified = spooky_aaad(\@list); print qq{returned: @spookified}; print qq{original: @list}; ;; sub spooky_aaad { my ($array_ref) = @_; ;; return map { $_ =~ s{ \A (.) (.*) \z }{\U$2\E$1}xms; $_; } @$array_ref; } " original: foo bar baz boff quux returned: OOf ARb AZb OFFb UUXq original: OOf ARb AZb OFFb UUXq
(Update: Also, the statement
    my @another_array = map { chomp($_);  $_; } @original_array;
is equivalent to the statements
    my @another_array = @original_array;
    chomp @another_array;
    chomp @original_array;
because $_ is aliased to the elements of @original_array. In the case of a statement like
    my @list = map { chomp($_); $_ } <$fh>;
this doesn't matter: the <$fh> expression creates a temporary list that is fed to map and then vanishes. But in other cases... See also ikegami's reply.)

Any time I see code structured in this way, my buggy-sense starts to tingle. If you're happy with the operation of your code, that's fine with me; you can just ignore or (I think) turn off the specific critique. Just be aware that pitfalls lurk here.

Update: Oops... In my haste to answer, I overlooked the latter questions of your post.

...why the expression form of eval is discouraged?
String eval is looked upon askance because it is an open invitation to all kinds of mischief if your data is not absolutely trustworthy — and even then many programmers would avoid it, preferring alternatives that may be slightly more roundabout, but that are a whole bunch safer.
my $var = "foo" if 'some condition';
This inadvertantly allowed syntax leaves $var in an undefined state of existence. It used to be used to simulate a persistent, "static" variable within a function; state is used for this purpose now, and the old usage is officially Frowned Upon.

Give a man a fish:  <%-{-{-{-<

In reply to Re: Perl::Critic says don't modify $_ in list functions and other things (updated x2) by AnomalousMonk
in thread Perl::Critic says don't modify $_ in list functions and other things by Lady_Aleena

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2021-03-01 12:48 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (4 votes). Check out past polls.