in reply to Re: Perl::Critic and Subroutines
in thread Perl::Critic and Subroutines

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?

Replies are listed 'Best First'.
Re^3: Perl::Critic and Subroutines
by tobyink (Canon) on Nov 28, 2012 at 08:17 UTC

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