http://qs321.pair.com?node_id=98761

John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I just noticed that a work-in-process didn't have a 1; at the end. The last thing in the .pm file was a sub definition.

It seems to me that this used to be a problem. Just what is the "value" of a sub definition? Is this something that changed at some point? I seem to recall that this used to make the pm fail to load.

—John

  • Comment on is 1; at the end of a PM still necessary in 5.6.1 ?

Replies are listed 'Best First'.
(MeowChow) Re: is 1; at the end of a PM still necessary in 5.6.1 ?
by MeowChow (Vicar) on Jul 22, 2001 at 09:25 UTC
    Subroutine definitions don't have values becuse they're declarations, not expressions. Anonymous subroutine assignments, however, obviously evaluate to their post-assignment lvalues.

    Your module probably reduces to this:
      
    package Foo; ... 1; # or any expression that returns a true value sub bar { ... }
    Because the subroutine declaration has no value, the value of the last expression before the subroutine is returned.

    Simplifying, it's a good thing to end your modules with a `1;', though in reality, only the last evaluated expression needs to return a true value for Perl to load your module.

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      I see. So rather than a non-value-carrying statement causing the module's return value to not be true (undef or some more specific error), it is just ignored in favor of the last thing that was an expression.

      I suppose that's true in general?

      Simplifying, it's a good thing to end your modules with a `1;'

      How boring. I prefer 69; or "peace";.

      srand 3.14159; print join("", sort{rand 1<0.5}map{$_^"\037"}split m{ }x,"qmptk|z~wOzm??l]pUqx^k?j"),",\n";
        How boring. I prefer 69; or "peace";.

        One of my colleagues ends his modules with:

        42;
(crazyinsomniac) Re: is 1; at the end of a PM still necessary in 5.6.1 ?
by crazyinsomniac (Prior) on Jul 22, 2001 at 09:15 UTC
    From permod it says:
    "1;  # don't forget to return a true value from the file"
    a subroutine is a true value ;-)

    I don't remember this not working in prior versions.

    What exactly is the value of a subroutine?it's always true, i know that much ;)

    My test code worked on all my ActivePerl and IndigoPerl installations, as well as, jcwren's machine.

    package test2; require Exporter; use vars qw/@ISA/; @ISA = qw(Exporter); sub new { my $me = shift; return bless {},$me; } sub Ed_Guine { return -reverse oy}; __END__
    and the test line: perl -Mtest2 -e"$_=new test2;print $_->Ed_Guine;"

    update:
    Apparently, what is happening is the return value is true, because @ISA = qw(Exporter); evaluates to true. I tested by inserting 0; in between the subs and such. MeowChow is right.

    So, to avoid all this confusion, just make sure you have 1; before __DATA__ (or __END__ depending on what you use).

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      So why did it work just fine when I did forget?
Re: is 1; at the end of a PM still necessary in 5.6.1 ?
by Anonymous Monk on Jul 23, 2001 at 05:58 UTC
    I find that 1; at the end of a file tends to be hard to see and sometimes gets removed accidently. Is there any reason I can't use return(1); at the end, is there any differences or problems to worry about in doing this?
      It won't be hard to see and probably won't get deleted if you use it the way perlmod does: (thanks crazyinsomniac)

      1; # don't forget to return a true value from the file

      The comment makes things a lot clearer, and should make someone think twice before deleting it.

      I really don't know what return from the main top-level code of a module does.

      I tried changing the 1 to

      return 1; # script loaded OK.
      on the PM I'm working on, and nothing changed. So I suppose it's just fine. I'd do without the extra parens, though: return is not a function, and I'm not expecting it to be "called" in list context.

      —John

      The other way to go is just:
      1
      then if you put anything after it you get an error.
      But I've said this before and I'm sure everyone else considers it bad practice and I suppose their right. It just always seems counterproductive to me to add the ';' when that only serves to hide it.

        p