Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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 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.

Replies are listed 'Best First'.
Re: Module mechanics question
by Your Mother (Bishop) 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 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, you'd need:
    package bar; use Modern::Perl; sub import { ... goto &Modern::Perl::import; }
    (Or you could just copy Modern::Perl's source as and add to it...)
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.


      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.
      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.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://920053]
Approved by davido
Front-paged by ig
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-06-25 03:16 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.