Perl and packge

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


Can somebody explain me when use *.pm files and when uses *.pl?

must be classes in *.pm files?

Re: Perl and packge

Quoted text here. Click to load it

You use a .pm file when you want it to be picked up by

    require Foo::Bar;


    use Foo::Bar;

If you are happy to load it with
    require "/path/to/";

you may call the file whatever you like, but *.pl is conventional.

Quoted text here. Click to load it

No, not at all, though it is *usual* for a class Foo::Bar to be defined
in a file Foo/, so it can be loaded with

    use Foo::Bar;

See perldoc -f require, perldoc -f use and perldoc perlmod.


Re: Perl and packge

cvrčak wrote:

Quoted text here. Click to load it

Will this do?

In short: there are two ways to specify files to load (require): the
special package syntax (Foo::Bar), and the plain (Unix) file path
format: "Foo/". The former is a special syntax that is recognized
as syntactic sugar for the latter, but only in source code. If you
specify the file as a string, or in a (scalar) variable, you *must* use
the file path format.

That's one way in which ".pm" special as a file extension. The second is
that with "use", Perl will treat Foo::Bar both as a file name (after
conversion), and as a package/class name, for which it will try to do a
"import" class method call. That's the main reason for which module
names and package names are commonly the same.

Quoted text here. Click to load it


Classes are defined as packages, and package declarations can be in any
type of source files. See above as to why package and file name are
usually the same. But it's not uncommon to have extra (utility) packages
in the same source file.


Re: Perl and packge


Site Timeline