perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Pang <pa...@earthlink.net>
Subject Re: mod_perl problems - require file
Date Wed, 13 Dec 2006 15:35:43 GMT

>
>Sorry, one other thing I didn't mention from the start is that the errors are
>happening inconsistently.  We can rarely duplicate the error, but see it
>showing up in log files, and a QA can periodically replicate it.
>
>So do you think it be related to a bad Apache process, rather than software
>based?
>

Nothing about Apahe.I had meet the same problem as you,thanks to some kind guys here like
Perrin,I got it resolved.
Now I would explain it to you and hope it's helps.

Given the TT1.pm and TT2.pl as below:
 
$ cat TT1.pm 
package TT1;
 
use strict;
require Exporter;
our @ISA = qw/Exporter/;
our @EXPORT = qw/&testA/;
 
sub testA {
 
    print "hello,test in process $$\n";
}
 
1;
 

$ cat TT2.pl 
 
use strict;
 
sub testA {
 
    print "hello,test in process $$\n";
}
 
1;
 
Here TT1.pm is a perl module,it export testA subroutine by default.TT2.pl is a perl lib file,it
has only a subroutine testA.
 
Now we use/require these two files in two perl packages,
 
$ cat t6.pl 
 
{
    package TEST1;
    use strict;
    use Data::Dumper;
    use TT1;
    #require "TT2.pl";
 
    testA();
    print Dumper \%INC;
 
    1;
}
 
{
    package TEST2;
    use strict;
    use Data::Dumper;
    use TT1;
    #require "TT2.pl";
 
    testA();
    print Dumper \%INC;
 
    1;
}
 
This can work well really.Where in the second package,when we use TT1,it shouldn't do the
real 'load' action to the TT1.pm,since TT1.pm has been loaded by the first package and it
is kept in the %INC in the same process.As we know,a 'use' statement is equal to:
 
require FOO;
FOO->import(...);
 
Since the first 'require' action doesn't happen in the second package,so it just do the 'import'
statement.It can do because TT1.pm package is already loaded by the first packages,it's located
in %INC.Also FOO->import() is the way of accessing package's subroutine by full package
name.
 
Ok,let's change the script now,
 
$ cat t6.pl 
 
{
    package TEST1;
    use strict;
    use Data::Dumper;
    #use TT1;
    require "TT2.pl";
 
    testA();
    print Dumper \%INC;
 
    1;
}
 
{
    package TEST2;
    use strict;
    use Data::Dumper;
    #use TT1;
    require "TT2.pl";
 
    testA();
    print Dumper \%INC;
 
    1;
}
 
The second package can't be run at all.As we see,TT2.pl isn't declared as a package,it doesn't
have the name space of itself.When 'require' TT2.pl,the subroutine in the TT2.pl should be
inserted into main script's name space.Now the first package has 'require' TT2.pl and insert
the testA() subroutine into its name space,and also save the libfile's name in %INC.While
when the second package try to require TT2.pl,it find TT2.pl has existed in %INC,then it skip
the load action,so the testA() subroutine shouldn't be inserted into the second package's
name space.So it failed when executing the testA().


--
Books below translated by me to Chinese.
Practical mod_perl: http://home.earthlink.net/~pangj/mod_perl/
Squid the Definitive Guide: http://home.earthlink.net/~pangj/squid/

Mime
View raw message