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

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.
  • Comment on Re^2: 'use' inside or outside of package declaration?

Replies are listed 'Best First'.
Re^3: 'use' inside or outside of package declaration?
by BrowserUk (Patriarch) on May 11, 2011 at 08:43 UTC
    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.