Mailing List Archive

Re: perl modules
CC'd to perl5-porters so they know what some people find hard to
find in the docs.

> From: otisg@panther.middlebury.edu (Otis Gospodnetic)
>
> Hi Tim,
>
> I read and printed out your excellent modules.list (I think that's the only
> place where one can actually learn how to write modules, and not just use the
> existing ones).

Hopefully Perl5.002 will come with more and better docs.

> I read through your list a few times, but I still have a few questions, so if
> you have time I'd really appreciate if you could help me a bit.
>
> Let me try being organized for once:
>
> 1) I noticed all existing perl modules have things like =head, =cut, =break,
> etc . in them, but I couldn't find explanation about those things even
> in your modules.list. What is it ? Is that something for man pages ?
> Any place where I could see what each of them means ?

man perlpod

> 2) I noticed some modules have __END__ and some don't, and I think all have 1;
> at the end. this 1; is a MUST, right ? What about __END__ ? Is it
> necessary in a module or ?

The 1 is required by require. Check the perlfunc manual entry for require.
Check the perldata and perlrun man pages for __END__.

> 3) Is it okay if my module relies on some other modules ?
> If yes, then if I have something like 'use SomeModule;' in my module,
> do I have to call things from SomeModule like SomeModule::subroutine
> or can I refer to them as just &subroutine; ?

You can use just &subroutine if SomeModule 'exports' the function name
'subroutine' to its users. Checkout Exporter.pm. The command 'perldoc Exporter'
may work if you're using a recent perl.

> 4) finally, this might be more of a general perl question - I'm thinking about
> writing my modules and its subroutines in a true/false manner, so to
> speak, so basically I want to return false if the subroutine in my
> module didn't finish properly (didn't do what it was supposed to for
> whatever reason, most probably because user screwed up), and true if
> the subroutine did what it was supposed to.
> For example:
>
> sub check_info {
> my (@Required) = @_;
>
> foreach $item (@Required) {
> return -1 if !$query->param($item);
> }
> return 1;
> } # end of check_info subroutine
>
> So I'm trying to return -1 fr false and if everything fine then return
> 1; returns true. is that how return work ?

Yes.

> What can return return ? 1 ? -1 ? 0 ? "a string" ?

It can return any scalar or list value.

> I looked in the Camel and Llama book but
> they don't talk muc about it and have only a few examples.

Most people think it's a simple concept and so it doesn't get much attention.

> I was thinking, if someone (like I) used this module he's have to say
> &subroutine_from_module || die "blah blah something";
> of course I'd make that die be a sub or something, but that's a
> detail.
> So I was wondering if that is a good/fine/ok or bad way to design
> modules ?

Yes it's very common in perl but note carefully that _both_ 1 and -1 are TRUE!
Use 0, "" or undef for false. E.g.,

foreach ... {
return 0 if ...;
}
return 1;

> 5) How do I test my module ? Just write a simple driver for it and see if it
> breaks ?

Yes. The URI::URL module has a very good builtin test mechanism.

> Okay, I don't want to bother you any more,

Thanks ;-)

> Thanks for your time and knowledge !
>
> Otis

Tim.