El módulo System::Commands proporciona una interfaz funcional
a los comandos del sistema operativo:
lhp@nereida:~/Lperl/src/systemcommand$ cat -n lib/System/Commands.pm
1 package System::Commands;
2 use warnings;
3 use strict;
4 use File::Which qw(which);
5 use List::Util qw{first};
6
7 my @ALLOWED;
8
9 our $AUTOLOAD;
10 sub AUTOLOAD {
11 $AUTOLOAD =~ m{.*::(\w+)$};
12 my $command = $1;
13
14 die "Error! sub $AUTOLOAD does not exists!\n" unless first { $command eq $_ } @ALLOWED;
15 no strict 'refs';
16 *{$AUTOLOAD} = sub {
17 return `$command @_`;
18 };
19
20 goto &{$AUTOLOAD};
21
22 }
23
24 sub import {
25 my $mypackage = shift;
26 push @ALLOWED, @_;
27
28 my ($caller_package) = caller;
29 {
30 no strict 'refs';
31 for my $command (@_) {
32 # Comprobar si existe el comando
33 die "Error! '$command' command does not exists!\n" unless which($command);
34 *{$caller_package."::".$command} = \&{$command};
35 }
36 }
37 }
38
39 1;
Casiano Rodríguez León
