in reply to Which "use" statements in a module do "bubble up" to the callers? [SOLVED]
obviously, using the constant TESTCONST in script.pl works as expected
Actually, that's not so obvious from what you've written so far. use Test::Test1; on its own should not cause TESTCONST to be exported from Test::Test1 into the current package unless there is additional code you haven't told us about, such as if you're using Exporter or your script.pl begins with package Test::Test1;. (Update 2: Ok, as per your reply, you don't have a package Test::Test1; at the top of Test1.pm, that explains it too. I wrote this post with the assumption that your Test1.pm begins with package Test::Test1;.)
Is there an overview about which of the various "use ..." statements
Unfortunately, no. There are some rough rules of thumb, such as modules whose names are lowercase are "pragmas" and their effect is often (but not always!) lexically scoped (warnings and strict being the two most common), and regular modules will usually at most export a few functions into your package (although there are lots of exceptions to this rule of thumb). So the short answer is you'll have to look at each module's documentation to see what useing it will do. The long answer is:
Once you understand what's going on under the hood, the mechanisms that Perl uses for modules and pragmas are actually relatively transparent in that there isn't a ton of magic involved, and one can see all the moving parts. When you use a module that you write yourself, here's a rough overview of the mechanisms involved:
So you can have modules that export functions in the way I described above, you can have modules that export nothing (usually OO modules), or your own custom "pragmatic" modules that, like warnings, only have an effect in the current lexical scope (documented in perlpragma), or even mixes of these kinds of modules. A close reading of perlmod and the other documentation referenced therein should explain most of this in more detail. Because Perl is very flexible in what it allows module authors to do, it isn't easy to give some general rules as to how modules act, beyond some guidelines and best practices about how modules should be written, and based on that some rules of thumb of what to expect. So you'll have to look at the documentation of each of the modules to see what effects useing them will have. Luckily, most modules have good documentation and the "Synopsis" and further descriptions at the top will usually tell you right away.
I have dozens of scripts which all should use the same basic settings
The recent thread Code Reuse and the threads linked therein (like use y) should help you there. In particular, I have myself used Syntax::Collector and it's a nice module. The only minor drawback is that it uses Exporter::Tiny instead of Exporter, so you can't export variables, only subs (including constants). And just for completeness, here's the code that does what you want without using an extra module:
package Test::Test1; use warnings; use strict; use constant TESTCONST => 123; use base 'Exporter'; our @EXPORT = qw/TESTCONST/; sub import { warnings->import; strict->import; __PACKAGE__->export_to_level(1, @_); return; }
Then, in code that does use Test::Test1;, warnings and strict are enabled and sub TESTCONST is exported as well.
* Update: The basis of what most sub import implementations do is described in the section "Symbol Tables" in perlmod - basically you take the entry for the sub whatever in the current package, &Current::Package::whatever, and copy it to the symbol table of the target module (e.g. main), so that both &main::whatever and &Current::Package::whatever refer to the same piece of code. For completeness, here is an extremely minimalistic implementation of sub import, but this is the basis of what Exporter and related modules do:
our @EXPORT = qw/ ... /; sub import { my $callerpackage = caller; for my $export (@EXPORT) { no strict 'refs'; *{$callerpackage."::".$export} = \&{$export}; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Which "use" statements in a module do "bubble up" to the callers? (updated)
by Nocturnus (Scribe) on Aug 31, 2017 at 16:56 UTC | |
by haukex (Archbishop) on Aug 31, 2017 at 17:06 UTC | |
by Nocturnus (Scribe) on Sep 01, 2017 at 07:35 UTC | |
by choroba (Cardinal) on Sep 01, 2017 at 09:15 UTC | |
by haukex (Archbishop) on Sep 01, 2017 at 12:23 UTC | |
by Nocturnus (Scribe) on Sep 02, 2017 at 10:04 UTC | |
|