Using use in programs vs modules

Discussion in 'Perl Misc' started by pgodfrin, Jan 9, 2009.

  1. pgodfrin

    pgodfrin Guest

    Greetings,
    I have little module, MyUtil.pm for example, that has utility
    functions I use in most of my perl programs. I would like to use one
    function from this module in another module - while at the same time
    referring to the same module subroutine in the "calling" perl program.
    For example

    The module (pseudo-code):
    package MyNewModule
    use strict;
    use warnings;
    use Carp;
    use lib '/home/modules';
    use MyUtill qw(mytool);

    sub test_it {
    mytool();
    }

    The perl program (psuedo again):

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Carp;
    use lib '/home/modules';
    use MyUtill qw(mytool);
    use MyNewModule qw(test_it);

    print mytool();
    print test_it();

    This seems to work. I wonder though - is this bad form? Or does the
    internal name space stuff keep it all straight?
    thanks,
    pg
    pgodfrin, Jan 9, 2009
    #1
    1. Advertising

  2. pgodfrin wrote:
    > Greetings,
    > I have little module, MyUtil.pm for example, that has utility
    > functions I use in most of my perl programs. I would like to use one
    > function from this module in another module - while at the same time
    > referring to the same module subroutine in the "calling" perl program.
    > For example
    >
    > The module (pseudo-code):
    > package MyNewModule
    > use strict;
    > use warnings;
    > use Carp;
    > use lib '/home/modules';
    > use MyUtill qw(mytool);
    >
    > sub test_it {
    > mytool();
    > }
    >
    > The perl program (psuedo again):
    >
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    > use Carp;
    > use lib '/home/modules';
    > use MyUtill qw(mytool);
    > use MyNewModule qw(test_it);
    >
    > print mytool();
    > print test_it();
    >
    > This seems to work. I wonder though - is this bad form? Or does the
    > internal name space stuff keep it all straight?


    Seems ok to me. use()ing a module multiple times from different packages
    does no harm; if the module is already loaded and hence added to %INC,
    it won't be loaded again.

    (In the real code I suppose you export stuff from your modules, or else
    you wouldn't be able to import any symbols.)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jan 9, 2009
    #2
    1. Advertising

  3. pgodfrin <> wrote:

    > I have little module, MyUtil.pm for example, that has utility
    > functions I use in most of my perl programs. I would like to use one
    > function from this module in another module - while at the same time
    > referring to the same module subroutine in the "calling" perl program.
    > For example
    >
    > The module (pseudo-code):
    > package MyNewModule


    > use MyUtill qw(mytool);

    ^^
    ^^ oops

    > sub test_it {
    > mytool();



    This is simply a shorthand way of writing:

    MyNewModule::mytool(); # assuming MyUtil @ISA Exporter

    which is also equivalent to:

    MyUtil::mytool(); # no exporting needed


    > The perl program (psuedo again):



    There is no psuedo-ness to it. It is Real Perl Code(tm).


    > use MyUtill qw(mytool);


    > print mytool();



    This is simply a shorthand way of writing:

    print main::mytool();

    which is also equivalent to:

    MyUtil::mytool();


    > This seems to work. I wonder though - is this bad form? Or does the
    > internal name space stuff keep it all straight?



    The name space stuff keeps it all straight.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Jan 9, 2009
    #3
  4. pgodfrin

    Tim Greer Guest

    pgodfrin wrote:

    > This seems to work. I wonder though - is this bad form? Or does the
    > internal name space stuff keep it all straight?


    That depends, if the functions are unique enough and you know no one
    else will be trying to figure out the code later, or that you won't
    forget (though comments help a lot in that way). Still, it's a lot
    easier when you use the right name spaces and know exactly what
    function is from what module sometimes. Either way can work, but if
    it's a large program that will end up with a lot of modules and
    functions and you risk some similar or same function name, then you
    have to start using name spaces to not cause conflicts.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
    Tim Greer, Jan 9, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,616
    Dave Thompson
    Jan 10, 2005
  2. Coca
    Replies:
    7
    Views:
    723
    Aidan Grey
    Aug 24, 2004
  3. Replies:
    18
    Views:
    608
    Dave Thompson
    Jan 10, 2005
  4. lone_eagle
    Replies:
    3
    Views:
    623
    psykeedelik
    May 26, 2009
  5. Casey Hawthorne
    Replies:
    14
    Views:
    435
Loading...

Share This Page