[comp.lang.perl] Newlines in prints

worley@compass.com (Dale Worley) (02/20/91)

I'd like a simpler way to end a print line with a newline than
appending
	, "\n"
to the print.  I looked at what it might take to make a naked
	\n
at the end of a print mean "\n", but a naked \n is treated as the
symbol 'n' -- not too promising.  And you can hardly use "/" as a
separator in place of "," to mean "add newline", as you do in Fortran.
Perhaps what I really want is to go back to Basic-Plus, where the
newline was implicit unless the print was ended with ";".

Dale

Dale Worley		Compass, Inc.			worley@compass.com
--
Morning amnesia -- Nature's way of keeping you from waking up screaming.
-- "Dilbert"

tchrist@convex.COM (Tom Christiansen) (02/20/91)

From the keyboard of worley@compass.com:
:I'd like a simpler way to end a print line with a newline than
:appending
:	, "\n"
:to the print.  

$\ = "\n";

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/20/91)

In article <1991Feb20.000312.5870@uvaarpa.Virginia.EDU> worley@compass.com writes:
: I'd like a simpler way to end a print line with a newline than
: appending
: 	, "\n"
: to the print.  I looked at what it might take to make a naked
: 	\n
: at the end of a print mean "\n", but a naked \n is treated as the
: symbol 'n' -- not too promising.  And you can hardly use "/" as a
: separator in place of "," to mean "add newline", as you do in Fortran.
: Perhaps what I really want is to go back to Basic-Plus, where the
: newline was implicit unless the print was ended with ";".

Well, you can save two characters by defining $n = "\n", but it sounds
more like you want to set $\ to "\n" and then have a subroutine for
printing without the newline:

	sub nprint { local($\) = ''; print @_; }

Incidentally, along these same lines, I just added a switch to do
autochopping and auto-$\-setting, so the colrm 81 program can be
emulated now with

	perl -lpe 'substr($_,80) = ""'

Larry

worley@compass.com (Dale Worley) (02/20/91)

   From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall)

   Well, you can save two characters by defining $n = "\n", but it sounds
   more like you want to set $\ to "\n" and then have a subroutine for
   printing without the newline:

This leaves various problems, mostly that setting $\ makes adding the
newlines mandatory.  Also, a subroutine for printing won't have the
same syntax as "print".  What I want is a quick-n-easy punctuation
mark that can be inserted at the end (and preferably anywhere) in the
list argument of "print" that means "insert an element into the list
here which is a string containing a newline".

Of course, all the punctuation marks are taken up.  :-(

Perhaps we need to add a new punctuation digraph!

Dale

Dale Worley		Compass, Inc.			worley@compass.com
--
A real man is someone whose .login contains "exec emacs".

tchrist@convex.COM (Tom Christiansen) (02/21/91)

From the keyboard of worley@compass.com:
:   From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall)
:
:   Well, you can save two characters by defining $n = "\n", but it sounds
:   more like you want to set $\ to "\n" and then have a subroutine for
:   printing without the newline:
:
:This leaves various problems, mostly that setting $\ makes adding the
:newlines mandatory.  Also, a subroutine for printing won't have the
:same syntax as "print".  What I want is a quick-n-easy punctuation
:mark that can be inserted at the end (and preferably anywhere) 

I don't see what a special token, like NL (or your own $N), would gain you
over "\n".  It doesn't seem worth it.  Modulo file-handles not getting
commas, &print and &println functions would look just like print's but
with parens, wouldn't they?

    sub print   { print @_; }
    sub println { print @_, "\n"; }

Tacking on a newline doesn't seem to my mind enough hassle to justify 
further obfuscating the language with another print function or a magic 
token or variable.  

    print "this is a foo: ", $foo;
    print "this is a newline foo", $foo, ;  # <- trailing comma trigger NL

This sounds too much like BASIC, which perl really isn't.  That comma is 
hard to see, and anything bigger undoes any convenience.

Now, swrite may be a whole nother matter.

--tom
-- 
Tom Christiansen		tchrist@convex.com	convex!tchrist
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/21/91)

In article <1991Feb20.095234.13843@newcastle.ac.uk> des0mpw@colman.newcastle.ac.uk (M.P. Ward) writes:
: How about a "println" statement? Larry?

No.

Larry

rbj@uunet.UU.NET (Root Boy Jim) (02/21/91)

In <1991Feb20.155958.8027@uvaarpa.Virginia.EDU> worley@compass.com writes:

>What I want is a quick-n-easy punctuation
>mark that can be inserted at the end (and preferably anywhere) in the
>list argument of "print" that means "insert an element into the list
>here which is a string containing a newline".

Dale, you are too smart to seriously consider garbage like this.

Take your own advice and define a variable if you need to.

Perl has take the position that newlines are neither created nor destroyed.

Only the programmer or user can make them appear or disappear.

We like it that way.
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

rbj@uunet.UU.NET (Root Boy Jim) (02/21/91)

tchrist@convex.COM (Tom Christiansen) writes:
>
>Now, swrite may be a whole nother matter.

It certainly is.

I have often wished that fopen would allow you to treat a piece of
memory as a file. Sprintf/sscanf sort of do this, but only
temporarily. They grab an unused FILE struct, or _IOSTRNG into
the flags word, set the length to a large (32k) number, then
call fprintf/fscanf to do the work.

So the question is, how do we (further) overload the open function
to allow the creation of file handles which refer to strings? In
fact, to get LISPish, how do we create a filehandle which gets/puts
from/to a user defined subroutine?

At the risk of stealing from the legal filename user space

	open(FH,'$STRING')	opens $STRING for IO
	open(FH,'&FUNC')	calls &FUNC to get/put each char

So how do we know whether to read or write?
By the context the file handle is used in.

Functions are expected to return a character if given no argument,
else put the character they're given. Optionally, they could be
given a RW flag to tell them what to do.

Strings would grow and shrink as needed, and would be seekable.
One interesting way of seeking would be by matching. The seek
pointer would be set to the beginning of $1 if parens were used.

Both strings and functions would always be open for both read and write.
This solves the problem of '>&FUNC' conflicting with '>&2'.

One could select these kinds of FH's for writing. No swrite needed.
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

rodger@hpdmd48.boi.hp.com (Rodger Anderson) (02/22/91)

>/ hpdmd48:comp.lang.perl / worley@compass.com (Dale Worley) /  5:03 pm  Feb 19, 1991 /
>I'd like a simpler way to end a print line with a newline than
>appending
>	, "\n"
>to the print.

Maybe I completely misunderstood your question, but it seems you could
do this near the top of your script;

$\ = "\n";

Then, all your print statements will output a "\n" at the end.

-- 
Rodger Anderson (rodger@hpdmmef.boi.hp.com)
              or(rodger%hpdmmef@relay.hp.com)