Module should not work, but works

B

Bart Van der Donck

Hello,

This is a file named p.pl:

#!/usr/bin/perl
use strict;
use warnings;
use lib '/path/to/mm_e.pm/';
use mm_e;
mm::printok();
printok();
sub printok { print "ok from p.pl\n"; }

This is a file named mm_e.pm:

package mm;
sub printok { print "ok from mm_e.pm\n"; }
1;

Result:

% perl -w p.pl
ok from mm_e.pm
ok from p.pl
%

Why does this work fine ?
Shouldn't "strict" or "warnings" give me at least an error about this
kind of construction ? I 'm on perl 5.8.3 built for i386-freebsd.
 
A

Arne Ruhnau

Bart said:
Why does this work fine ?
Shouldn't "strict" or "warnings" give me at least an error about this
kind of construction ? I 'm on perl 5.8.3 built for i386-freebsd.

Hm, why shouldn't it? You name your package mm, use it, call printok
residing in the namespace mm, and then you call printok residing in
main. So, what's the deal?
Maybe you misinterpret the filename and the packagename, which happen to
be different?

puzzled,

Arne Ruhnau
 
C

Chris Mattern

Bart said:
Hello,

This is a file named p.pl:

#!/usr/bin/perl
use strict;
use warnings;
use lib '/path/to/mm_e.pm/';
use mm_e;
mm::printok();
printok();
sub printok { print "ok from p.pl\n"; }

This is a file named mm_e.pm:

package mm;
sub printok { print "ok from mm_e.pm\n"; }
1;

Result:

% perl -w p.pl
ok from mm_e.pm
ok from p.pl
%

Why does this work fine ?
Shouldn't "strict" or "warnings" give me at least an error about this
kind of construction ? I 'm on perl 5.8.3 built for i386-freebsd.
Um, what error? Why would perl complain about that construction? It's
perfectly proper in every way. You've declared two subs, &mm::printok
and &main::printok (with main being the default package name; all
globals have a package name, and if you don't write a package statement,
your default package is main. In mm_e.pm, the default package is
mm, because you put in a package statement). Then you call them.
Nothing wrong with any of that. Note that perl doesn't finish
parsing your source until it finishes reading your source. It
doesn't care that you used &main::printok before you defined it.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
G

Gunnar Hjalmarsson

Bart said:
use lib '/path/to/mm_e.pm/';

That line does not help much. @INC is supposed to contain paths to
*directories* with .pm files, not paths to .pm files directly.

I suppose that the reason why it still works is that mm_e.pm happens to
be located in the same directory as p.pl, while your @INC includes the
current directory.

Others have answered your actual question.
 
B

Bart Van der Donck

Arne said:
Maybe you misinterpret the filename and the packagename, which happen to
be different?

Yes this one - I have always worked with same names there.

Bart
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top