Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://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!

Consider:

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

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 learning in the Monastery: (2)
As of 2024-04-26 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found