Skip to Content
Perl Hacks
book

Perl Hacks

by Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe
May 2006
Beginner
298 pages
6h 51m
English
O'Reilly Media, Inc.
Content preview from Perl Hacks

Hack #69. Simulate Hostile Environments in Your Tests

Test devastating failures with aplomb.

When you publish a CPAN module that depends on other modules, you list the prerequisite modules in your Makefile.PL or Build.PL script.

Using Build.PL:

my $builder = Module::Build->new(
  # ... other Build.PL options ...
  requires =>
  {
      'Test::More'       => 0,
      'CGI'              => 2.0,
  }
);

Using Makefile.PL:

WriteMakefile(
    # ... other Makefile.PL options ...
    'PREREQ_PM' =>
    {
        'Test::More'     => 0,
        'CGI'            => 2.0,
    }
);

However, there are a few ways that this standard prerequisite checking can be insufficient. First, you may have optional prerequisites. For instance, your module will use Foo::Bar if it happens to be installed, but should fail gracefully when Foo::Bar is absent.

Second, if the behavior of a module changed between two versions, you may still want to support both versions. For example, CGI changed how it handles PATH_INFO in version 3.11. Your CGI::Super_Path_Info module probably wants to be compatible with both CGI version 3.11 and also with earlier (and later) versions.

Finally, occasionally a user will install your module by hand to bypass the prerequisite check, hoping to use an older version of Foo::Bar than the one you require. Sometimes your module works fine (maybe with some feature limitations), but your test suite breaks because your tests assumed the presence of a new feature.

For each of these cases. you can make your module and tests more robust. For example, you can skip tests that are incompatible ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Debugged

Perl Debugged

Peter Scott, Ed Wright
Perl 6 Deep Dive

Perl 6 Deep Dive

Andrew Shitov
Learning Perl 6

Learning Perl 6

brian d foy
Perl by Example

Perl by Example

Ellie Quigley

Publisher Resources

ISBN: 0596526741Supplemental ContentErrata Page