[comp.lang.perl] keeping the text-format when using write

koerberm@nixsin.UUCP (Mathias Koerber) (07/20/90)

Howdy,

I just got perl running on my machine, and it really seems to be a fine thing.
Just now I'm trying to write my first perl-script, and End-of-Day script
which else i would have to write in sh (C is to inflexible when it comes
to easily adding another procedure ..)

Even better, I can keep a nice log using the format and write. But there my
first problem begins.

I try to do something like this:

format SPOOL =
@<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$time, $description,                     $result
~~     ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
       $description,                     $result
.

then, when it comes to show an error, I do this:

$result = <<"EOT";
There was an error in function xxxxx. This may be due to different reasons
	a) .....
	b) .....
	c) .....

remedies:
	a)...
	b)...
	c)...
EOT
	write SPOOL;


My problem is, that write squeezes as much as possible into the text, getting
rid of my nice indents, newlines etc.
I tried setting 
	$: = "\n";
so that write has to break at the newline, without success.
I don't care if it breaks ALSO where the line is full, but 'd like it to
break on newlines and keep tabs whenever it finds them.

Is there any way to do this?

As I said, I am so green on perl, you could call me a cucumber :-)

regards, Mathias.

PS: My only documentation for perl is the man-pages. Can anyone recommend a
	good book that someone like me, who is more (not *very*) proficient on
	C,sh and the likes, can understand?

MK

-- 
Mathias Koerber           |Tel:   +65 / 7473828 ext 1852|Fax: +65 / 7474331
Nixdorf Computer Singapore|EUnet: koerber.sin@nixpbe    |nerv:  koerber.sin
2 Kallang Sector          |uunet: uunet!linus!nixbur!koerber.sin
Singapore 1334            |[Standard-disclaimer:All views personal...     ]

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/21/90)

In article <1104@nixsin.UUCP> koerberm@nixsin.UUCP (Mathias Koerber) writes:
: I try to do something like this:
: 
: format SPOOL =
: @<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
: $time, $description,                     $result
: ~~     ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
:        $description,                     $result
: .
: 
: then, when it comes to show an error, I do this:
: 
: $result = <<"EOT";
: There was an error in function xxxxx. This may be due to different reasons
: 	a) .....
: 	b) .....
: 	c) .....
: 
: remedies:
: 	a)...
: 	b)...
: 	c)...
: EOT
: 	write SPOOL;
: 
: 
: My problem is, that write squeezes as much as possible into the text, getting
: rid of my nice indents, newlines etc.
: I tried setting 
: 	$: = "\n";
: so that write has to break at the newline, without success.
: I don't care if it breaks ALSO where the line is full, but 'd like it to
: break on newlines and keep tabs whenever it finds them.
: 
: Is there any way to do this?

Well, you could do it something like this:

format SPOOL =
@<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$time, $description,                     $result = $result || shift(@result)
~~     ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
       $description,                     $result = $result || shift(@result)
.

@result = split(/\n/,<<"EOT");
There was an error in function xxxxx. This may be due to different reasons
	a) .....
	b) .....
	c) .....

remedies:
	a)...
	b)...
	c)...
EOT
for (@result) { s/\t/        /; s/^$/./; }

write SPOOL;

Note that it turns a blank line into a . to fool the ~~.  Otherwise it
doesn't print out the whole array.

You could also use a subroutine:

format SPOOL =
@<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$time, $description,                     $result = &load
~~     ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
       $description,                     $result = &load
.

@result = split(/\n/,<<"EOT");
There was an error in function xxxxx. This may be due to different reasons
	a) .....
	b) .....
	c) .....

remedies:
	a)...
	b)...
	c)...
EOT

sub load {
    $result .= shift(@result) if $result eq '';
    $result =~ s/\t/    /g;		# shorten tabs to 4 spaces
    $result = '.' if $result eq '' && @result;
    $result;
}

write SPOOL;

Or something like that.  You've still got the problem that you might want
to ignore some newlines and not others.

You could in fact just load up an array the way you want, with the lines
split just where you want, and then just use regular @ fields with a shift.
Nothing says you have to use ^ fields just because they're there, if they
don't do what you want.

Larry