Re: println
by Dog and Pony (Priest) on Jun 07, 2002 at 19:10 UTC
|
I wonder, wouldn't this be more perlish?
#!/usr/bin/perl -w
use strict;
sub println(@)
{
print map{"$_\n"} @_;
}
println 'Foo', 'Bar';
println 'Baz';
Since print accepts an array of things to print, you'd think that the Perl println would provide a shortcut to adding a newline to each element of the array. :)
That said, I've not really missed this shortcut (even though I have a java background), but if it was added, I might just use it.
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue. | [reply] [d/l] |
|
|
Well, personally I'm a big fan of print()ing lists, so I'm apprehensive about changing my beloved println. For example, I'm always doing; print($x, $y, $z). Although, now you've stated the idea of printing the list as multiple lines, it's suddenly become very tempting. It's not really println (print-line), but more of a printlns (print-lines)...
I see this slightly camp module becoming bloated :D
| [reply] |
Re: println
by jepri (Parson) on Jun 08, 2002 at 05:18 UTC
|
| [reply] |
|
|
Heh, nostalgia...
Can't say I really miss it, but pressing P on the ZX81 for PRINT was not only handy, but the only way of typing statements :)
Less handy was the inability to save my programs on the tape recorder :)
/J
| [reply] |
|
|
jepri++
I miss that too! What I don't miss, however is editing program lines "live" on the command line, as it where... where all lines starting with a number was interpreted as part of your (only possible) program... you did LIST on the program and went up and down though "history" resubmitting lines to make changes... ;-)
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.
| [reply] |
Re: println
by ignatz (Vicar) on Jun 08, 2002 at 12:07 UTC
|
Grr... Here I was thinking ya know, now that I think about it, that's right; all those \ns do get on my nerves, and then you go and delete your post. Stand behind your words. No point in talking if you don't stand behind what comes out.
()-()
\"/
`
| [reply] |
Re: println
by stefp (Vicar) on Jun 07, 2002 at 21:52 UTC
|
sub prtln { local $,=""; print +( @_ ? @_ : $_ ), "\n" }
sub prtlns { local $,="\n"; print +( @_ ? @_ : $_ ), "\n" }
--
stefp -- check out TeXmacs
wiki | [reply] [d/l] |
Re: println
by Juerd (Abbot) on Jun 08, 2002 at 15:43 UTC
|
There is a reason for there not being a println function in Perl. It is bad to have two functions that do exactly the same but with a small change. There is int() but no ceil(), floor(), fix(), round() etcetera, as all of those can be made using int(). POSIX has some of these, though.
Refactoring code to save only a few keystrokes also makes your code less readable. Suppose you have five listing functions that do the same, but format their output a little different. It would probably be a good idea to have a single function that takes an extra argument instead of five separate functions.
There really isn't an easy way to create your own println, as there is (correct me if I'm wrong) no way to emulate prints "print HANDLE LIST" syntax. Well, a source filter might take you there, but that will never be exactly the same.
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
| [reply] |
|
|
I realize this is an old thread, but I was asking myself the question, "Why is println not built into the perl language?". I came upon this post, and I still don't feel I have an answer.
Is this the real reason that println is not a builtin function? Then why is there a printf function? Based on the documentation, printf is equivalent of calling print with parameters.
http://perldoc.perl.org/5.10.0/functions/printf.html
"Equivalent to print FILEHANDLE sprintf(FORMAT, LIST) ..."
Perl is a language that provides one with many options to accomplish a single goal, so your reasoning makes little sense to me. I personally would love to see println added as a builtin function. I don't see any problems with a little syntactic sugar if it helps developers to be more productive.
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: println
by Anonymous Monk on Jun 07, 2002 at 18:03 UTC
|
sub println {
local $\ = "\n";
print @_;
}
would work too, with the added benefit of not actually being broken.
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: println
by thelenm (Vicar) on Jun 08, 2002 at 00:14 UTC
|
Another simple way to get a newline each time you print is to use perl's -l command-line switch. But then again, it will be more difficult to print anything without a newline if you ever want to (though I suppose you could do it by monkeying with $\).
-- Mike
--
just,my${.02} | [reply] |