http://www.perlmonks.org?node_id=289248


in reply to A simple example OO script for total beginners

1; # this 1; is neccessary for our class to work

Although this is true, I think that it would be more instructive to indicate what the 1; statement does, and why it is neccessary for the package to work.

The module has to return something to indicate success ie. a non-zero/null value. Someone new to the language might not know that by default perl will return the value of last statement. Perhaps you should use an explicit return statement, something like:

return 1; # let program know package has successfully compiled

Rather than just mentioning that there are other tutorials available, it would also be useful to link to some of them from this post.

</ajdelore>

Replies are listed 'Best First'.
Re: Re: A simple example OO script for total beginners
by nothingmuch (Priest) on Dec 30, 2003 at 01:29 UTC
    The module has to return something to indicate success

    I'd like to clarify. This is indeed not necessary for the class to work, but for perl to assume the inclusion of a file went well. When you do, use or require a file containing perl code, the value it returns to the caller is used to indicate proper loading, execution, or whatever the role of that code was. It is customary to end any perl code, especially that which is not a script pe se in a simple

    1;

    because this is not erroneous as using return is when there's nothing to return to (like in a simple script file), but does return truth when truth is needed, because in Perl the last value evaluated in some scope which returns (file, subroutine), is the return value.

    In short, it's not necessary for the class to work, but it is necessary if the module which contains the class will be loaded via an external file inclusion, instead of being supplied in the same file.

    -nuffin
    zz zZ Z Z #!perl
Re^2: A simple example OO script for total beginners
by shobhit (Sexton) on Apr 27, 2007 at 19:53 UTC
    An excellent article which got me started with OOP in Perl. This is exactly the kind of thing I was looking for.
    However, I would like to point out something. The
    1;
    at the end is not entirely necessary. We can just as well use,
    our @ISA=qw/Exporter/;
    Which I guess is a better way of telling the world, that you may import this package/class in your own scripts.