For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2004 > Question about import user defined modules.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Question about import user defined modules.
ckacka

2004-03-29, 8:35 am

I write code as follows:

========================================
==========================
===== /home/ckacka/public_html/cgi-bin/ckacka/Module.pm

package Module;

use strict;
use Exporter;
use vars qw/$VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/;

$VERSION = 0.91;
@ISA = qw/Exporter/;
@EXPORT = ();
@EXPORT_OK = qw/&wwwdoc &wwwcgi &docdir &cgidir/;
%EXPORT_TAGS = (
':default' => [ qw/&wwwdoc &wwwcgi &docdir &cgidir/ ]
);

# Global varibles.
# Depends your host's setting.
sub wwwdoc { return 'http://localhost/~ckacka'; }
sub wwwcgi { return 'http://localhost/~ckacka/cgi-bin'; }
sub docdir { return '/home/ckacka/public_html'; }
sub cgidir { return '/home/ckacka/public_html/cgi-bin'; }

1;

========================================
==========================
===== /home/ckacka/public_html/cgi-bin/home.cgi

#!/usr/bin/perl -w


use Fcntl qw/:flock/;
use CGI::Pretty qw/:standard/;
use strict;
use diagnostics;

use ckacka::Module qw/:default/;
BEGIN { push @INC, '/home/ckacka/public_html/cgi-bin' }


my $wwwdoc = wwwdoc();
my $wwwcgi = wwwcgi();
my $docdir = docdir();
my $cgidir = cgidir();

....
...
..

========================================
==========================

Run it with ouput:

