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.
| [reply] |
Re: puts vs say
by chromatic (Archbishop) on Feb 28, 2009 at 18:26 UTC
|
| [reply] |
Re: puts vs say
by ikegami (Patriarch) on Feb 28, 2009 at 19:11 UTC
|
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". | [reply] [d/l] [select] |
|
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 (κς,πμ,πλ)
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
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.
| [reply] |
|
say join ", ", @some_bunch_of_stuff;
# vs this which I find incredibly annoying–
print join(", ", @some_bunch_of_stuff), "\n";
| [reply] [d/l] [select] |
|
It was ported from perl6, so it does what perl6 say does.
| [reply] |
|
I get it. I didn't say it does chomp, I said it should chomp.
| [reply] |
|
|
|
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 $, = " "). | [reply] [d/l] [select] |
Re: puts vs say
by CountZero (Bishop) on Mar 01, 2009 at 11:40 UTC
|
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
| [reply] [d/l] [select] |
|
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. | [reply] |
|
| [reply] [d/l] [select] |
|
> Using the 18 keystrokes in use feature 'say'; somewhat negates the saving really...
c:\>perl -e"use 5.10.0; say 'works'"
works
Specifying a minimal version activates many "newer" features, not only say
see https://perldoc.perl.org/feature#IMPLICIT-LOADING for more.
| [reply] [d/l] [select] |
|
perl -E 'say "What you will."'
| [reply] [d/l] |
|
Re: puts vs say
by Anonymous Monk on Feb 28, 2009 at 12:06 UTC
|
Because its called say not puts | [reply] |