in reply to Perl::Critic and Subroutines

I cannot figure out how to fix these issues.

Buy the book, then you might stand a chance :)

$ pm_which Perl::Critic C:\perl\site\5.14.1\lib\Perl\Critic.pm $ ack "Useless interpolation of literal string" C:\perl\site\5.14.1\li +b\Perl\Critic C:\perl\site\5.14.1\lib\Perl\Critic\Policy\ValuesAndExpressions\Prohib +itInterpolationOfLiterals.pm 24:Readonly::Scalar my $DESC => q{Useless interpolation of literal str +ing};

perldoc Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals

I am not a fan of this policy :)

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?

What is the purpose of these functions? Your use of string-eval could be critical :)

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

    "I am not a fan of this policy :)"

    There seems little point to it. There is a very slight compile time speed penalty to double-quoted strings. This benchmark demonstrates that:

    use Benchmark ':all'; cmpthese(250_000, { e_double => sub { eval q{"foo"} }, e_single => sub { eval q{'foo'} }, });

    But at runtime, there's no difference, as they compile to the same optree...

    $ perl -MO=Concise -e'$x=q{foo}' > single.txt -e syntax OK $ perl -MO=Concise -e'$x=qq{foo}' > double.txt -e syntax OK $ diff -s single.txt double.txt Files single.txt and double.txt are identical $ rm single.txt double.txt

    That is not always the case in all programming languages though. The PHP compiler is less smart than the Perl compiler, so in PHP there's a real speed penalty on double-quoted strings...

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

      "I am not a fan of this policy :)"

      There seems little point to it.

      I actually think this is one of the best policies. As you pointed out, the performance benefits are very small. But it is immensely helpful for clarifying intent and detecting typos. Consider this code:

      my $name = <STDIN>; print "Hello, name";

      Unless you had test coverage on that (or you just spotted it by eye) you'd never find that bug. But ProhibitUselessInterpolation will find it for you.

Re^2: Perl::Critic and Subroutines
by daugh016 (Initiate) on Nov 27, 2012 at 22:22 UTC

    The point of them is that I am tying to avoid having to comment and uncomment multiple lines of print code. Specifically, the list one is 3 lines and I am doing this multiple times with testing. Also, I like to have informative prints where I have the variable/list name and if it is a list , I like to have a label for each element.

      The point of them is that I am tying to avoid having to comment and uncomment multiple lines of print code ...

      So you're writing some kind of developer aid, like Smart::Comments?

      eval probably won't work the way you want -- try PadWalker or Data::Dumper::Names

      The point of them is that I am tying to avoid having to comment and uncomment multiple lines of print code ...

      FWIW, that ought to be as simple as calling Ctrl+Q repeatedly (well if you have SciTE, different keyboard shortcuts for different editors)