7

Let's say I create a class named Bar. The file Bar.pm starts

package Bar;

To avoid colliding with other Bar classes, I put the file in a subdirectory Foo. So now, when I use the class, I have to write

use Foo::Bar;

My question is, do I need to change the name of the class to Foo::Bar? In other words, do I need to change the first line of Bar.pm to

package Foo::Bar;

? The problem is, if I do this, I now have to refer to the class as Foo::Bar everywhere, e.g.

my $obj = Foo::Bar->new();
Foo::Bar->doClassMethod();

which is annoying (the same problem was discussed in this question), especially since I am fond of class methods.

5 Answers 5

9

Yes, you have to change the name of the package to exactly match that of the use.

2
  • Can you expand on the meaning of "have to" a little? I ask because it works (compiles and runs) without changing the package name. What are the consequences of not changing it?
    – shawkinaw
    Feb 29, 2012 at 18:41
  • 9
    @shawkinaw If you don’t, then the use statement’s import method will not find the right class, and other class methods including constructors will (often) misfire. It’s like getting the case wrong in the use statement. It kinda works on case-insensitive filesystems, but only by accident, and all kinds of other stuff gets hosed. Don’t do that. Just make your file, package, and use all line up exactly. Years from now you might have some reason to do something else, but that comes only with severe and battle-word experience. It’s still dubious.
    – tchrist
    Feb 29, 2012 at 18:44
2

If you think having a class name Bar might conflict with other classes named Bar then simply moving the file won't help. If your program eventually uses both Bar and Foo::Bar then both will have been loaded into the same namespace. At that point what happens to your program is anyone's guess.

If don't want to type long class names then you can use a variable to hold the name.

use My::Long::Class::Name::For::Bar; 
my $bar_class = 'My::Long::Class::Name::For::Bar'; 

$bar_class->class_method(); # the same as My::Long::Class::Name::For::Bar->class_method()
1
  • True, but presumably you wouldn't load two different Bar classes at the same time. But I see your point. It would be nice if Perl let you refer to classes by a shortened name, but only if there was no ambiguity. Tcl, for all its faults, lets you invoke procedures by a shortened name as long as it is unique.
    – shawkinaw
    Mar 1, 2012 at 17:06
1

You don't strictly need to (i.e. it's a style decision, not something the compiler enforces), but it is a good idea to follow the relevant guidelines set out in perlmod/perlnewmod to make the software easily distributable.

IOW, if long names bother you, get an editor with autocompletion.

1

Quoting perldoc -f require:

If EXPR is a bareword, the require assumes a ".pm" extension and replaces "::" with "/" in the filename for you, to make it easy to load standard modules. This form of loading of modules does not risk altering your namespace. In other words, if you try this:

    require Foo::Bar;     # a splendid bareword 

The require function will actually look for the "Foo/Bar.pm" file in the directories specified in the @INC array.

So, yes, it's convention that if your module is located at $dir/Foo/Bar.pm for some $dir in @INC, then it must be called Foo::Bar.

0

The package Local is reserved just for this use. For example, if you create a package Bar.pm, you could call the package Local::Bar and know it won't clash with something in CPAN.

By default, @INC includes the current directory, so you could create a Local subdirectory, and then put all of your packages there. In a company, I'll divide up the Local package namespace into groups and even names of the developers. For example, I could use Local::Cm::Bar or Local::David::Bar. That way, if I decide I could use Alice's Bar class, I could simply include Local::Alice::Bar.

Yes, the standard CPAN rules state not to use your name in package namespaces, but Local packages never go into CPAN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.