Shell::Autobox - pipe Perl strings through shell commands


Shell-Autobox documentation Contained in the Shell-Autobox distribution.

Index


Code Index:

NAME

Top

Shell::Autobox - pipe Perl strings through shell commands

SYNOPSIS

Top

  use Shell::Autobox qw(xmllint);

  my $xml = '<foo bar="baz"><bar /><baz /></foo>';
  my $pretty = $xml->xmllint('--format');

DESCRIPTION

Top

Shell::Autobox provides an easy way to pipe Perl strings through shell commands. Commands passed as arguments to the use Shell::Autobox statement are installed as subroutines in the calling package, and that package is then registered as the handler for methods called on ordinary (i.e. non-reference) scalars.

When a method corresponding to a registered command is called on a scalar, it is passed as the command's standard input; additional arguments are passed through as a space-delimited list of options, and - if no error occurs - the command's standard output is returned. This can then be piped into other commands.

The scalar is written to a temporary file, and the name of this file is appended to the program name and the supplied options, so commands that expect their input to be supplied in some other fashion may need to be massaged.

e.g.

	$foo->bar('-input')->baz(...)

	# or

	$foo->bar('--option1', '--option2', '<')->baz(...)

The registered methods can also be called as regular functions e.g.

	use Shell::Autobox qw(cut);

	my $bar = cut("foo:bar:baz", "-d':' -f2");

EXPORT

None by default.

SEE ALSO

Top

autobox, autobox::Core, Shell

AUTHOR

Top

chocolateboy <chocolate.boy@email.com>

COPYRIGHT AND LICENSE

Top

VERSION

Top

0.03


Shell-Autobox documentation Contained in the Shell-Autobox distribution.

package Shell::Autobox;

use strict;
use warnings;

use File::Temp;
use Carp qw(confess);
use base qw(autobox);

our $VERSION = '0.03';

sub import {
	my $class = shift;
	my $caller = (caller)[0];

	for my $program (@_) {
		my $sub = sub {
			my $input = shift;
			my $args = join ' ', @_;
			my $stdin = File::Temp->new();
			my $stdout = File::Temp->new();
			my $stderr = File::Temp->new();
			my $command = "$program $args $stdin 2> $stderr > $stdout";
			my ($output, $error, $status);

			print $stdin $input;
			$status = system $command;

			{
				local $/ = undef;
				$error = <$stderr>;
				$output = <$stdout>;
			}

			if ($status) {
				confess "can't exec $command" . ((length $error) ? ": $error" : '');
			} elsif (length $error) {
				warn $error;
			}

			return $output;
		};

		{
			no strict 'refs';
			*{"$caller\::$program"} = $sub;
		}
	}

	$class->SUPER::import(SCALAR => $caller);
}

1;

__END__