Hi Monks,

I'm trying to split up a script into a number of component scripts on the advice at All-in-one script vs independent scripts. In the code below, login.pl is loaded at the start of the script, whether or not it's actually needed.

######### Main script ######### index.pl #!C:/perl5.8/bin/perl.exe use CGI qw(:cgi); my $query = get_param('action') || 'default'; # login.pl contains the subroutine do_login() require "login.pl"; my %actions = ( default => \&default, login => \&do_login ); &{ $actions{$query} } sub default { # show default page } ######### Auxiliary script ######### login.pl sub do_login { #process login } 1;
That works but I would like to load login.pl as and when it's called. How do I achieve that? Something like:
######### Main script ######### index.pl #!C:/perl5.8/bin/perl.exe use CGI qw(:cgi); my $query = get_param('action') || 'default'; my %actions = ( default => \&default, login => \&login ); &{ $actions{$query} } sub default { # show default page } sub login { require "login.pl"; } ######### Auxiliary script ######### login.pl sub do_login { #process login } 1;
How do I accomplish what I'm trying to do above?

In reply to How to use require... by kiat

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.