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

Hi

I came up with the following code for module testing in one file.

(Most probably saw something similar before)

use strict; use warnings; package use_test; sub import { local $"=", "; warn __PACKAGE__."->import( @_ ) called"; } BEGIN { $INC{__PACKAGE__.".pm"} = 1 } # mark as loaded package test; use use_test 1,2,3;
prints use_test->import( use_test, 1, 2, 3 ) called at c:/tmp/use_test.pl line 9.

Is there an easier way to do it? :)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re: "Inline" module testing?
by Arunbear (Prior) on Aug 15, 2017 at 10:35 UTC
      I like and use the modulino concept to run my test suites.

      > I'm reminded of "Modules as executable scripts?"

      Because it's the mirrored approach, I want to embed a fake module into a runnable script.pl .

      But in hindsight it's probably better to use the modulino approach right from the beginning to avoid later migration costs.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Modulinos the evil gimmick? Seriously?
Re: "Inline" module testing?
by Anonymous Monk on Aug 15, 2017 at 00:42 UTC

    Hi,

    I don't understand what you want

    This is iffy  $INC{__PACKAGE__.".pm"} = 1 ; better is  $INC{ Module::Load::_to_file(__PACKAGE__) } = __FILE__;

    I find this easier

    #!/usr/bin/perl -- use strict; use warnings; MeMyselfTestSuite() unless caller ; exit 0; BEGIN { package MeMyself; use Module::Load qw/ /; use Data::Dump qw/ dd /; $INC{ Module::Load::_to_file( __PACKAGE__ ) } = __FILE__; sub import { dd( \@_ , [caller] ); } 1; } sub MeMyselfTestSuite { use Module::Load; load( 'MeMyself', 1,2,3,4 ); }
      Thanks!

      > I don't understand what you want

      Maybe the wording "module testing" was to sloppy

      Real module testing requires a test suite, not a sub at the end of the file.

      I meant rather "module demonstration" or "proof of concept" testing, where module.pm and user.pl are packed into the same file, like when posting an example on perlmonks.

      Thank you for the interesting code, but it's significantly un-easier for my case, might help me in other occasions though.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!