$ perl home.cgi
Undefined subroutine &main::wwwdoc called at home.cgi line 12 (#1)
(F) The subroutine indicated hasn't been defined, or if it was, it has
since been undefined.

Uncaught exception from user code:
Undefined subroutine &main::wwwdoc called at home.cgi line 12.

========================================
==========================

Thanks

ckacka


Anno Siegel

2004-03-29, 9:45 am

ckacka <ckacka@163.com> wrote in comp.lang.perl.misc:
> I write code as follows:
>
> ========================================
==========================
> ===== /home/ckacka/public_html/cgi-bin/ckacka/Module.pm
>
> package Module;
>
> use strict;
> use Exporter;
> use vars qw/$VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/;
>
> $VERSION = 0.91;
> @ISA = qw/Exporter/;
> @EXPORT = ();
> @EXPORT_OK = qw/&wwwdoc &wwwcgi &docdir &cgidir/;
> %EXPORT_TAGS = (
> ':default' => [ qw/&wwwdoc &wwwcgi &docdir &cgidir/ ]
> );
>
> # Global varibles.
> # Depends your host's setting.
> sub wwwdoc { return 'http://localhost/~ckacka'; }
> sub wwwcgi { return 'http://localhost/~ckacka/cgi-bin'; }
> sub docdir { return '/home/ckacka/public_html'; }
> sub cgidir { return '/home/ckacka/public_html/cgi-bin'; }
>
> 1;
>
> ========================================
==========================
> ===== /home/ckacka/public_html/cgi-bin/home.cgi
>
> #!/usr/bin/perl -w
>
>
> use Fcntl qw/:flock/;
> use CGI::Pretty qw/:standard/;
> use strict;
> use diagnostics;
>
> use ckacka::Module qw/:default/;
> BEGIN { push @INC, '/home/ckacka/public_html/cgi-bin' }
>
>
> my $wwwdoc = wwwdoc();
> my $wwwcgi = wwwcgi();
> my $docdir = docdir();
> my $cgidir = cgidir();
>
> ...
> ..
> .
>
> ========================================
==========================
>
> Run it with ouput:
>
> $ perl home.cgi
> Undefined subroutine &main::wwwdoc called at home.cgi line 12 (#1)
> (F) The subroutine indicated hasn't been defined, or if it was, it has
> since been undefined.
>
> Uncaught exception from user code:
> Undefined subroutine &main::wwwdoc called at home.cgi line 12.
>
> ========================================
==========================


This is awfully messy.

I'm not sure how the module "ckacka::Module" is found at all, but
what you push on @INC has nothing to do with it because it comes
after "use ckacka::Module ...".

In any case, the imports you have planned will not be executed. For
that, the name of the module you load ("ckacka::Module") and the
package from which to export must be the same. However, your
package is just "Module", so the system looks for "ckacka::Module->import",
doesn't find it and shrugs.

Either change the module name (which, in part, determines where the
module file must be stored_), or change the "package" statement in the
module.

Anno
ckacka

2004-03-29, 10:35 am


"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> 写入邮件
news:c4986n$q7k$1@mamenchi.zrz.TU-Berlin.DE...
> ckacka <ckacka@163.com> wrote in comp.lang.perl.misc:
>
> This is awfully messy.
>
> I'm not sure how the module "ckacka::Module" is found at all, but
> what you push on @INC has nothing to do with it because it comes
> after "use ckacka::Module ...".
>
> In any case, the imports you have planned will not be executed. For
> that, the name of the module you load ("ckacka::Module") and the
> package from which to export must be the same. However, your
> package is just "Module", so the system looks for

"ckacka::Module->import",
> doesn't find it and shrugs.
>
> Either change the module name (which, in part, determines where the
> module file must be stored_), or change the "package" statement in the
> module.
>
> Anno


Sorry, I think I make a silly mistake ;-) . I have changed something, and
get another error:

========================================
==========================
===== /home/ckacka/public_html/cgi-bin/ckacka/Module.pm

package Module;

use strict;
use Exporter;
use vars qw/$VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/;

$VERSION = 0.91;
@ISA = qw/Exporter/;
@EXPORT = (); #################### This is Line 10
@EXPORT_OK = qw/&wwwdoc &wwwcgi &docdir &cgidir/;
%EXPORT_TAGS = (
':default' => [ qw/wwwdoc wwwcgi docdir cgidir/ ],
);

# Global varibles.
# Depends your host's setting.
sub wwwdoc { return 'http://localhost/~ckacka'; }
sub wwwcgi { return 'http://localhost/~ckacka/cgi-bin'; }
sub docdir { return '/home/ckacka/public_html'; }
sub cgidir { return '/home/ckacka/public_html/cgi-bin'; }

1;

========================================
==========================
===== /home/ckacka/public_html/cgi-bin/ckacka/Module.pm

#!/usr/bin/perl -w

use Fcntl qw/:flock/;
use CGI::Pretty qw/:standard/;
use strict;
use diagnostics;

BEGIN { push @INC, '/home/ckacka/public_html/cgi-bin/ckacka' }
use Module qw/:default/; #################### This is Line 10

my $wwwdoc = wwwdoc();
my $wwwcgi = wwwcgi();
my $docdir = docdir();
my $cgidir = cgidir();

....
...
..

========================================
==========================

Run it with ouput:

$ perl home.cgi
"default" is not defined in %Blog::EXPORT_TAGS at home.cgi line 10
main::BEGIN() called at
/home/ckacka/public_html/cgi-bin/ckacka/Blog.pm
line 10
eval {...} called at /home/ckacka/public_html/cgi-bin/ckacka/Blog.pm
lin
e 10
Uncaught exception from user code:
Can't continue after import errors at home.cgi line 10
BEGIN failed--compilation aborted at home.cgi line 10.

========================================
==========================

Thanks

ckacka





ckacka

2004-03-29, 10:35 am

Sorry,
========================================
==========================

Run it with ouput:

$ perl home.cgi
"default" is not defined in %Blog::EXPORT_TAGS at home.cgi line 10
main::BEGIN() called at
/home/ckacka/public_html/cgi-bin/ckacka/Blog.pm
line 10
eval {...} called at /home/ckacka/public_html/cgi-bin/ckacka/Blog.pm
lin
e 10
Uncaught exception from user code:
Can't continue after import errors at home.cgi line 10
BEGIN failed--compilation aborted at home.cgi line 10.

========================================
==========================

the "Blog" should be "Module". "Module" is just for my test.


Anno Siegel

2004-03-29, 11:47 am

ckacka <ckacka@163.com> wrote in comp.lang.perl.misc:
>
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> 写入邮件
> news:c4986n$q7k$1@mamenchi.zrz.TU-Berlin.DE...

[import problems]
[color=darkred]
> Sorry, I think I make a silly mistake ;-) . I have changed something, and
> get another error:
>
> ========================================
==========================
> ===== /home/ckacka/public_html/cgi-bin/ckacka/Module.pm
>
> package Module;
>
> use strict;
> use Exporter;
> use vars qw/$VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/;
>
> $VERSION = 0.91;
> @ISA = qw/Exporter/;
> @EXPORT = (); #################### This is Line 10
> @EXPORT_OK = qw/&wwwdoc &wwwcgi &docdir &cgidir/;
> %EXPORT_TAGS = (
> ':default' => [ qw/wwwdoc wwwcgi docdir cgidir/ ],

^

Here is your error. The leading ":" is only used in the calling program,
not in the exporting module. Cf. examples in the Exporter documentation.
Change that and it's going to work.

Anno

[...]
ckacka

2004-03-29, 11:47 am


"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> 写入邮件
news:c49fik$2ao$1@mamenchi.zrz.TU-Berlin.DE...
> ckacka <ckacka@163.com> wrote in comp.lang.perl.misc:
>
> [import problems]
>
and[color=darkred]
> ^
>
> Here is your error. The leading ":" is only used in the calling program,
> not in the exporting module. Cf. examples in the Exporter documentation.
> Change that and it's going to work.
>
> Anno
>
> [...]


Thanks for your help. Now, I have another question:

I use

========================================
==========================
> ^

========================================
==========================
because I see the CGI.pm use:
========================================
==========================
}

%EXPORT_TAGS = (
':html2'=>['h1'..'h6',qw/p br hr ol ul li dl dt dd menu code var strong em
tt u i b blockquote pre img a address cite samp dfn html head
base body Link nextid title meta kbd start_html end_html
========================================
==========================
in CGI.pm, the ":" is used before every symbol, such as

':html2'=>
tt
bas
inp
':html3'=>
emb
':
.....

I cannot understand it.


thanks

ckacka



Anno Siegel

2004-03-29, 11:47 am

ckacka <ckacka@163.com> wrote in comp.lang.perl.misc:
>
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> 写入邮件
> news:c49fik$2ao$1@mamenchi.zrz.TU-Berlin.DE...
> and
>
> Thanks for your help. Now, I have another question:
>
> I use
>
> ========================================
==========================
> ========================================
==========================
> because I see the CGI.pm use:
> ========================================
==========================
>
> %EXPORT_TAGS = (
> ':html2'=>['h1'..'h6',qw/p br hr ol ul li dl dt dd menu code var strong em
> tt u i b blockquote pre img a address cite samp dfn html head


CGI.pm doesn't use the standard Exporter, it defines its own import()
method. Apparently this one works differently. That's all there's to
it.

With standard Exporter, only the client uses the ":", or rather, the
client uses one more ":" than the exporting module. (You can access an
export tag defined as ":default" by saying "use Module '::default'".
That's a (harmless) bug, not a feature, I'd say.)

Anno
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com