The trivial answer is to just rename it, and make sure it returns a true value (this is usually done by making sure the last line of code is "1;").

More generally you probably want to:

Here's an example script...

# greeting.pl print "What is your name?\n"; chomp(my $name = <STDIN>); print "Hello, $name!\n";

You could refactor it into a module like this:

# Greeting.pm package Greeting; sub say_text { my $handle = shift; print $handle "$_\n" for @_; } sub read_answer { my $handle = shift; chomp(my $answer = <$handle>); return $answer; } sub question { return "What is your name?"; } sub response { my $name = shift; return "Hello $name"; } 1;

Which could be used like this...

# greeting.pl use Greeting; my $in = \*STDIN; my $out = \*STDOUT; Greeting::say_text($out, Greeting::question()); my $name = Greeting::read_answer($in); Greeting::say_text($out, Greeting::response($name));

After that, something to consider is making your code more object-oriented, thus allowing people to subclass it, overriding parts of its functionality.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: How to convert Perl file to a perl module(.pm file). by tobyink
in thread How to convert Perl file to a perl module(.pm file). by Anonymous Monk

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.