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

Probably a dumb question, but I have been assuming I know the answer...

I noticed that some of my modules have a "return 1" near the top of the file, while others, I had put it as the last line. (I am still a Perl newbie)

Does this affect package globals or subroutine definitions?

Is there a good standard?

Note that I am not asking where to put my subs() though ;-)

ps

There is a cgi "bug" on this page that if you put double quote something in the title, after you hit preview, everthing after the first double quote is blasted

Replies are listed 'Best First'.
Re: the "return" in a module
by Juerd (Abbot) on Mar 09, 2002 at 01:59 UTC

    Is there a good standard?

    It's very common to simply end with 1;. It's needed, because perl uses the last expression that was evaluated to determine if the module was loaded correctly. When it's not there, but the module still works, that means the last expression was true, but it's considered bad style to depend on that.

    I've also seen people use strings over there (q/Murphy's law is recursive. Washing your car to make it rain doesn't work./;), or 42;, and other values representing truth.

    44696420796F7520732F2F2F65206F
    7220756E7061636B3F202F6D736720
    6D6521203A29202D2D204A75657264
    

Re: the "return" in a module
by mpeppler (Vicar) on Mar 09, 2002 at 00:56 UTC
    AFAIK the placement of the return 1; (or just 1;) in the module file makes no difference.

    Michael

      Actually it can make a difference in two ways.

      The first is that if you put a return before some code that you want executed, it won't be because you returned.

      The second is that if you just use a true value but don't return, and accidentally put some executable code after it (eg a variable initialization) with a false value, then your module will fail to load.

      Both should be obvious if you are bitten by them. But I like putting the true return value at the end anyways.

        That makes sense - thanks.

        Michael

Re: the "return" in a module
by theguvnor (Chaplain) on Mar 09, 2002 at 05:13 UTC

    Not that it's yet a concern, I believe (but could not find the evidence!) it was announced that in Perl 6 modules will no longer be required to return true. (And where is that Exegesis 4 anyway? ;^)

    ..Guv