Newsgroup: comp.lang.perl


Path: doc.ic.ac.uk!dds
From: dds@doc.ic.ac.uk (Diomidis Spinellis)
Newsgroups: comp.lang.perl
Subject: New Perl structure plea
Date: 23 Nov 1993 12:32:22 GMT
Organization: Dept of Computing, Imperial College, England
Lines: 47
Distribution: world
Message-ID: <2csvsm$2ka@frigate.doc.ic.ac.uk>
NNTP-Posting-Host: scorch.doc.ic.ac.uk
Content-Length: 1568
Ever since I started working with Perl 3 I found a number of tasks
that were easier to implement using the Bourne shell (sh).  All of
them were structured as a set of processes piping into each other.
I know about open(.., "| process"), and that I could do the same
thing forking Perl, but I think this would be inefficient.  The ideal
solution would be to allow a pipe construct between Perl statements
as in:

# Preliminary processing; remove comments and merge continuation lines
while (<>) {
	chop;
	# Remove comment and blank lines
	next if (/^#/ || /^$/);
	# Handle \ continuation lines
	if (s/\\$//) {
		$line .= $_;
		next;
	} else {
		$line = $_;
	}
	print $line;
	$line = '';
} | # <-- Notice pipe symbol
# Main program processing
while (<>) {
	# More processing
}

I have the impression that the new organisation of Perl 5 would make
such a structure relatively easier to implement.  The semantics are
obvious: statements that start or end with a | get executed as coroutines
whose stdin/stdout are replaced by the stdin/stdout of the previous/next
coroutine.

Diomidis

#!/bin/sh
(
        look ju | grep st | head -1 | tr j J
        look ano | grep the
        echo -n P
        grep meyer /usr/dict/words | sed "s/\(...\).*/\1/" | tr E-Z e-z
        look Ack | grep man | sed "s/man//;s/A/ha/"
) | perl -e 'print join(" ", grep(chop, <>)) , ".\n"'
-- 
Diomidis Spinellis    Internet: <dds@doc.ic.ac.uk>  UUCP: ...!uknet!icdoc!dds
Department of Computing, Imperial College, London SW7     #include "/dev/tty"



Newsgroup comp.lang.perl contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.