in reply to 'use' inside or outside of package declaration?

I tend to start my modules in exactly the same way as my scripts:

#! perl -slw use strict; package fred; ...

But that's because I often end my modules with:

return 1 if caller; package main; fred->import; ## code that tests the package above goes here.

In this way, running the module as a perl fred.pm runs the test.

This keeps the tests with the tested code, ensures it is kept up to date with that code and acts as documentation for its use. To my mind, this is a 3 way win.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: 'use' inside or outside of package declaration?
by John M. Dlugosz (Monsignor) on May 11, 2011 at 07:52 UTC
    If the -l switch is not lexically scoped (I don't think it could be), then print statements will behave differently when running testing than when run as a module, if the consumer using your code isn’t using such a switch.
      then print statements will behave differently when running testing than when run as a module, if the consumer using your code isn’t using such a switch.

      Only if the module uses print. And I consider this to be exactly as I want it.

      If modules want to use $\, or any other global variable, then they *must* localise and set it. The main program *always* own the globals. So if modules want predictable behaviour from their use of globals, they must localise and set to their requirements.

      If my modules need to print to a predefined (global) filehandle, and I want $\ = "\n", then I will localise and set it so either on a case by case basis, or more likely, for (say) debug purposes, I define a private debug function that does so and call that rather than print.

      But, most modules don't print directly to STDOUT or STDERR. We'd mostly be pissed off if they did.

      Test scripts frequently do print stuff, and when the module is run as a script for testing, the -l is in force.

      As for -s, I frequently use runtime configuration in my test scripts like our $MAX //= 1e6;. Then if I want to run a quick test, I can use

      perl fred.pm -MAX=1e3

      Silly eg:

      #! perl -slw use strict; package fred; sub new { my $class = shift; return bless { @_ }, $class; } sub method { my $self = shift; return keys %{ $self }; } return 1 if caller; package main; our $MAX //= 10; print 'Max: ', $MAX; my $o = fred->new( 'a' .. 'z' ); print for $o->method; __END__ C:\test>perl -s fred.pm -MAX=123 Max: 123 w e a m s y u c k q g i o

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: 'use' inside or outside of package declaration?
by tchrist (Pilgrim) on May 11, 2011 at 19:21 UTC
    It is very important that package be the first non-comment token in a module.

      Because?

        Because otherwise some of the pragmata will misbehave, because then you don’t enclue Perl6 that it’s come across a Perl5 module, and because it gratuitously complicates the tool-chain.
          A reply falls below the community's threshold of quality. You may see it by logging in.