[comp.lang.perl] internal pipes? chaining functions?

eichin@athena.mit.edu (Mark W. Eichin) (09/26/90)

I've got a script which I was able to express conveniently as
	tr \\015 \\012 | 
	sed -e 's/^[/^[F\n' -e 's/\(%CO:.,[0-9]*,[0-9]*%\)/\1\n/' |
	perl mksc.perl

(where mksc is a while(<>) { if(//)..elsif(//)..else.. } script.) I
expect there is a convenient way of expressing the whole thing in
perl, but I'm not quite sure what it is... Essentially, I'd like to
have the nicer features (like <>) in later stages be able to operate
on the result of earlier ones. Is there a "model" that I'm missing
here, or is this just messy? I know I could do this with three perl
processes easily enough. Going from that to one is the important part.
				_Mark_ <eichin@athena.mit.edu>

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (09/27/90)

In article <1990Sep26.011749.2999@uvaarpa.Virginia.EDU> eichin@athena.mit.edu writes:
: I've got a script which I was able to express conveniently as
: 	tr \\015 \\012 | 
: 	sed -e 's/^[/^[F\n' -e 's/\(%CO:.,[0-9]*,[0-9]*%\)/\1\n/' |
: 	perl mksc.perl
: 
: (where mksc is a while(<>) { if(//)..elsif(//)..else.. } script.) I
: expect there is a convenient way of expressing the whole thing in
: perl, but I'm not quite sure what it is... Essentially, I'd like to
: have the nicer features (like <>) in later stages be able to operate
: on the result of earlier ones. Is there a "model" that I'm missing
: here, or is this just messy? I know I could do this with three perl
: processes easily enough. Going from that to one is the important part.

tr and sed commands are easily translated to the corresponding Perl commands
(disregarding the fact that your first sed substitution is missing its
trailing slash).

	while (<>) {
	    tr/\015/\012/;
	    s/^\[/^[F\n/;
	    s/(%CO:.,\d*,\d*%)/$1\n/;
	    if (//) ...
	    elsif (//) ...
	}

Or something like that, depending on whether there are line feeds after the
carriage returns.  If not, then you probably just want:

	$/ = "\015";		# Set input delimiter.
	while (<>) {
	    s/\r$/\n/;		# Or just chop it.
	    s/^\[/^[F\n';
	    s/(%CO:.,\d*,\d*%)/$1\n/;
	    if (//) ...
	    elsif (//) ...
	}

Larry

eichin@athena.mit.edu (Mark W. Eichin) (09/27/90)

I wrote
: I've got a script which I was able to express conveniently as
:       tr \\015 \\012 | 
:       sed -e 's/^[/^[F\n' -e 's/\(%CO:.,[0-9]*,[0-9]*%\)/\1\n/' |
:       perl mksc.perl

Actually, Larry's right, I left out a slash after the first
expression, but I also neglected to clarify something: \n above is a
literal newline, as ^[ is a literal escape.
	The problem I specifically need to address is that both of the
sed expressions insert newlines. With the pipe, perl interprets them
as seperate lines. 
	It occurs to me that I can probably do something like
for (split(/\n/)) around the final perl script to be sure it gets
individual lines. That'll do.
				Thanks...		_Mark_