In addition to being a very useful little piece of system administration code, this code also neatly demonstrates the namespace specification of localised variables (@output within the eval block) which may prove illustrative to some - This is also documented on the perlsub man page under the heading "Temporary Values via local()".
#!c:\Perl\Bin\Perl.exe use constant EMAIL => 'you@yourdomain.com'; use constant SERVER => 'mail.yourdomain.com'; use IPC::Open3; use Net::SMTP; use strict; my $cmd = join ' ', @ARGV; my $mail = Net::SMTP->new( SERVER ); $mail->to( EMAIL ); $mail->mail( 'root@yourdomain.com' ); $mail->data(); my @output; @output = eval { local *output; open3( "<&STDIN", \*OUTPUT, \*OUTPUT, $cmd ) || die $!; push @::output, $_ while (<OUTPUT>); close OUTPUT; return @::output; }; if ($@) { $mail->datasend( "The command '$cmd' could not be executed - $@\n" + ); } else { $mail->datasend( join('', @output), "\n" ); } $mail->dataend; $mail->quit; exit 0;
In reply to Command Line Program Wrapper by rob_au
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |