andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

In the good spirit of chromatic and his book Perl Testing, I'm writing a test for my Perl script. The script uses Getopt::Long to parse command line arguments. I have yet to find a way to succesfully pass arguments to the script using require. luckynumber.pl
use strict; use warnings; use Getopt::Long; my $luckynumber = undef; GetOptions("luckynumber=i" => \$luckynumber) or die("GetOptions failed +"); die "missing argument luckynumber" unless defined($luckynumber); print "Lucky number is " . $luckynumber . "\n";
luckynumber.t
use warnings; use strict; use Test::More; use Test::Exception; BEGIN { plan tests => 2, todo => [] } # ------ load script without arg - expect failure throws_ok { require 'luckynumber.pl' } qr/missing argument/, 'needs argument'; # ------ load script with arg - expect ok ok( require 'luckynumber.pl --luckynumber=42', 'script loaded ok');
Running the test yields:
$ prove luckynumber.t luckynumber....ok 1/2Can't locate luckynumber.pl --luckynumber=42 in @ +INC ..
Is there a way to pass arguments to a script using require?

Andreas
--

Replies are listed 'Best First'.
Re: Argument in require statement
by almut (Canon) on May 21, 2007 at 11:24 UTC

    Not sure whether this is the way it's "supposed to be done", but you could populate @ARGV with the appropriate arguments:

    # ... @ARGV = ( '--luckynumber' => 42 ); ok( require 'luckynumber.pl', 'script loaded ok');
Re: Argument in require statement
by andreas1234567 (Vicar) on May 21, 2007 at 11:31 UTC
    If only I had turned to page 166 in the book above I found a neat solution: Use caller to determine whether the script is called from the command-line (caller returns undef) or from a subroutine or "eval" or "require" (caller returns name of the calling package).
    luckynumber.t
    use warnings; use strict; use Test::More; use Test::Exception; BEGIN { plan tests => 1, todo => [] } # ------ load script with arg - expect ok ok( require 'luckynumber.pl', 'script loaded ok');

    luckynumber.pl
    use strict; use warnings; use Getopt::Long; my $luckynumber = undef; sub main { GetOptions("luckynumber=i" => \$luckynumber) or die("GetOptions fa +iled"); die "missing argument luckynumber" unless defined($luckynumber); print "Lucky number is " . $luckynumber . "\n"; } # See also perldoc -f caller main() if! caller(); 1;

    Running the test yields:
    $ prove luckynumber.t luckynumber....ok All tests successful. Files=1, Tests=1, 0 wallclock secs ( 0.06 cusr + 0.01 csys = 0.07 C +PU)


    Andreas
    --
[OT] Re: Argument in require statement
by jettero (Monsignor) on May 21, 2007 at 12:19 UTC

    I like the way "if!" looks. It feels like a precursor to "if not," which coincidentally is the same length (in characters) as "unless." I definitely like "&main unless caller" more than "main() if! caller()," but I like "if!" more than "if !" ... I found that interesting enough to mention.

    -Paul