Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-20 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found