Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Export and use different package in module

by chenhonkhonk (Acolyte)
on Feb 22, 2019 at 18:42 UTC ( [id://1230405]=perlquestion: print w/replies, xml ) Need Help??

chenhonkhonk has asked for the wisdom of the Perl Monks concerning the following question:

I have what sounds to me like a simple problem. I want to use a different 'package' in a module for brevity, and I want to export things in the normal package namespace. If I include the package My::foo; alone, my functions and variables export with @EXPORT_OK and our @EXPORT. Perfectly fine no problems. If I then put package barpackage; somewhere for making all the functions/variables not have barpackage::otherpackagestuff:: in front for the sub-package, all my exports are not found. I'm used to C just having extern or include'ing, and not needing to have tricks and hacks just so I can export things without typing 5x the characters for any value/function.

I know it's "bad" programming to pollute a namespace, but it shouldn't be a broken kludge of the language to stop me from overlapping namespaces at all.

  • Comment on Export and use different package in module

Replies are listed 'Best First'.
Re: Export and use different package in module
by haukex (Archbishop) on Feb 22, 2019 at 19:00 UTC

    Could you show an SSCCE of the situation you're describing? I'm not sure I fully understand what you're asking. Perhaps something like me::inlined could help?

      I'll try to post it, see if it makes more sense.

      main

      use strict; use warnings; use File::Basename qw(dirname); use Cwd qw(abs_path); use lib dirname(dirname abs_path $0); use Library; print STDOUT $hash{"key"} . "\n"; exit 0;

      this works

      Library.pm

      package Library; use strict; use warnings; use Exporter qw(import); our @EXPORT = qw( %hash ); our %hash = ( "key" => "Hello world!" ); 1;

      these don't work

      Library2.pm

      package Library2; use strict; use warnings; our @EXPORT = qw( return_string ); sub return_string { return "Hello embedded library"; }

      Library.pm

      package Library; use strict; use warnings; use Exporter qw(import); our @EXPORT = qw( %hash ); use File::Basename qw(dirname); use Cwd qw(abs_path); use lib dirname(dirname abs_path $0); use Library2; package Library2; our %hash = ( "key" => return_string() ); 1;

      I can understand it looks like the namespaces are overlapping, but the definition isn't exported even with 'our'. Just fiddled around with it, I can do our %hash = (); BEFORE the package othernamespace, and export the modification. That'll work for my case, but I'd rather not have 3-4 places I need to put the hash instead of just export and our. It seems my gripe is that a variable declared before the package othernamespace trickles down, but a variable declared after the package othernamespace doesn't trickle up when it's the same file, even though there's no complaint about the definition in another namespace and not in the original.

        First, note that Library2.pm is missing the use Exporter qw(import);, so nothing is actually exported when you do use Library2;.

        I suspect you added the package Library2; to Library.pm to work around that? However, the effect of doing so is that our %hash is declared in the current package, which is now Library2, so you're assigning a value to %Library2::hash, while what Library.pm is exporting is %Library::hash. Although you could change our %hash = ... into %Library::hash = ..., that's just fixing a workaround with a workaround, and the IMO much cleaner solution is to add the use Exporter qw(import); statement I mentioned above to Library2.pm, and just remove the package Library2; from Library.pm, which is then no longer needed, because now sub return_string will be exported from Library2 into Library and be callable there.

        BTW, this:

        use File::Basename qw(dirname); use Cwd qw(abs_path); use lib dirname(dirname abs_path $0);

        Can be replaced by the more reliable:

        use FindBin; use lib $FindBin::Bin;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-25 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found