Monks, I have a(some) question(s) having to do with the critique of my subroutines by Perl::Critic. I am new to Perl and have been writing my code to perform what I would like then making the recommendations from Perl::Critic. I know this isn't the most efficient way to learn the language but it has helped somewhat since I am new. Anyway, here are the two sub routines I have:

#!/usr/bin/perl # $Id: XXX.plx; # $Revision: 1 $ # $HeadURL: 1 $ # $Source: /Perl/XXX.plx $ # $Date: 10.30.2012 $ # $Author: daugh016 $ use 5.014; #this enables strict use warnings; use vars qw/ $VERSION /; $VERSION = '1.00'; use English qw(-no_match_vars); use Readonly; my $print_err = "Cannot print:\t"; my $var_test = 'This is a test variable'; print_var_with_err( "\$var_test", "\$print_err" ); my @list_test = qw(This is a test list); print_list_with_err( "\@list_test", "\$print_err" ); # Print variable with error messages sub print_var_with_err { my ( $a, $b ) = @_; my $a_eval = eval $a; my $b_eval = eval $b; print $a . qq{:\n} . $a_eval . qq{\n} or croak( $b_eval . $ERRNO ) +; return; } # Print list with error messages sub print_list_with_err { my ( $c, $d ) = @_; my @c_eval = eval "$c"; my $d_eval = eval $d; while ( my ( $index, $elem ) = each @c_eval ) { say $c . q{[} . $index . qq{]:\n} . $elem . qq{\n} or croak( $d_eval . $ERRNO ); } return; }

The output follows:
$var_test:
This is a test string
@list_test[ 0]:
This

@list_test[ 1]:
is

@list_test[ 2]:
a

@list_test[ 3]:
test

@list_test[ 4]:
list

I am getting the following problems from Perl::Critic:

Useless interpolation of literal string at line 21, column 21. See page 51 of PBP. Severity: 1
Useless interpolation of literal string at line 21, column 35. See page 51 of PBP. Severity: 1
Useless interpolation of literal string at line 23, column 22. See page 51 of PBP. Severity: 1
Useless interpolation of literal string at line 23, column 37. See page 51 of PBP. Severity: 1
Expression form of "eval" at line 29, column 18. See page 161 of PBP. Severity: 5
Expression form of "eval" at line 30, column 18. See page 161 of PBP. Severity: 5
Expression form of "eval" at line 39, column 18. See page 161 of PBP. Severity: 5
Expression form of "eval" at line 40, column 18. See page 161 of PBP. Severity: 5

I cannot figure out how to fix these issues.

Also, since essentially these functions are doing the same thing (one with variables and one with lists), is there a way to combine them where it wouldn't matter if variables or arrays are passed?

Last, I am sure I am missing some other best practices. Any suggestions would be greatly appreciated!

Thank you Monks! You guys/gals are great!


In reply to Perl::Critic and Subroutines by daugh016

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.