Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

autoconverting print to say

by dlarochelle (Sexton)
on Nov 13, 2009 at 22:27 UTC ( #807070=perlquestion: print w/replies, xml ) Need Help??

dlarochelle has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if there is any tool that could go through code and replace print with say where appropriate.

For example,

print "foo\n"; should become say "foo"; or say 'foo';

Similarly print "$a\n" could become say "$a" or ideally just say $a

Manually making this type of change seems tedious and error prone. I was hoping for something similar to perltidy that could do it automatically.

P.S. If no tool like this exists, any thoughts and what it would take to hack something like this together?

Thanks in advance.

Replies are listed 'Best First'.
Re: autoconverting print to say
by GrandFather (Saint) on Nov 14, 2009 at 00:05 UTC

    Why? print is unlikely to go away and is no less readable than say. It does contain two more characters, but hard disks and monitors these days can probably cope with that excess.

    The risk if introducing errors or at least cosmetic changes in the output far outweigh the minor perceived cosmetic advantage of using say instead of print. If you have a compelling desire to make the change, do it piecemeal during maintenance as you visit code for other reasons.


    True laziness is hard work

      Nit: Typically 4 or 8 more characters.

      print "...\n"; say "...";1234 print ..., "\n"; say ...;12345678

      Your good point still applies, of course. There's also the point that using say lock you into 5.10+.

Re: autoconverting print to say
by Fletch (Bishop) on Nov 13, 2009 at 23:16 UTC

    Pardon the possibly obvious question, but . . . Why? Seems like a clear cut case of "ifn ain't broke don't frell with it". I mean aside from scoring points with the k00l k1dz for using the h3p n00 (*cough* sorry, it's out of my system now) style, what benefit is there?

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: autoconverting print to say
by chromatic (Archbishop) on Nov 13, 2009 at 22:46 UTC

    I convert this code by hand (or a Vim macro) only when I can verify that it's correct. Consider the case of a modified $\, for example; if you substitute print for say then, you'll change the behavior of the program.

      Chances are print "...\n" isn't used in conjunction with $/.

      I'd look for $/ to make sure it's not going to interfere, then use something like

      while (<>) { s/print(.*?)\\n("\)?;)/say$1$2/; s/(say.*?)[.,]\s*""(\)?;)/say$1$2/; print; }

      Then, I'd do a quick diff between the original and the modified file to verify the change. Don't forget to look for missed occurrences, including local $\ = "\n"; print "foo";

Re: autoconverting print to say
by ssandv (Hermit) on Nov 13, 2009 at 23:29 UTC
    I'd be somewhat leery of doing it unless I knew a lot about the code base. To wit:
    print " ",(join " \n",@list),"\n"; #one of many ways to indent a list print "$var\n" ; #replaced hardcoded 'print "hello\n";' line.
    are just two of many strange things people might do that could match a s/^print(stuff)\\n(more stuff)/say $1$2/; line oddly.
Re: autoconverting print to say
by Jorge_de_Burgos (Beadle) on Nov 14, 2009 at 07:02 UTC
    Since both print and say have output, and your main concern is avoiding alteration of that output when replacing one with the other, I think an automated replace should test your program's output (all variants of it) before and after the replace. If outputs don't differ, it means your automated replace has worked OK. If modified $/ (or other) bugs occur, a diff on outputs will scream out loud and clear!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://807070]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2023-03-24 15:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (61 votes). Check out past polls.

    Notices?