in reply to How to use require...

Simply:
sub do_login { require "login.pl"; goto &do_login; }
The problem is that if you have warnings on, you'll get a "subroutine redefined" message. You can turn your login library into a login ".pm" file rather easily and then you can use autouse, which is core.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: How to use require...
by kiat (Vicar) on Jan 03, 2004 at 07:33 UTC
    Thanks, merlyn!

    The following uses 'autouse' and I'm getting the results as desired. I just want to make sure I'm getting it correctly...:)

    ######### Main script ######### index.pl #!C:/perl5.8/bin/perl.exe use CGI qw(:cgi); use Login; use autouse Login => qw(&do_login &do_login2); my $query = get_param('action') || 'default'; my %actions = ( default => \&default, login => \&do_login, login2 => \&do_login2 ); &{ $actions{$query} } ######## Auxiliary script ######## Login.pm sub do_login { # show login page } sub do_login2 { # process login } 1;
    updated

    I commented out "use autouse Login => qw(&do_login &do_login2);" and it still works. Am I missing something?

      I commented out "use autouse Login => qw(&do_login &do_login2);" and it still works. Am I missing something?
      Ahh, you don't need both that and the previous line. That's like wearing both suspenders and a belt.

      Comment out the use Login line.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        I commented out use Login and only have use autouse Login => qw(&login &login2); and got the error: Undefined subroutine &main::login called at C:/apache/www/index.pl line 87.

        It was okay with use Login uncommented. The code I've is as follows

        ######### index.pl #!C:/perl5.8/bin/perl.exe use strict; use warnings; use diagnostics; use CGI::Carp qw(fatalsToBrowser); use lib 'C:/apache/www/modules'; #use Login; use autouse Edit => qw(&login &login2); my %actions = ( default => \&default, login => \&login, login2 => \&login2 ); # Thanks to fruiture for pointing this out... $query = 'default' unless exists $actions{ $query }; &{ $actions{$query} } sub default { # show default page } ######### Login.pm use strict; use warnings; use diagnostics; use CGI::Carp qw(fatalsToBrowser); sub login { # display login page } sub login2 { # process login } 1;
        Also, I'm wondering why in the Login.pm it doesn't seem I need the usual exporting stuff as follows:
        # The package declaration and the code to # do the exporting are absent in Login.pm # Do I need to put them in? package Login; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(&login &login2);

        Let me know if I've made any blunders :)