in reply to Re^2: Perl::Critic and Subroutines
in thread Perl::Critic and Subroutines
"I cannot implement the recommendations without losing the functionality."
Well then, Perl::Critic is having the desired effect - steering you away from dodgy areas. :-)
"Are there better ways to get the same output without the sloppy coding I have done?"
Yes, others have pointed you towards PadWalker. That's a much better way for a sub to peek at its caller's variables. Much better in that it actually usually works. (Your current solution works through luck - because the variables you're peeking at are actually file-level.
Here's an example using that.
#!/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; use PadWalker qw(); sub peek_above { my $name = shift; PadWalker::peek_my(2)->{$name} // PadWalker::peek_our(2)->{$name}; } my $print_err = "Cannot print:\t"; my $var_test = 'This is a test string'; print_var_with_err(q[$var_test], q[$print_err]); my @list_test = qw(This is a test list); print_list_with_err(q[@list_test], q[$print_err]); # Print variable with error messages sub print_var_with_err { my ( $a, $b ) = @_; my $a_eval = ${peek_above $a}; my $b_eval = ${peek_above $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 = @{peek_above $c}; my $d_eval = ${peek_above $d}; while ( my ( $index, $elem ) = each @c_eval ) { say $c . q{[} . $index . qq{]:\n} . $elem . qq{\n} or croak( $d_eval . $ERRNO ); } return; }
Better still would be to actually pass the subs the values they need as arguments, so they don't need to peek at their caller's variables...
#!/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 string'; print_var_with_err(q[$var_test], $var_test, q[$print_err], $print_err) +; my @list_test = qw(This is a test list); print_list_with_err(q[@list_test], \@list_test, q[$print_err], $print_ +err); # Print variable with error messages sub print_var_with_err { my ( $a, $a_eval, $b, $b_eval ) = @_; 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, $c_eval, $d, $d_eval ) = @_; while ( my ( $index, $elem ) = each @$c_eval ) { say $c . q{[} . $index . qq{]:\n} . $elem . qq{\n} or croak( $d_eval . $ERRNO ); } return; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Perl::Critic and Subroutines
by daugh016 (Initiate) on Nov 28, 2012 at 20:46 UTC |