Skip to content

Instantly share code, notes, and snippets.

@dolmen
Created May 4, 2011 17:00
Show Gist options
  • Save dolmen/955574 to your computer and use it in GitHub Desktop.
Save dolmen/955574 to your computer and use it in GitHub Desktop.
Shell::AutoLoad : similar to Shell, but without captures
use strict;
use warnings;
package Shell::AutoLoad;
use Carp;
sub import
{
my $pkg = caller;
my $autoload = sub {
my $sub = do {
no strict 'vars';
$AUTOLOAD;
};
(my $command = $sub) =~ s/^.*:://;
{
no strict 'refs';
*$sub = _make_cmd($command);
}
goto &$sub;
};
no strict 'refs';
*{"${pkg}::AUTOLOAD"} = $autoload;
for(@_) {
*{"${pkg}::$_"} = _make_cmd($_);
}
}
sub _make_cmd
{
my $command = shift;
return sub {
if (defined wantarray) {
local $Carp::CarpLevel = $Carp::CarpLevel+1;
Carp::croak "only void context is supported by ".__PACKAGE__." magic subs";
}
system $command $command, @_;
};
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment