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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |