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.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

In reply to Re: Executing a program from within a Perl Module in a non-standard path by tobyink
in thread Executing a program from within a Perl Module in a non-standard path by GoldElite

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.