One useful way to think of a module is as a collection of related subroutines. They should be designed to be easy to use by any programmer without regards to the internals. Now think about a script which looks like this:

#!/usr/bin/perl use warnings; use strict; my @files = find_files(shift); foreach my $file (@files) { eval { process_file($file) }; die "Could not process $file: $@" if $@; } sub find_files { .... } sub process_file { .... }

With that, you have reused whatever code you have in &process_file. That's good. But what if another program needs the same functionality?

package My::FileProcessor; use strict; use warnings; use base 'Exporter'; our @EXPORT_OK = qw(find_files process_file); sub find_files { .... } sub process_file { .... } 1;

And your original code becomes this:

#!/usr/bin/perl use warnings; use strict; use My::FileProcessor qw(find_files process_file); my @files = find_files(shift); foreach my $file (@files) { eval { process_file($file) }; die "Could not process $file: $@" if $@; }

That code is now shorter, but more importantly, other programs can more easily share the functionality provided by My::FileProcessor.

Does that help?

Cheers,
Ovid

Ich verstehe nur ein bisschen Deutsch. -- Ovid.

New address of my CGI Course.


In reply to Re: Subroutines vs Modules by Ovid
in thread Subroutines vs Modules by sub_chick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.