cyber-guard has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks
I am porting some perl v5.10 to v5.8, and one annoying thing is replacing all the say functions with print and a new line; isn't there a way to define my own say function to use it without brackets, just like you use it in perl v5.10.
Cheers

Replies are listed 'Best First'.
Re: replacing say function
by bart (Canon) on Feb 11, 2011 at 21:33 UTC
    sub say { local $\ = "\n"; print @_ ? @_ : $_; }
    That will not work if you ever use a file handle as the first argument (without comma), though.

    And, uh... there's also Perl6::Say

      I like bart's (edited to correct name) solution except you may want to add a prototype so you can call the sub without parentheses around the arguments.

      #!/usr/bin/perl use strict; use warnings; use 5.008; sub say (@){ local $\ = "\n"; print @_ ? @_ : $_; } say 'Hey';
      >>>Update>>>: On further testing, I guess you don't need a prototype. The trick is to define the sub above the statement where you call it. The following works just as well for me:
      #!/usr/bin/perl use strict; use warnings; use 5.008; sub say{ local $\ = "\n"; print @_ ? @_ : $_; } say 'Hey';
        You don't need a prototype for that. All you need to do is to define the sub before you call it — or put it in a module that you use. Same thing.
        Sweet! Thanks, all done now :)
      And it won't work for a say without arguments when a lexical $_ is in effect. But your say will work in 99.5% of the cases.
        Since lexical $_ was new in Perl 5.10, I don't think there is a problem with that.
Re: replacing say function
by chromatic (Archbishop) on Feb 11, 2011 at 22:10 UTC
    I am porting some perl v5.10 to v5.8...

    Can you use App::perlbrew to stick with a Perl 5 that has support?

    If not, feel free to tell your client/boss that you're taking on unnecessary risk by mandating the use of an obsolete and abandoned product.

      Thank you for that, very handy, unfortunately the project is for a large web host company, and the admins are just reluctant to upgrade, whatever the reason may be...