[alt.sources.d] xargs in Perl

merlyn@iwarp.intel.com (Randal Schwartz) (12/02/89)

In article <EMV.89Nov30010143@picasso.math.lsa.umich.edu>, emv@math (Edward Vielmetti) writes:
| 
|    Gentle reminder:  many versions of Berzerklix don't have xargs.
| 
|    ++Brandon
| 
| followed by the obvious,
| 
|   does anyone have a version of xargs in perl?

Yeah, I wrote one when the discussion came up in c.u.q, and began my
never-ending quest to post a Perl solution to nearly every problem
posted there. :-)

Lessee... where is it... <scruffle scruffle>... oh yeah, here it is:

================================================== snip snip
#!/usr/bin/perl

$lengthmax = 512;
@command = $#ARGV > -1 ? @ARGV : ("/bin/echo");
$lengthcommand = length(join(" ", @command)) + 1;
while (<stdin>) {
	if ($lengthcommand + length($args) + length($_) > $lengthmax) {
		system @command, split(/\n/, $args);
		$err = 1 if $?;
		$args = "";
	}
	$args .= $_;
}
if (length($args)) {
	system @command, split(/\n/, $args);
	$err = 1 if $?;
}
exit $err;

================================================== snip snip

This was written under Perl2.0, and could probably be modded a bit
under Perl3.0 (I see that 'stdin' is lowercase, for example...).

Enjoy.

Just another Perl hacker (but getting more visibility each day...),
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/