perlfan99 has asked for the wisdom of the Perl Monks concerning the following question:

What is difference between .pl and .pm file? Is .pm just a convention to distinguish a library/module file from other files? It seems I can simply rename a .pm to .pl file, and everything still works.

Thanks!

Replies are listed 'Best First'.
Re: perl file extenson question
by toolic (Bishop) on Jul 20, 2008 at 03:41 UTC
    According to Perl Modules in perlmod:
    All Perl module files have the extension .pm. The use operator assumes this so you don't have to spell out "Module.pm" in quotes.
    This is at least one subtle difference.
      that make sense. thanks!
Re: perl file extenson question
by McDarren (Abbot) on Jul 20, 2008 at 03:11 UTC
    Yes, correct. It's just a convention.
    .pm for modules, .pl for Perl programs/scripts.
    Both are just text files, and the perl interpreter (afaik) doesn't care about file extensions, so you can name them pretty much anything you like.
    Keeping with convention is usually a good idea, though :)
Re: perl file extenson question
by oko1 (Deacon) on Jul 20, 2008 at 15:38 UTC

    Well, no - in fact, everything won't still work.

    ben@Tyr:/tmp$ echo -e 'sub greet { "Hello, world!\\n" }\n1;' > hello.p +l ben@Tyr:/tmp$ perl -Mhello.pl -we'print greet' syntax error at -e line 0, near "use hello." Execution of -e aborted due to compilation errors. ben@Tyr:/tmp$ perl -Mhello -we'print greet' Can't locate hello.pm in @INC (@INC contains: /etc/perl /usr/local/lib +/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/per +l5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl /us +r/local/lib/perl/5.8.7 /usr/local/share/perl/5.8.7 .). BEGIN failed--compilation aborted.

    As toolic pointed out, you can't load a '.pl' file as a module; you'd have to use "require" (or "do") instead of "use". Which opens up a very large can of worms.

    Libraries are loaded at run-time; modules are loaded at compile time. This, in itself, is a huge difference that allows modules much, much more scope for action than libraries. "use Xyz" also imports the symbols and the semantics from package "Xyz" into the current one - not something that happens with "require Xyz.pl" (which just checks to see if Xyz.pl has already been loaded, updates %INC, and "wraps" a lexical scope block around the content of "Xyz.pl".)

    Last of all, libraries are pretty much deprecated. They're a Bad Idea - at least, they've come to be seen as one - for a number of reasons (namespace conflicts, among other reasons.) In general, the preferred thing is to "use" a module - and to create modules instead of libraries if you're building things.

    For more info, please see 'perldoc perlmodlib'.

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells