Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Perl includes to reuse code.

by Anonymous Monk
on May 30, 2016 at 05:28 UTC ( [id://1164483]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I have created a file called system_mod_includes.pm and the contents of the file are as below:

# # System Modules # use CGI; use HTML::Template; use File::stat; use File::Type; use File::Temp; use JSON; use Time::Local; use POSIX 'strftime'; use XML::Simple; use XML::Parser; use IO::Socket::INET; use Digest::MD5 qw(md5 md5_hex md5_base64); use Data::Dumper; use Net::SMTP; use Net::SMTP::TLS; use MIME::Base64; use Compress::Zlib; use URI::Escape; use Locale::Country; use Socket; use HTML::Entities; use List::Util qw[min max]; use IO::Uncompress::Gunzip qw(gunzip $GunzipError); use IO::Compress::Gzip qw(gzip $GzipError); 1;

Then I have another module called Utils.pm. In this module I am using a function of perl module Data::Dumper.

For example as below:

print Dumper(%HASH);

OR

print Data::Dumper(%HASH);

But this gives me error saying the function Dumper is unknown.

But use Data::Dumper is already defined in system_mod_includes.pm.

The error however goes away if I defined use Data::Dumper in Utils.pm as well.

Replies are listed 'Best First'.
Re: Perl includes to reuse code.
by Athanasius (Archbishop) on May 30, 2016 at 06:09 UTC

    You need to include the line:

    use system_mod_includes;

    at the head of the Utils.pm module. This will allow you to use both of these:

    print Dumper(%HASH); print Data::Dumper::Dumper(%HASH);

    But note that print Data::Dumper(%HASH); is a syntax error: the module name is Data::Dumper, so if you want to specify the namespace explicitly when calling the Dumper function, the syntax has to be Data::Dumper::Dumper.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Perl includes to reuse code.
by Tanktalus (Canon) on May 30, 2016 at 14:30 UTC

    The error doesn't completely go away if you use Data::Dumper; in your Util.pm. The second option, print Data::Dumper(%HASH) line still won't work, because that's just not how perl works.

    Instead, if you want to import things, whether implicitly (such as Dumper from the Data::Dumper module) or explicitly (such as gzip from IO::Compress::Gzip), and then make them available to any module that uses your module, you need to explicitly mark those symbols for export, either by providing your own import routine, or using the one from Exporter or other module that provides export functionality.

    However, I would only really recommend doing this for truly global functions - ones that really will be used essentially everywhere. I've done this, but I really mean the things I put in there were well and truly global - such as strict, warnings, feature, and a number of other modules that I expected 90%+ of the rest of my modules to require (translation services and trace-logging). Other required modules I would prefer making explicit when and where needed, not to keep the memory usage down, but to help self-documentation: making things explicit. The above ones I put into a "policy.pm" file because it was our policy to follow strict, enable all warnings, enable perl features explicitly and commonly across the project, to support translation, and to trace nearly everything.

Re: Perl includes to reuse code.
by Anonymous Monk on May 30, 2016 at 07:05 UTC
    Currently I have my Utils.pm as below: package Utils;
    use system_mod_includes;

    So should I write my Utils.pm as below?
    use system_mod_includes;
    package Utils;

      No, in my test scripts, Utils.pm begins as follows:

      package Utils; use system_mod_includes;

      But note that there is no package directive in system_mod_includes.pm. If I add one, I get an error like the one you originally reported:

      Undefined subroutine &Utils::Dumper...

      I assume this is a namespace problem.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found