Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer


by tlhf (Scribe)
on Jun 07, 2002 at 17:47 UTC ( #172587=perlmeditation: print w/replies, xml ) Need Help??


Well, this post seems to have been downvoted to hell, especially the replies, with repercussions of my posts from months ago also being modded down. Even the ones to off topic threads. I will conceed to the will of and retire it.

Replies are listed 'Best First'.
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.
      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
Re: println
by jepri (Parson) on Jun 08, 2002 at 05:18 UTC
    I miss being able to just type '?' and have it expand to 'print'. Oops. That's really showing where I learnt to program. :)

    I didn't believe in evil until I dated it.

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



      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.
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.
Re: println
by stefp (Vicar) on Jun 07, 2002 at 21:52 UTC
    I use:

    sub prtln { local $,=""; print +( @_ ? @_ : $_ ), "\n" } sub prtlns { local $,="\n"; print +( @_ ? @_ : $_ ), "\n" }

    -- stefp -- check out TeXmacs wiki

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.

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

        say was added in 5.10.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: println
by Anonymous Monk on Jun 07, 2002 at 18:03 UTC
    a simple
    sub println { local $\ = "\n"; print @_; }
    would work too, with the added benefit of not actually being broken.
      Getting feisty, aren't we?

      While I don't much see the use for an internal function named println, which reminds me of Pascal of all things, how about doing it correctly?
      sub println { print ((@_? join($/, @_) : $_), $/); }
      Not everyone uses UNIX, BeOS, or AmigaOS, so putting a literal "\n" in there is simply bad form.

      As Dog and Pony points out, "\n" does represent a logical newline, as it is quietly and magically transformed into the appropriate value before being output. One could argue, though, that using $/ eliminates four shifted keystrokes.
        According to perlport, \n represents a logical newline, and thus (often) actually does the right thing across platforms.

        That said, nothing wrong with erring on the side of safety. :)

        You have moved into a dark place.
        It is pitch black. You are likely to be eaten by a grue.
    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


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://172587]
Approved by sparkyichi
Front-paged by stefp
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2021-07-29 19:30 GMT
Find Nodes?
    Voting Booth?

    No recent polls found