I'm sure this is a bad idea, but I can't see why.

I have a CGI which, depending on user input, does one of a guzillion different things. Each of these things gets done by a subroutine. So for example with subroutines foo, bar and baz, they might be called thus:
if ($Action eq 'foo') { &foo; } elsif ($Action eq 'bar') { &bar; } else { &baz; }
But as far as I can see this means that whenever the CGI is called it compiles all the different subroutines, even though it actually only ever runs one of them. And this is true whether the subs are in a lib or in the script itself.

Then I started listening to The Voices. And what they said was, put the code from each subroutine in its own file, call the files foo.pl, bar.pl, baz.pl, and call them with
if ($Action eq 'foo') { do 'foo.pl'; } elsif ($Action eq 'bar') { do 'bar.pl'; } else { do 'baz.pl'; }
Now, I can see one reason why this is bad: errors don't show up so easily - you just get a blank screen if foo.pl is n.b.g. But actually that's not too bad, because they're all fairly stable scripts that don't need to change much - and if I'm getting errors, that's a bigger problem than just not being able to track them very easily.

I can't see any other reason why this is bad... but it feels bad. It may be that I've made a mistake about how compiling works and this approach doesn't save any time. I don't think so, but request enlightenment. On the other hand, if it does work - why is it the wrong thing to do?

§ George Sherston

In reply to Saving compile time by running subroutines as separate files with 'do' by George_Sherston

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.