in reply to code file refactoring to use English module as per PBP

I'm not sure that there is a 'p' modifier for regular expressions (did you mean to use the '-p' command-line switch?) and there is no point in using the 'x' modifier if you don't use extended syntax. This can be done in a one-liner like this.

$ mkdir lloderl74 $ cat > lloderl74/script1 #!/usr/bin/perl -w print "Hello World!\n"; exit; $ cat > lloderl74/script2 #!/usr/bin/perl -w die "Goodbye, Cruel World\n"; $ perl -pi.bak -e ' s{^(#!/usr/bin/perl.*)}{$1\n\nuse English q{no_match_vars}}' lloderl74 +/* $ head -99 lloderl74/* ==> lloderl74/script1 <== #!/usr/bin/perl -w use English q{no_match_vars} print "Hello World!\n"; exit; ==> lloderl74/script1.bak <== #!/usr/bin/perl -w print "Hello World!\n"; exit; ==> lloderl74/script2 <== #!/usr/bin/perl -w use English q{no_match_vars} die "Goodbye, Cruel World\n"; ==> lloderl74/script2.bak <== #!/usr/bin/perl -w die "Goodbye, Cruel World\n";

I hope this helps you.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: junior application developer
by lloder174 (Novice) on Apr 29, 2009 at 22:06 UTC
    Thanks, johngg. This unfortunately does not help because of my wanting to avoid the command line and I also want to adhere to Perl Best Practices, (PBP)! I have changed the $INPUT_RECORD_SEPARATOR to undefined on purpose so that I may slurp in an entire file at a time, because this code is a small part of a script and this is more efficient than rewriting the file a line at a time. If you'll notice, I am using the English module within this code and I originally used ${^MATCH}, (which requires the /p expression modifier), instead of $1 but it also did not work! Help still needed! P.S. My apologies for a mistakenly created subject line which does not really help!
      You shouldn't need to use PBP for one-liners - that defeats the point. And if you're so worried about it, shouldn't you also be using strict and warnings? In that case, you would see the compilation error

      Bareword "no_match_vars" not allowed while "strict subs" in use at ...

      promptly fix it with use English qw( -no_match_vars ) and be on your merry way. :)

      Update - added the qw (thanks moritz).

      junior application developer
      lloder174: You can go back and Update the title of your post (making a note that you have done so), and you can at the same time fix the problem that your code does not have a valid  </code> end tag, which renders it almost unreadable.

      johngg: I believe the  //p regex modifier is new with 5.10: see p in perlre.

        Thank you for that pointer, I'm still mostly using 5.8.x on Solaris boxes so I hadn't encountered it before. I wish that O'Reilly would do an update of the Camel book for 5.10!

        Cheers,

        JohnGG