Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

why do some perl files end with 1;

by nikhil.patil (Sexton)
on Feb 21, 2008 at 11:05 UTC ( [id://669235]=perlquestion: print w/replies, xml ) Need Help??

nikhil.patil has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have noticed that some perl files end with 1;
Is there any particular reason why such a construct is required? What is its significance?

Replies are listed 'Best First'.
Re: why do some perl files end with 1;
by olus (Curate) on Feb 21, 2008 at 11:15 UTC

    When a module is loaded (via use) the compiler will complain unless the last statement executed when it is loaded is true.

    For that reason, Perl modules end with 1;

Re: why do some perl files end with 1;
by moritz (Cardinal) on Feb 21, 2008 at 11:16 UTC
    A module that is useed or required by other perl code needs to return a true value.

    That's what the 1; is for. See perlmod for details.

Re: why do some perl files end with 1;
by parv (Parson) on Feb 21, 2008 at 12:02 UTC
    If you don't like 1, (after reading other replies) you can have any other true value, say 42, the strings 'undef' or 'zero', etc. I wonder what 0E0 would give ... nope, that doesn't work.

        I share my favorite HEREDOC delimiter in the same spirit.

        print <<"AsPhynCteRsAyswHat";

        Another dev did write to ask me once.

      Module authors have the opportunity to use a string in this position. They could say

      • 'Hello, world!';
      • 'Have a nice day!';
      • 'Thank you for using Acme::Foo!';

      It's like a comment, except if the user deletes it, their program breaks.

      Some CPAN authors do use this opportunity to have a last word. Does anybody have a favorite example?

        Some good ones.
        • Explicit and correct: from List::Maker
          1; # Magic true value required at end of module
        • Epitaph: at Acme::DonMartin
          'This module is dedicated to Don Martin' ; 1931-2000
        • Identify your package: from nuffin's packages:
          __PACKAGE__;
        • Remarkable quote: at WWW::CPAN
          "I didn't do it! -- Bart Simpson";
        • the end of Sex
        Favorite example:

        return 1; # module loaded successfully

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://669235]
Approved by olus
Front-paged by jdporter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found