Simple use-vs-require problem

Discussion in 'Perl Misc' started by clarence@silcom.com, Jan 25, 2005.

  1. Guest

    This has to be about the simplest question ever. And I was quite
    competent with perl before I stopped using it 7 years ago. I've
    been banging my head against the camel book (and the wall) for two
    hours now... I'm using Perl 5.8.6

    I have a module file called mod.pm, containing:
    sub P {
    print "in P()\n";
    }
    return 1;
    if I do
    $ perl
    use mod;
    P();
    I get
    in P()

    If I do
    $ perl
    require mod;
    mod::p();
    I get
    Undefined subroutine &mod::p called at - line 2.
    What the heck is wrong with that?

    Thanks.
    , Jan 25, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    <> wrote in message
    news:...
    > This has to be about the simplest question ever. And I was quite
    > competent with perl before I stopped using it 7 years ago. I've
    > been banging my head against the camel book (and the wall) for two
    > hours now... I'm using Perl 5.8.6
    >
    > I have a module file called mod.pm, containing:
    > sub P {
    > print "in P()\n";
    > }
    > return 1;
    > if I do
    > $ perl
    > use mod;
    > P();
    > I get
    > in P()
    >
    > If I do
    > $ perl
    > require mod;
    > mod::p();
    > I get
    > Undefined subroutine &mod::p called at - line 2.
    > What the heck is wrong with that?


    This has nothing to do with use vs require. Try swapping your examples
    use mod;
    mod::p();
    will still fail, and
    require mod;
    P();
    will still succeed.

    The problem is that your mod.pm file doesn't declare itself a part of
    any package. Therefore, all the code in mod.pm is in package main. A
    package is not defined by the name of the file, it is defined by the
    package statement.

    Try putting
    package mod;
    as the top line of mod.pm and see what results you get now. You will
    note that simply using 'use' instead of require does not grant you the
    ability to call P() without qualifying it with the package name. For
    that, &P has to be exported. Take a look at
    perldoc Exporter
    for more information

    Paul Lalli
    Paul Lalli, Jan 25, 2005
    #2
    1. Advertising

  3. wrote:

    > This has to be about the simplest question ever. And I was quite
    > competent with perl before I stopped using it 7 years ago. I've
    > been banging my head against the camel book (and the wall) for two
    > hours now... I'm using Perl 5.8.6
    >
    > I have a module file called mod.pm, containing:
    > sub P {
    > print "in P()\n";
    > }
    > return 1;
    > if I do
    > $ perl
    > use mod;
    > P();
    > I get
    > in P()
    >
    > If I do
    > $ perl
    > require mod;
    > mod::p();
    > I get
    > Undefined subroutine &mod::p called at - line 2.
    > What the heck is wrong with that?
    >

    Why would think that the sub "P" is in the "mod" package?
    It can't be, because there IS no "mod" package; you never
    defined one. You have a "mod" module, but not a "mod"
    package. If you put "package mod;" at the top of mod.pm,
    then all the global symbols you define in mod.pm will
    then be part of package "mod". Note that now your
    *first* program will break, because you haven't exported
    P out of mod...
    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Jan 25, 2005
    #3
    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. Patrick LeBoutillier

    use/require handler?

    Patrick LeBoutillier, Jan 31, 2004, in forum: Perl
    Replies:
    4
    Views:
    488
    Gunnar Hjalmarsson
    Jan 31, 2004
  2. Phillip Vong
    Replies:
    5
    Views:
    309
    Cowboy \(Gregory A. Beamer\)
    Oct 17, 2006
  3. ZyLo

    Proper use of 'require'

    ZyLo, Dec 30, 2005, in forum: Ruby
    Replies:
    2
    Views:
    98
    Hal Fulton
    Jan 1, 2006
  4. Servando Garcia

    how to use it: require

    Servando Garcia, Mar 5, 2006, in forum: Ruby
    Replies:
    2
    Views:
    91
    Diego Cano Lagneaux
    Mar 5, 2006
  5. Bill
    Replies:
    4
    Views:
    142
    Tad McClellan
    Jan 19, 2004
Loading...

Share This Page