Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Issue with Exporter

by almut (Canon)
on Jan 11, 2007 at 14:57 UTC ( [id://594169]=note: print w/replies, xml ) Need Help??


in reply to Issue with Exporter

For the sake of completeness, yet another approach would be to directly import Exporter's import method into the namespace of your module. In that case it doesn't need to be looked up via inheritance, so you don't need to setup @ISA or use base ...

In other words, from the following 3 options, you can choose whichever you like best:

package One; use strict; use warnings; # --- option 1 - inheritance via @ISA # require Exporter; # our @ISA = qw(Exporter); # --- # --- option 2 - inheritance via use base ... # use base "Exporter"; # --- # --- option 3 - directly import Exporter's import() use Exporter "import"; # --- # ( @EXPORT or @EXPORT_OK always needed ) our @EXPORT = qw(method1); sub method1 { print "method1 greets hello"; } 1;

(Also, note that I changed the package name to "One" (capitalized). AFAIK, lowercase module names are - by convention - reserved for pragmatic modules like use strict;, use integer;, etc.)

BTW, a slight peculiarity with use strict; use warnings; in this particular case is that it does not (and cannot) warn you about potential problems due to not loading Exporter yourself (because, as ferreira pointed out, Exporter already is being loaded as a side effect of using strictures). For this reason, it could be argued that options 2 or 3 are the ones to prefer, as they do not share this potential pitfall...

Replies are listed 'Best First'.
Re^2: Issue with Exporter
by palette (Scribe) on Jan 12, 2007 at 11:43 UTC
    I tried the code with the 3rd option mentioned. Its working fine when I am using @EXPORT but when I am using
    our @EXPORT_OK = qw(method1);

    Its not working and the output I am getting is "its herehere"

    Thanks

      Do you understand the difference between @EXPORT and @EXPORT_OK?

      Symbols mentioned in @EXPORT are automatically exported by your module. Symbols mentioned in @EXPORT_OK are made available for export, they are not automatically exported. to export symbols that are in @EXPORT_OK, you need to explicitly name them in the "use" statement for the module.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      @EXPORT hold the symbols that are exported by default when you don't mention explicitly any symbols.

      use one; # will import the symbols in @EXPORT

      while @EXPORT_OK hold the symbols that can be exported on demand.

      use one qw(m1 m2); # m1 and m2 should be in @EXPORT or @EXPORT_OK

      Read more about it in the Exporter documentation.

      Hi,

      I just forgot to include the name of the method in the use statement.

      Thankyou very much.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found