Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

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

Hello all. I have been playing around with Per::Critic on the command line and found to my dismay that some of my modules do not pass gentle. I realize that I can ignore the recommendations of Perl critic, but I would love for my modules to pass "gentle" before I begin ignoring things.

The most common issue is that I modify $_ in list functions. The following is a convenient and short one liner to put the lines of files into a list. I thought doing it this way was nice and slim.

my @list = map { chomp($_); $_ } <$fh>; my @uc_list = map { chomp $_; [uc $_] } <$lc_fh>; # used only onc +e my @split_list = map { chomp $_; [ split(/\|/, $_) ] } <$piped_fh>; +# used only once

In one subroutine, I have this three times.

my @array = map { $_ =~ s/^[\*\+-] (.+)/$1/; some_sub($_, $opt); } @another_array;

Also, my idify subroutine is just modifying $_.

my @ids = map { $_ =~ s/<.+?>//g; $_ =~ s/^(\d+([stnrdh]{2}|))/NUMWORDS($1)/e; $_ =~ s/(.)\.\w{2,5}?$/$1/; $_ =~ s/&amp/and/g; $_ =~ s/&/and/g; $_ =~ s//Ae/g; $_ =~ s//C/g; $_ =~ s//U/g; $_ =~ s/(||)/e/g; $_ =~ s/#/No/g; $_ =~ s/ /_/g; $_ =~ s/[^\w:.\-]//g; $_; } grep {defined($_)} @base;

And my Fancy::Map modifies $_ too.

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}; }

If this is something ignored usually, I will ignore it, but I would like to know how to make it better. Should I just assign $_ to a variable and then use the variable?

Another thing that I am confused by is why the expression form of eval is discouraged?

if ($raw_value =~ /\*/) { # multiplication $total_value = eval($raw_value); ($amount, $base_value) = split(/\*/,$raw_value); } elsif ($raw_value =~ /\//) { # division $base_value = eval($raw_value); ($total_value, $amount) = split(/\//, $raw_value); }

The rest of the gentle issues are me being lazy with conditionals. While writing I was thinking my $var = "foo" if 'some condition', but Perl critic does not like it, but it is fixed easily.

NOTE: Please see my update. Most are fixed, but the eval problem remains.

My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.8.8 on web host.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

In reply to 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 taking refuge in the Monastery: (3)
    As of 2021-01-23 08:47 GMT
    Find Nodes?
      Voting Booth?