Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Module mechanics question

by Logicus
on Aug 12, 2011 at 17:18 UTC ( #920053=perlquestion: print w/ replies, xml ) Need Help??
Logicus has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, lets say I had;
package foo; use bar; #more code here 1;

And furthermore I wished for package bar.pm to call in Modern::Perl (alongside doing what it does) such that I don't need to add that to each script as well as having to add use bar... how would I would I go about doing it?

As in I'd rather write the above than write this:

package foo; use bar; use Modern::Perl; #code 1;

My first thought was of exporter but it doesn't seem quite right for the task.

Comment on Module mechanics question
Select or Download Code
Re: Module mechanics question
by yitzchak (Sexton) on Aug 12, 2011 at 18:17 UTC
    Like so:
    package bar; use parent 'Modern::Perl'; # rest of bar stuff 1;
    If you need to use import for something else in bar.pm, you'd need:
    package bar; use Modern::Perl; sub import { ... goto &Modern::Perl::import; }
    (Or you could just copy Modern::Perl's source as bar.pm and add to it...)
Re: Module mechanics question
by Your Mother (Canon) on Aug 12, 2011 at 18:24 UTC

    This is pretty much what Modern::Perl itself does. So just check its source; it's both terse and straightforward. Other modules that do similar things might also be interesting to look at, like utf8::all or Moose. Related enough to mention though not really what you're asking after: Sub::Exporter; note that it doesn't mix well with messing with import manually.

Re: Module mechanics question
by armstd (Friar) on Aug 12, 2011 at 19:49 UTC

    If foo wants to use Modern::Perl, then foo better 'use Modern::Perl;' (or require...). If foo isa bar, and bar uses Modern::Perl, then it's merely a coincidence that foo can access Modern::Perl, and must not be depended upon. Same goes for export/import. If bar imports everything from Modern::Perl, and re-exports it, then foo imports everything from bar, then foo is still using bar, and should not assume anything about Modern::Perl.

    If you code all of your dependencies into bar, and then restructure foo not to use bar, foo breaks. You'll have to sift through it all and figure out what foo really used that bar magically provided. Magic is bad. @EXPORT is bad for similar reasons... not explicitly specifying what symbols come from where in every package leads to lots of guesswork for future maintainers, and guesswork leads to bugs.

    --Dave

      No worries I actively want to enforce the use of "bar" and because of it Modern::Perl, strict and so on, onto folks I'll employ to write packages like "foo" for me.
      I think you've misunderstood what Modern::Perl is. It's basically a pragma equivalent to use strict, use warnings, and a couple other "modern" things, all lexically scoped. So if foo isa bar and bar uses Modern::Perl, there is no effect on foo. But the question is how to make your own "pragma" that does what Modern::Perl does, presumably plus some other things.
Re: Module mechanics question
by TomDLux (Vicar) on Aug 15, 2011 at 15:17 UTC

    The reason you differentiate 'foo' and 'bar' into separate modules is because they do different things. That creates a possibility that either 'foo' or 'bar' will be re-used in separate programs.

    Therefore you should have use Modern::Perl in each file, so they will be modern even if used separately from each other.

    After all, you used to tolerate two standard lines in each module to get 'warnings' and 'strict'; now one line in each module gets you those and more.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      In this case package foo is one of many dependant on bar and not intended or designed to function without it, but I can see what your saying. If foo were reusable as a standalone package then yes, it would be better to have the Modern::Perl pragma in there.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://920053]
Approved by davido
Front-paged by ig
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-20 00:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls