http://www.perlmonks.org?node_id=747124

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).

Replies are listed 'Best First'.
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 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 ikegami (Patriarch) 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 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

      For me it’s preferable to print 90% of the time. One example in particular comes to mind–

      say join ", ", @some_bunch_of_stuff; # vs this which I find incredibly annoying– print join(", ", @some_bunch_of_stuff), "\n";
      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 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.

      Six less keystrokes!

      Using the 18 keystrokes in use feature 'say'; somewhat negates the saving really...

      I never think to use say - it seems as easy to use print and add a newline myself if I need one!

        You never have to type boilerplate if you use a template for new scripts. Command line is even better–

        perl -E 'say "What you will."'
Re: puts vs say
by Anonymous Monk on Feb 28, 2009 at 12:06 UTC
    Because its called say not puts