Created
May 4, 2011 17:00
-
-
Save dolmen/955574 to your computer and use it in GitHub Desktop.
Shell::AutoLoad : similar to Shell, but without captures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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