Some other posts suggest writing modules, which is the normal thing for programmers to do. But as you are
"NOT a programmer" :-)
perhaps the more old-fashioned use of files that contain subroutines for you to reuse would be easier to implement?

For example, in your brand new program that does the day's latest challenge (and perhaps you start with a virtual template of a script that you know functions vaguely similarly) you have something like:

require 'my_general_subs.pl'; my $var1 = shift; my ( $var2 ) = mgs::get_data( $var1 );

Then, in a file named my_general_subs.pl that is in one of the @INC directories (thus allowing my_general_subs.pl to be in the current directory), you start with:

package mgs;

followed by subroutine after subroutine (i.e. no "main" stuff required in this library file, just cool subs that you want to reuse and maybe some initializing statements). Eventually, the file even contains the subroutine you are invoking as in:

sub get_data { my ( $name, # optional comment explaining @name $grade, # optional comment for optional $grade ) = @_; my $answer; # Do stuff with $name, $grade, concoct some kind of $answer # ... return ( $answer ); }

Keeping to the general rule of one package statement per library file (i.e. files like 'my_general_subs.pl' as above), you create several of these library files to contain related subroutines. Suddenly, code reuse becomes a piece of cake! (Check out how to pass filehandles as parameters and also ways of redirecting STDERR to always point to the same file for more robustness here.)

As you are alluding, it's great to write code just once so that you aren't always rewriting the same old thing (also multiple-script maintenance is easier), but since Perl code is fairly easy to generate anew as necessary, you get the best of code reuse without being locked into a one-template-fits-all approach.


In reply to Re: Re: Re: Perl Templates by ff
in thread Perl Templates by ellem

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.