in reply to Perl::Critic and Subroutines

'Useless interpolation of literal string' means that you're using double-quoted strings when single-quoted would do.

'Expression form of "eval"' means you're using stringy eval (i.e. eval "code") rather than block eval (eval { code }).

By the way, what you're doing (passing around names of variables and evaling them) is kinda dumb. Notice how this doesn't work:

sub define_var_and_print_it { my $xyz = "hello"; print_var_with_err("\$xyz", "\$xyz"); }
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: Perl::Critic and Subroutines
by daugh016 (Initiate) on Nov 27, 2012 at 22:26 UTC

    For both of the issues, I have tried to do your recommendation but then the functions stopped working. The was the problem I was having-- I cannot implement the recommendations without losing the functionality. Are there better ways to get the same output without the sloppy coding I have done?

      "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.

      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...

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Thank you so much! This has helped tons!