Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

puts vs say

by dgaramond2 (Monk)
on Feb 28, 2009 at 11:14 UTC ( #747124=perlquestion: print w/ replies, xml ) Need Help??
dgaramond2 has asked for the wisdom of the Perl Monks concerning the following question:

Just wondering why perl 5.10's new say() doesn't have a little bit of intelligence like Ruby's puts(), i.e. if the string to be printed already ends in a newline, it doesn't print additional newline. say() just add a newline no matter what. What was the rationale for this? I thought most people want puts()' behaviour most of the time (at least I do).

Comment on puts vs say
Re: puts vs say
by Anonymous Monk on Feb 28, 2009 at 12:06 UTC
    Because its called say not puts
Re: puts vs say
by jettero (Monsignor) on Feb 28, 2009 at 12:07 UTC
    I definitely agree with this. say() should be smart. What gain is there really but just adding a \n? Regular old print() is just fine if I have to keep track of the \n anyway.

    -Paul

      It was ported from perl6, so it does what perl6 say does.
        I get it. I didn't say it does chomp, I said it should chomp.

        -Paul

Re: puts vs say
by bruno (Friar) on Feb 28, 2009 at 13:38 UTC
    I would also appreciate it if say added a space or tab between elements, (a la  local $, = " " ).
Re: puts vs say
by chromatic (Archbishop) on Feb 28, 2009 at 18:26 UTC

    How would it know when people don't want the final line autochomped?

Re: puts vs say
by hangon (Deacon) on Feb 28, 2009 at 19:06 UTC

    No thank you. I would prefer that simple functions not make arbitrary decisions under the guise of "smartness". What if I want that extra newline? Using say is perfect for temporarily peppering code for debugging. As such, it's not helpful for it to have variable behavior. With say $foo, I want to be able to tell if $foo has a newline of its own.

    Then what about formatting text with blank lines? Does put chomp one or multiple newlines? What about whitespace between or after newlines? Do you really want to worry about behavior of a bunch of potential edge cases for what should be a simple function? When the convenience of say gives way to unnecessary complexity its usefulness diminishes.

Re: puts vs say
by ikegami (Pope) on Feb 28, 2009 at 19:11 UTC

    That means I'd have to use

    say("$var\n");

    to make sure say prints what I want it to print. That's pretty dumb because the whole point of say is to remove the need to specify "\n".

      ikegami, you and CountZero below got the original question wrong. The case is that, in ruby,
      puts "a\n"
      outputs the same as
      puts "a"
      And that is: a followed by a newline.
      The question is: why doesn't say does the same? I happen to think that autochomping-before-printing-and-then-printing-a-newline is an interesting piece of DWIMery (if one really wants an a followed by two newlines, puts "a\n\n" always works. Of course,
      sub puts { local $_ = pop; chomp; push @_, $_; goto &say }
      should also work :-)
      []s, HTH, Massa (κς,πμ,πλ)

        You have not changed my understanding of anything. One of us hasn't made their point clearly enough, and I'm guessing it's me.

        Most of the time, I generate the text I output. I don't want it to be modified by my output function. When using puts, the only way to guarantee the text I generated will be output with a newline added is to add one myself.

        say($var); # Newline added. That's why I use "say". puts($var); # Newline maybe added. Useless for text I generated. puts("$var\n"); # Newline added, but purpose defied.

        Yes, they are uses for puts, but there are for say too. Don't change say, add puts.

        But that's not to say ruby's puts makes sense either. Since the goal of puts is to prevent third parties from messing the output, it makes no sense for puts to chomp just one newline. It should remove all but one, adding one if necessary.

Re: puts vs say
by CountZero (Bishop) on Mar 01, 2009 at 11:40 UTC
    The rationale is simple:
    print "$hello\n";
    say $hello;
    Six less keystrokes!

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      But puts() saves even more keystrokes. *Aside* from the above case, there's also the (quite common):

      print $hello, ($hello =~ /\n$/ ? "" : "\n");
      
      puts $hello;

      Anyway, I don't want to start a flamewar here. Obviously Larry et al didn't think puts-like behaviour attractive enough to make say() behave the same. Oh well. We can always define puts() ourselves.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-08-01 02:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls