Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Writing Modules/namespace polution

by chas (Priest)
on Mar 16, 2005 at 16:03 UTC ( [id://439995]=note: print w/replies, xml ) Need Help??


in reply to Writing Modules/namespace polution

Actually, the default exports should be in @EXPORT, not @EXPORT_OK. The only thing I notice immediately is that I believe you should "use Exporter" not "require Exporter" in the module file (Update: plus the other posted comments...)
chas

Replies are listed 'Best First'.
Re^2: Writing Modules/namespace polution
by Mugatu (Monk) on Mar 16, 2005 at 16:50 UTC
    Notwithstanding the fact that he doesn't appear to need to export anything -- as pointed out by other posts -- I'm not so sure it's advisable to suggest using @EXPORT very often. I realize a lot of well known and good modules do this, but most of the time, I think it makes more sense to use @EXPORT_OK and let the caller specify what they want to import.
      Agreed, but aside from the object/non-object mixup which I didn't catch, I interpreted the original poster's comments as indicating that he expected use mymodule; would make the entries in @EXPORT_OK available without explicit package referencing. My misunderstanding, I guess...
      chas
Re^2: Writing Modules/namespace polution
by ikegami (Patriarch) on Mar 16, 2005 at 16:08 UTC
    No, require is fine.
Re^2: Writing Modules/namespace polution
by nobull (Friar) on Mar 16, 2005 at 18:05 UTC
    I believe you should "use Exporter" not "require Exporter"

    I believe you should "require Exporter" or "use base 'Exporter'" not "use Exporter".

    The exporter module defines &Exporter::import so that it can be inheritied by other modules. Calling Exporter::import('Exporter') doesn't really make sense.

      Doing a "use Exporter;" will not do anything different than a "require Exporter;" because the import() function in Exporter doesn't do anything because Exporter doesn't define @EXPORT or @EXPORT_OK.

      The only difference between "use base Exporter;" and either "use Exporter;" or "require Exporter;" is that "use base" will modify @ISA for you and the others make you modify it yourself. You either have to have Exporter in your @ISA or have done something like *import = \&Exporter::import; to get the benefits of Exporter.

      Nothing more, nothing less.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Well, I just checked in the Camel books. The 3rd edition does indicate "require Exporter;" in modules, but the 2nd edition (which I learned from) has "use Exporter;" in modules in most of the examples (although there was a "require Exporter;" in an example.) Randal Schwartz's book "Learning Perl Objects, References & Modules" says "...As a module author, all you do is add:
      use Exporter; our @ISA = qw(Exporter);
      I just made a module skeleton using h2xs, and "require Exporter;" appears in the module file. So maybe either will suffice..I'll go with the "require" form since that's what h2xs produced.
      chas
      (Update: Hadn't seen Dragonchild's reply yet - that seems to settle the difference. Fixed typo.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://439995]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-26 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found