in reply to Executing a program from within a Perl Module in a non-standard path
This is quite cute - a subclass of System::Sub that croaks at compile time if the command cannot be found:
use v5.10; use strict; use warnings; package System::Sub::Env; use Carp qw(croak); use File::Which qw(which); use base qw(System::Sub); sub import { my $class = shift; my @super; while (@_) { my $cmd = shift; my %opt = @{ ref($_[0]) ? shift : [] }; my $env = uc("PATH_TO_$cmd"); $opt{'$0'} //= ($ENV{$env} // which($cmd)); -x $opt{'$0'} or croak("Could not find '$cmd'; please set '$env' environ +ment variable; stopped"); push @super, $cmd => [%opt]; } @_ = ($class, @super); goto \&System::Sub::import; } 1;
You can use it like this:
use System::Sub::Env "mv"; mv("oldname.txt" => "newname.txt");
At compile time, it will check to see if there's a $ENV{PATH_TO_MV environment variable set, and use mv from there (it should be the full path to the binary). If that's not set, it will fall back to a normal $ENV{PATH} search. And otherwise it will croak, asking them to set the environment variable.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Executing a program from within a Perl Module in a non-standard path
by GoldElite (Beadle) on Mar 20, 2013 at 11:53 UTC | |
by tobyink (Canon) on Mar 20, 2013 at 15:30 UTC |