CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Modules + Import Parameters

 



yapp
User

Nov 13, 2001, 10:35 AM

Post #1 of 5 (1566 views)
Modules + Import Parameters Can't Post

Hi there.

If I make a module, can I also pass parameters to it as the use line, line this:

use XProgramming::CGI::Template 'the template filename';

And how can I find out / handle that?

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com


rGeoffrey
User

Nov 13, 2001, 1:11 PM

Post #2 of 5 (1562 views)
Re: Modules + Import Parameters [In reply to] Can't Post

This should work...


Code
package Some::Module; 

BEGIN{
use Exporter ();
use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = 0.10;
@ISA = qw (Exporter);
@EXPORT = qw (
);
use vars qw ($FILENAME
);
}

And then you call it with these two lines...


Code
use Some::Module; 
$Some::Module::FILENAME = 'some/file/name/here.txt';

So $FILENAME is a package variable inside Some::Module, but it can be changed from outside. This will slow down anyone who wants to try to change or even use $FILENAME from outside Some::Module, but you can still set it from outside. Remember that you will not be able to use local or my on $FILENAME if you want to deal with it from outside the file.



yapp
User

Nov 14, 2001, 1:11 AM

Post #3 of 5 (1559 views)
Re: Modules + Import Parameters [In reply to] Can't Post

Yes, I know you can so that. Thanks anyway for your reply...

however, can I put that filename at the import line, like
use Language::Pack qw(english)
or use A::Template qw(/home/template.html)


Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com


wog
stranger

Nov 21, 2001, 7:22 AM

Post #4 of 5 (1544 views)
Re: Modules + Import Parameters [In reply to] Can't Post

"

Code
use packagename parameters

" is roughly equivilent to:


Code
BEGIN { 
require packagename;
packagename->import(parameters);
}

So, if you want to do your own thing with the parameters you get you can write your own "import" class method, like:


Code
package XProgramming::CGI::Template; 
sub import {
my $class = shift;
my $filename = shift;
# ... process ...
}



yapp
User

Nov 22, 2001, 4:11 AM

Post #5 of 5 (1532 views)
Re: Modules + Import Parameters [In reply to] Can't Post

Thanks a lot!!!!

This is great.

so the parameter list I add (the qw(..) line) is received by the import function ;-)

The other code you posted is indeed roughly the same, except that the use statements are even executed earier. Since I have all my error trap functions inside a module (without imports or Exporter exports) (all) the errors are trappable

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives