What is a script? A body of Perl code that executes straight through based on some global data (eg @ARGV), right?

What is a function? A body of Perl code that executes straight through, generally based on private data passed in through @_, right?

While there are some technical differences in how you write them, there is no real difference in what you can do with a script and a function. However what you have written to be called as a function can trivially be rewritten as a script that just calls that function. The converse is not true, what is written as a script may take more work to write as a function.

The upshot of this is that when you want to encapsulate a bunch of functionality, you are generally better off thinking about making it a function than making it a script. And you are better off reaching for functions than scripts.

If you apply that principle in this case, your problem resolves itself. Can you write a program that calls two functions, one to produce the main content and another to produce the side menu? The answer is that you can. And if you put the function for the side menu into a module, you can share that content across many pages very easily.

In fact that is basically how this site works. If you look at the page, you will see that it is broken up into various "nodelets". Filling in each nodelet is just a question of calling a function. And if you register for the site you will find out that what nodelets you have is itself configurable - and behind the scenes just represents a series of choices about which functions to call.

But the principle remains. Whenever possible, encapsulate functionality in a function, not a script. Do that consistently and you will find that a lot of technical problems resolve themselves very directly and naturally, in fact often so smoothly that you don't even notice that you might have had a problem if you had done things a different way.


In reply to Re (tilly) 1: 2 script in a page by tilly
in thread 2 script in a page 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.