Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Actually, all you're putting off is running the import (which in an OO class you aren't likely to be running anyway). All of the code sitting at the class level will still run ...

Not when the require() happens inside a conditional in the library consumer, which is what I was getting at. There's no point (and in fact it can't work) to call require() or use() for packages provided inline, so I presume both of us aren't talking about that case.

Consider the following structure shown below. The entire Foo:: package (and in fact the module file itself) is only loaded if the conditional is hit. This can be demonstrated by running an strace(1) (or your system's equivalent) against a call to that program both without, and then with the "load" parameter. This parameter in fact causes the module to be conditionally loaded, including the BEGIN code defined in the module.


use strict; use warnings; use lib 'lib'; my $arg = shift // ''; if ($arg eq "load") { print "Now loading optional library.\n"; require Foo; my $o = Foo->new; # Do stuff with $o here.. } else { print "I skipped loading the optional library.\n"; print "Try passing the 'load' option to load it.\n"; }

File: lib/

package Foo; use strict; use warnings; BEGIN { printf "BEGIN called from package %s\n", __PACKAGE__; } print "Class level logic.\n"; sub new { my $class = shift; $class = ref($class) || $class; # subclass boilerplate. print "constructor invoked for $class\n"; return bless {}, $class; } 1;

In reply to Re^4: Behavior of 'our' variables in the package-namespace by Apero
in thread Behavior of 'our' variables in the package-namespace by Apero

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2022-07-07 13:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found