What wrong with my module?

G

Great Deals

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.
 
G

Gunnar Hjalmarsson

Great said:
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.
 
S

Sam Holden

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...
 
K

Keith Keller

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

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

- --
(e-mail address removed)-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-----
 
J

Jeff 'japhy' Pinyan

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.
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.
 
J

Jeff 'japhy' Pinyan

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.
 
S

Sam Holden

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.
 
G

Gunnar Hjalmarsson

Keith said:
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.
 
K

Keith Keller

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

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

--
(e-mail address removed)-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-----
 
G

Great Deals

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?
 
G

Gunnar Hjalmarsson

Great said:
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!
 
G

Great Deals

Gunnar Hjalmarsson said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top