views:

76

answers:

2

I am new to Perl and I face following issue, having no clue why following is not working.

My Perl module contains:

package PACK2;
use Exporter;
@ISA = ('Exporter');
@EXPORT_OK=('whom');

sub why(){
    print "why\n";
}

sub whom(){
      print "whom\n";
}
1;

My Perl file contains:

#!/usr/bin/perl -w

use pack;
use pack2 ('whom');

PACK::who();
&whom();

I run this program and can't find whom:

perl use_pack_pm.pl

who
Undefined subroutine &main::whom called at use_pack_pm.pl line 7.
+7  A: 

Perl is a case-sensitive language. I don't think modules "pack2" and "PACK2" are the same. (But I haven't actually tested this.)

reinierpost
Yes your are right after changing the file name to PACK2.pm it works fine. In that case perl behaves two different way in two different case. In my above example use pack; the first module is working fine.here is what contain in the first module..........................> cat pack.pmpackage PACK;sub why(){ print "why\n";}sub who(){ print "who\n";}1
You're right. They are wholly different packages. Thus, Perl *loads* `pack2.pm`, and does not `use` PACK2 package. Probably, using warnings would have told you this.
Axeman
i used warning which didn't give me any warning message.#!/usr/bin/perl -w
@user427394, well that's a part I didn't research. It's a good thing to remember that Perl is a UNIX-world language: case sensitive. And a UNIX hack sees this almost immediately. It runs on a variety of platforms, but it is most at home on *nix.
Axeman
use strict; and use warnings; as well don't catch that error. :(
Sometimes case-preserving but case-insensitive filesystems let people get away with these case issues.
brian d foy
Also be aware that not all shells honour options on the shebang line. That's why it's always better to write use warnings instead of appending -w there.
reinierpost
@reinierpost, perl will always try to read the switches on the #! line of the executed file even if the shell doesn't. `use warnings;` is still preferable for other reasons, like not enabling warnings in code expecting to be run without warnings or losing warnings because a module is used in a script that doesn't use `-w`.
Ven'Tatsu
@brian d foy: Yes, I've seen this issue on "Case-preserving but case-insensitive" file systems too. Like NTFS. :)
Robert P
+3  A: 

Internally use pack2 ('whom'); is translated to something like

BEGIN {
    require pack2;
    pack2->import('whom');
}

Except that perl will check to see if it can call import on pack2 before it tries to call it. In your example there is no package named pack2 and so no import function to call. If your package name and file name match then perl would find the import function provided by Exporter.

There is no warning for this because Perl has a hard time telling when this was done deliberately. Most OO modules won't export any functions or variables and so they don't privide an import function.

Ven'Tatsu
Thnx all it answer my Question.
I regret to say that I can only give a single +1. I've explained many times that file names and package names don't have to match, one file can contain multiple packages, one package can be spread across multiple files, etc., but never noticed that `use` and Exporter only work together properly if the file name and package name match.
Dave Sherohman