[comp.sys.amiga] why i like perl: an example

rminnich@metropolis.super.org (Ronald G Minnich) (08/08/88)

Since perl finally made its way to comp.binaries.amiga i thought
an example would be fun.
###########################begin perl example (fast grep)
#Reply-To: bob@dhw68k.cts.com (Bob Best)
#Organization: Wolfskill residence; Anaheim, CA (USA)

This perl script for grep(1) actually executed faster than the /bin/grep
on my SCO Xenix 386 system (case insensitive searches were slower.)
In addition, all the enhancements that Larry Wall added to Henry Spencer's
regular expression package are available.
See the perl manual page under "Regular Expressions" for details.
-------------cut here-------------------
#!/bin/sh
cat > grep.pl <<ENDOFITALL
# grep.pl - perl version of grep(1)
# written by bob best (bob@dhw68k.cts.com)
#
$match=1;
while ($_ = $ARGV[0], /^-/) {
	shift;
	/^-y/ && ($fold='i') && next;
	/^-v/ && ($match=0) && next;
	/^-c/ && ($count=1) && next;
	/^-n/ && ($line=1) && next;
	/^-l/ && ($fileonly=1) && next;
}
$pattern=$ARGV[0]; shift;
++$multifile if $#ARGV > 0;

dogrep: {

if ($match && $fileonly) {
	eval "while (<>) { if (/$pattern/$fold) {
		print \"\$ARGV\\n\"; close(ARGV);} } ";		
	last dogrep;
}

if ($match && $count && $multifile) {
	eval "while (<>) { ++\$cnt if /$pattern/$fold;
		if (eof) { print \"\$ARGV:\$cnt\\n\"; \$cnt=0;} } ";
	last dogrep;
}

if ($match && $count) {
	eval "while (<>) { ++\$cnt if /$pattern/$fold;} print \"\$cnt\\n\"; ";
	last dogrep;
}

if ($match && $multifile && $line) {
	eval "while (<>) { print \"\$ARGV:\$.:\$_\" if /$pattern/$fold;
		close(ARGV) if eof; } ";
	last dogrep;
}

if ($match && $multifile) {
	eval "while (<>) { print \"\$ARGV:\$_\" if /$pattern/$fold; } ";
	last dogrep;
}

if ($match && $line) {
	eval "while (<>) { print \"\$.:\$_\" if /$pattern/$fold;} ";
	last dogrep;
}

if ($match) {
	eval "while (<>) { print if /$pattern/$fold; } ";
	last dogrep;
}

if (!$match && $fileonly) {
	eval "while (<>) { unless (/$pattern/$fold) {
		print \"\$ARGV\\n\"; close(ARGV);} } ";		
	last dogrep;
}

if (!$match && $count && $multifile) {
	eval "while (<>) { ++\$cnt unless /$pattern/$fold;
		if (eof) { print \"\$ARGV:\$cnt\\n\"; \$cnt=0;} } ";
	last dogrep;
}

if (!$match && $count) {
	eval "while (<>) { ++\$cnt unless /$pattern/$fold;} print \"\$cnt\\n\"; ";
	last dogrep;
}

if (!$match && $multifile && $line) {
	eval "while (<>) { print \"\$ARGV:\$.:\$_\" unless /$pattern/$fold;
		close(ARGV) if eof; } ";
	last dogrep;
}

if (!$match && $multifile) {
	eval "while (<>) { print \"\$ARGV:\$_\" unless /$pattern/$fold; } ";
	last dogrep;
}

if (!$match && $line) {
	eval "while (<>) { print \"\$.:\$_\" unless /$pattern/$fold;} ";
	last dogrep;
}

if (!$match) {
	eval "while (<>) { print unless /$pattern/$fold; } ";
	last dogrep;
}

} #end of dogrep block
ENDOFITALL
exit 0
-- 
Bob Best
uucp: ...{trwrb,hplabs}!felix!dhw68k!bob	InterNet: bob@dhw68k.cts.com

ron