What wrong with my module?

Discussion in 'Perl Misc' started by Great Deals, Sep 23, 2003.

  1. Great Deals

    Great Deals Guest

    I put a file called test.pm in the same dir as test.pl

    test.pm:
    #perl
    package test;
    sub getit (){
    return time;
    }

    test.pl:
    #perl
    use test;

    $ab= test::getit();
    print "$ab\n";

    the error is:
    Undefined subroutine &test::getit called at test.pl line 4.
     
    Great Deals, Sep 23, 2003
    #1
    1. Advertising

  2. Great Deals wrote:
    > I put a file called test.pm in the same dir as test.pl
    >
    > test.pm:
    > #perl
    > package test;
    > sub getit (){
    > return time;
    > }


    Ensure that the module returns a true value by adding

    1;

    to the bottom.

    > test.pl:
    > #perl


    Try adding

    use lib '.';

    here.

    > use test;
    >
    > $ab= test::getit();
    > print "$ab\n";
    >
    > the error is:
    > Undefined subroutine &test::getit called at test.pl line 4.


    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 23, 2003
    #2
    1. Advertising

  3. Great Deals

    Sam Holden Guest

    On 22 Sep 2003 20:08:03 -0700,
    Great Deals <> wrote:
    > I put a file called test.pm in the same dir as test.pl
    >
    > test.pm:
    > #perl
    > package test;
    > sub getit (){
    > return time;
    > }
    >


    Since the temp.pm above doesn't return a true value either aren't
    getting the error you specifiy below, or you aren't using the code
    you posted.

    > test.pl:
    > #perl
    > use test;
    >
    > $ab= test::getit();
    > print "$ab\n";
    >
    > the error is:
    > Undefined subroutine &test::getit called at test.pl line 4.


    There's not much point in fixing some random code that you aren't
    actually using.

    test.pm doesn't contain what you claim it does, or a different
    test.pm is getting included than the one you think is...

    --
    Sam Holden
     
    Sam Holden, Sep 23, 2003
    #3
  4. Great Deals

    Keith Keller Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    In article <>, Sam Holden wrote:

    > There's not much point in fixing some random code that you aren't
    > actually using.
    >
    > test.pm doesn't contain what you claim it does, or a different
    > test.pm is getting included than the one you think is...


    If the latter, then certainly Great Deals could be using the
    code he posted. (I tested it, and it gives the undefined
    sub error.) The second fix Gunnar suggested would produce an
    error due to not returning true in test.pm. Without it, I bet
    the OP is on a case-insensitive filesystem which has Test::Harness
    installed. (Another reason not to name things test or Test.)

    - --keith

    - --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (Darwin)

    iD8DBQE/b8QDhVcNCxZ5ID8RAoTPAKCKoNlVmSH6LqsIPzyxRrLZwMKtFQCeOXpd
    iSvVZORrEPk23bbEUGikUiM=
    =G9r6
    -----END PGP SIGNATURE-----
     
    Keith Keller, Sep 23, 2003
    #4
  5. On 23 Sep 2003, Sam Holden wrote:

    >On 22 Sep 2003 20:08:03 -0700,
    > Great Deals <> wrote:
    >> I put a file called test.pm in the same dir as test.pl
    >>
    >> test.pm:
    >> #perl
    >> package test;
    >> sub getit (){
    >> return time;
    >> }
    >>

    >
    >Since the test.pm above doesn't return a true value either aren't
    >getting the error you specifiy below, or you aren't using the code
    >you posted.


    But we have no reason to believe that. Using the code he has, I can
    reproduce the problem.

    >> test.pl:
    >> #perl
    >> use test;
    >>
    >> $ab= test::getit();
    >> print "$ab\n";
    >>
    >> the error is:
    >> Undefined subroutine &test::getit called at test.pl line 4.

    >
    >There's not much point in fixing some random code that you aren't
    >actually using.
    >
    >test.pm doesn't contain what you claim it does, or a different
    >test.pm is getting included than the one you think is...


    The second is the proper diagnosis. He is on Win32 (or some other OS that
    has case-insensitive filenames). His program is using the Test.pm module,
    rather than his local test.pm module.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Sep 23, 2003
    #5
  6. On 22 Sep 2003, Great Deals wrote:

    >I put a file called test.pm in the same dir as test.pl


    Don't name it test.pm. Name it MyTest.pm or some variant thereof. You're
    using Windows, I bet, and your code is using the Perl standard Test.pm
    module instead of the test.pm you wrote.

    >test.pm:
    >#perl
    >package test;
    >sub getit (){
    >return time;
    >}
    >
    >test.pl:
    >#perl
    >use test;
    >
    >$ab= test::getit();
    > print "$ab\n";
    >
    >the error is:
    >Undefined subroutine &test::getit called at test.pl line 4.


    I know this because, with your code, on a Unix machine, I'd bet told that
    test.pm doesn't return a true value during compilation. This is because
    your module is missing the

    1;

    at the end, so that Perl knows everything went ok.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Sep 23, 2003
    #6
  7. Great Deals

    Sam Holden Guest

    On Tue, 23 Sep 2003 01:23:20 -0400,
    Jeff 'japhy' Pinyan <> wrote:
    > On 23 Sep 2003, Sam Holden wrote:
    >
    >>On 22 Sep 2003 20:08:03 -0700,
    >> Great Deals <> wrote:
    >>> I put a file called test.pm in the same dir as test.pl
    >>>
    >>> test.pm:
    >>> #perl
    >>> package test;
    >>> sub getit (){
    >>> return time;
    >>> }
    >>>

    >>
    >>Since the test.pm above doesn't return a true value either aren't
    >>getting the error you specifiy below, or you aren't using the code
    >>you posted.

    >
    > But we have no reason to believe that. Using the code he has, I can
    > reproduce the problem.


    Since I don't get that error, I have a perfectly valid reason for
    believing the code he posted wasn't the code he actually had.

    Am I supposed to know the workings of every possible platform with every
    possible set of module installs? I don't think that's reasonable.

    I noticed the missing true return in the module, so I copied the code
    into the file names he specified and tested it. And as I suspected,
    got a different error message. I even did a perl -Mtest -e1 to see if
    there was a test pragma I hadn't noticed before. What more do you
    propose I do?

    Stating the two reasons I can see for the problem seems a reasonably
    helpful and useful answer.

    Do I really have to test everything on all possible architectures before
    I can take a guess at the cause of a problem? That's really going
    to slow down any answers I might give as I hunt for a VMS machine to
    test on.

    --
    Sam Holden
     
    Sam Holden, Sep 23, 2003
    #7
  8. Keith Keller wrote:
    > In article <>, Sam
    > Holden wrote:
    >> There's not much point in fixing some random code that you aren't
    >> actually using.
    >>
    >> test.pm doesn't contain what you claim it does, or a different
    >> test.pm is getting included than the one you think is...

    >
    > If the latter, then certainly Great Deals could be using the code
    > he posted. (I tested it, and it gives the undefined sub error.)
    > The second fix Gunnar suggested would produce an error due to not
    > returning true in test.pm. Without it, I bet the OP is on a
    > case-insensitive filesystem which has Test::Harness installed.
    > (Another reason not to name things test or Test.)


    So that's why my "use lib '.';" suggestion makes a difference. :)

    Choosing some other name for the module, as suggested by Jeff, is
    obviously a better fix.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 23, 2003
    #8
  9. Great Deals

    Keith Keller Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1
    NotDashEscaped: You need GnuPG to verify this message

    On 2003-09-23, Sam Holden <> wrote:
    >
    > Do I really have to test everything on all possible architectures before
    > I can take a guess at the cause of a problem?


    No, of course that's unreasonable. I think Jeff's point is that you
    shouldn't accuse someone of not using the code he's posted unless you're
    *sure* it couldn't produce the posted output. (e.g., an error like
    $my=; shouldn't parse on any platform) You could even phrase it more
    like a question: "Are you *sure* that's the code you're running? Here's
    what I get...."

    Some might contend that, while we shouldn't have to know every nuance
    of every platform, popular problems like a case-insensitive filesystem
    should be considered when reading about a problem you can't reproduce.
    Obviously this is a grey area of problem-solving.

    --keith

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.0.6 (GNU/Linux)
    Comment: For info see http://www.gnupg.org

    iEYEARECAAYFAj9wdXAACgkQhVcNCxZ5ID+HfACeKg6wDHMBT/kHVXjIHxI9H09C
    mycAn0E773O5E1JL59x357FQ6MsgV20A
    =8ZJq
    -----END PGP SIGNATURE-----
     
    Keith Keller, Sep 23, 2003
    #9
  10. Great Deals

    Great Deals Guest

    here is the working one: the 1st mistake i made was the test.pm, it
    does not call my local test.pm but some default test.pm, secondly i
    missed the

    our @ISA=qw(Exporter);
    our @EXPORT=qw (getit2);

    test2.pm:
    #!/usr/bin/perl -w
    package test2;

    use strict;
    use Exporter;

    our @ISA=qw(Exporter);
    our $abc;
    our @EXPORT=qw (getit2);

    sub getit2{
    return $abc;
    }

    1;
    __END__

    in test2.pl:

    #!/usr/bin/perl -w
    use test2;
    $abc='123';
    $ab= getit2;
    print "$ab\n";
    print getit2;


    My questtion is
    our @ISA=qw(Exporter);
    our @EXPORT=qw (getit2);

    a must thing to declare?

    Secondly, how the module use the global value in main? I want to set a
    value $abc to 123 in test2.pl, but I also want to get this value in
    module in test2.pm. I don't want to pass the value as a parameter,
    because I am still learning the passing by ref.

    If in sub ($para1){$para1='new value'}, will the global value of para1
    will changed? or once the sub is finished the value will be reset just
    as using local or my?





    (Great Deals) wrote in message news:<>...
    > I put a file called test.pm in the same dir as test.pl
    >
    > test.pm:
    > #perl
    > package test;
    > sub getit (){
    > return time;
    > }
    >
    > test.pl:
    > #perl
    > use test;
    >
    > $ab= test::getit();
    > print "$ab\n";
    >
    > the error is:
    > Undefined subroutine &test::getit called at test.pl line 4.
     
    Great Deals, Sep 23, 2003
    #10
  11. Great Deals wrote:
    > secondly i missed the
    >
    > our @ISA=qw(Exporter); our @EXPORT=qw (getit2);


    That was not necessarily a mistake. An alternative is to fully qualify
    the subroutine name as you did in the first post:

    $ab = test2::getit2();

    > Secondly, how the module use the global value in main? I want to
    > set a value $abc to 123 in test2.pl, but I also want to get this
    > value in module in test2.pm. I don't want to pass the value as a
    > parameter, because I am still learning the passing by ref.


    Right now you have two $abc variables: $main::abc and $test2::abc. You
    set $main::abc in test2.pl, while the subroutine in test2.pm returns
    the value of $test2::abc (which is empty).

    Again, you can qualify the name:

    return $main::abc;

    Or you can export/import it (but then you need to skip $test2::abc).

    > If in sub ($para1){$para1='new value'}, will the global value of
    > para1 will changed? or once the sub is finished the value will be
    > reset just as using local or my?


    Not sure what you are talking about here.

    You'd better do some reading and homework, right? A couple of pointers:

    http://www.perldoc.com/perl5.8.0/pod/perlmod.html

    http://www.perldoc.com/perl5.8.0/pod/perlintro.html#Variable-scoping

    Good luck!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 23, 2003
    #11
  12. Great Deals

    Great Deals Guest

    Gunnar Hjalmarsson <> wrote in message news:<bkq5po$4ed53$-berlin.de>...
    > Great Deals wrote:
    > > secondly i missed the
    > >
    > > our @ISA=qw(Exporter); our @EXPORT=qw (getit2);

    >
    > That was not necessarily a mistake. An alternative is to fully qualify
    > the subroutine name as you did in the first post:
    >
    > $ab = test2::getit2();
    >


    I understand now that I don't need our @EXPORT=qw (getit2); if I do
    $ab = test2::getit2(); but what is the @ISA for?
    in perlmod:
    There is no special class syntax in Perl, but a package may act as a
    class if it provides subroutines to act as methods. Such a package may
    also derive some of its methods from another class (package) by
    listing the other package name(s) in its global @ISA array (which must
    be a package global, not a lexical).

    Does this mean if I don't use subs as methods then I don't need ISA
    either? Can you show me the syntax difference between using subs and
    using methods?
    sub: test2::getit2();
    method: test2->getit2();
    Am I right?
     
    Great Deals, Sep 24, 2003
    #12
    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. Matthew
    Replies:
    7
    Views:
    866
    Priscilla Walmsley
    Jan 7, 2005
  2. Maric Michaud
    Replies:
    0
    Views:
    7,219
    Maric Michaud
    Jun 24, 2006
  3. yoma
    Replies:
    3
    Views:
    209
    Terry Reedy
    Nov 7, 2008
  4. Pix
    Replies:
    0
    Views:
    303
  5. smith jack

    module import error, what's wrong?

    smith jack, Aug 8, 2011, in forum: Python
    Replies:
    0
    Views:
    177
    smith jack
    Aug 8, 2011
Loading...

Share This Page