Search code examples
perlfilebashsearch

How can I use 'say' with Perl 5.8.8?


I'm assigned to a project (PHP/MySQL) that needs some review and possibly fixes.

As a part of it it is planned to check correctness of all variables that we get via GET and POST. We have a bunch of PHP (.php) and Smarty (.tpl) files that have several thousands of lines of code all together, so it would be painful to search for all $_GET[...] and $_POST[...] references manually. So I tried doing this:

find . -name "*.php" -or -name "*.tpl" |
xargs perl -ne 'use feature say; say $1 while m/(\$_(GET|POST)\[[\s]*[\S]+[\s]*\])/g;' |
sort -u

Basically it creates a list of all $_GET[...] and $_POST[...] references in the project, and then deletes the repeated values from it. But it didn't work, because I have Perl 5.8.8 on my development machine, which does not support 5.10+ feature 'say', and our system administrator said that upgrade is undesired. I'm not sure why, but he's the boss.

So, is there a way to replace 'say' with some other code, or maybe even replace Perl with another tool?


Solution

  • Perl 5.10 added 'say', which is a substitute for 'print', that automatically adds a newline to the output. Hence you can write

    say "hello";
    

    ...instead of having to write:

    print "hello\n";
    

    Simply drop the 'use feature say;' and replace 'say $1' with:

    print "$1\n";