Gossamer Forum
Home : General : Perl Programming :

Beginner Perl Package Question

(Page 1 of 2)
> >
Quote Reply
Beginner Perl Package Question
Code:
package MyModule;

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

$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(&func1 &func2);
%EXPORT_TAGS = ( DEFAULT => [qw(&func1)],
Both => [qw(&func1 &func2)]);

sub func1 { return reverse @_ }
sub func2 { return map{ uc }@_ }

1;


I just found this example for using functions with my new package (Brand new to this BTW).
I guess I am looking for a generic format for creating a module which has a load of functions
I want to share with my other perl modules and cgi.

My question is, does the %EXPORT_TAGS in the above example suit the normal/best way to
share the functions in this file.

Also, I don't follow the use of DEFAULT or BOTH :

%EXPORT_TAGS = ( DEFAULT => [qw(&func1)],
Both => [qw(&func1 &func2)]);


Should I just ignore the DEFAULT line and use the both line for a generic form for inlcuding multiple functions?

use MyModule qw(:Both);
print func1(@list),"\n";
print func2(@list),"\n";


I aplogise for sounding lost... I just started dabbling in this a half hour ago (thanks Paul)


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Wil: Jul 13, 2002, 12:39 PM
Quote Reply
Re: [Ian] Beginner Perl Package Question In reply to
May wanna sort out that code tag before your time runs out Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Beginner Perl Package Question In reply to
I hate those code tags!

Often when I cut and paste, it removes the line breaks.


Edit: now it won't let me remove the code tag's opening tag. Oh well, you get the idea.


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jul 13, 2002, 12:02 PM
Quote Reply
Re: [Ian] Beginner Perl Package Question In reply to
>>
My question is, does the %EXPORT_TAGS in the above example suit the normal/best way to share the functions in this file.
<<

Yep, or by using @EXPORT/@EXPORT_OK (@EXPORT_OK recommended more so)

>>
Also, I don't follow the use of DEFAULT or BOTH :
<<

It just lets you specify what to import. If you know you don't need func2() in your module then it means you can just import DEFAULT to speed things up.

There are other ways of importing/exporting such as by using import() but I won't bore you with all that :)
Quote Reply
Re: [Paul] Beginner Perl Package Question In reply to
Thanks Paul.

Let me play a little more.... I'll be backSmile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Beginner Perl Package Question In reply to
A quick example incase I made no sense is when using the CGI module.

If you just wanted to print a header, instead of using:

use CGI;

...which is pretty slow and imports loads of stuff.....you could do:

use CGI qw/:header/;

....which would just import the functions related to printing headers.

Edit: Actually I tell a lie, it is :cgi in my copy which loads some other stuff too...

Last edited by:

Paul: Jul 13, 2002, 12:40 PM
Quote Reply
Re: [Paul] Beginner Perl Package Question In reply to
Nah! Use the OO interface of CGI.pm -- bring it on, Perl 6 which will hopefully convert everyone into having to use OO interfaces of modules -- then you can just do Use CGI; and it will load objects as required.

- wil
Quote Reply
Re: [Wil] Beginner Perl Package Question In reply to
Well of course but we are dealing with the basics here for someone who said themselves they are new to this...I'm not going to dive in the deepend with my examples am I? Crazy
Quote Reply
Re: [Paul] Beginner Perl Package Question In reply to
That's a fair enough point, but with Perl 6 on the horizon (OK, maybe I'm being a bit hopeful here) then OO is really the way to go. I appreciate the need to walk before you can run, but why bother walking when you can just run? :-)

- wil
Quote Reply
Re: [Paul] Beginner Perl Package Question In reply to
Thanks.

So for me (beginner)... I'll:

use MYPACKAGE qw/:only the stuff I want to import/;

And the ':' is necessary for this sytax then?

Perl 6 sounds like a big improvement, but I guess it while be a while until it has spread itself onto everyone's server.


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jul 13, 2002, 12:46 PM
Quote Reply
Re: [Ian] Beginner Perl Package Question In reply to
>>
And the ':' is necessary for this sytax then?
<<

Nope, only with %EXPORT_TAGS if you want to import a few different things, otherwise you can just do:

use MyModule qw/$some_var/;
Quote Reply
Re: [Paul] Beginner Perl Package Question In reply to
Got it!

Thanks!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Beginner Perl Package Question In reply to
OO functionality has been supported since Perl 4 AFAIK, Ian.

On seconds thought -- It's actually a hell of a lot easier than the method Paul is trying to show you.

- wil
Quote Reply
Re: [Wil] Beginner Perl Package Question In reply to
Have a look here:

http://www.perlcircus.com/site/modules.html

And scroll down to the OO section to see how easy it is. No need to remember what to Export or anything! No need to remember what you need to pre-load!

- wil
Quote Reply
Re: [Ian] Beginner Perl Package Question In reply to
On a totally unrelated note, this rocks...

Code:
#!/usr/bin/perl
#==========================

use strict 'subs vars';
main();

#==========================

sub main {
#-----------------------------------------

my %list = ('red', 'yellow', 'blue', 'green');
*$_ = $list{$_} for qw/red blue/;

print "Content-type: text/html\n\n";
print "Roses are " . red('red') . " violets are " . blue('blue') . "most poems rhyme but this one doesn't";

}

sub yellow { return qq|<font color="red">$_[0]</font>| }
sub green { return qq|<font color="blue">$_[0]</font>| }

I just fancied a play ;)
Quote Reply
Re: [Wil] Beginner Perl Package Question In reply to
>>
And scroll down to the OO section to see how easy it is. No need to remember what to Export or anything! No need to remember what you need to pre-load!
<<

....using objects has nothing to do with importing/exporting from your modules. Just because you are using objects doesn't mean you don't have to export anything.

Last edited by:

Paul: Jul 13, 2002, 1:09 PM
Quote Reply
Re: [Paul] Beginner Perl Package Question In reply to
Nah......... But just look at the examples. The OO interface to the same module is so much more sexy!

- wil
Quote Reply
Re: [Wil] Beginner Perl Package Question In reply to
Thanks guys... baby steps for me though. I am going to try this method first... then come back to this postSmile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Wil] Beginner Perl Package Question In reply to
You are confusing the issue. Objects is a totally different area to exporting vars from one module to another.

Last edited by:

Paul: Jul 13, 2002, 1:17 PM
Quote Reply
Re: [Ian] Beginner Perl Package Question In reply to
Ok it was:

use CGI 'header';

....I knew that all along Cool
Quote Reply
Re: [Paul] Beginner Perl Package Question In reply to
So no ':' infront of header beacuse it is not an array of functions?


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jul 13, 2002, 1:21 PM
Quote Reply
Re: [Ian] Beginner Perl Package Question In reply to
This may help....

http://www.perldoc.com/....1/lib/Exporter.html

Last edited by:

Paul: Jul 13, 2002, 1:25 PM
Quote Reply
Re: [Ian] Beginner Perl Package Question In reply to
Use CGI;
my $q = new CGI;

print $q->header;

See how simple it is with OO! :-)

- wil
Quote Reply
Re: [Paul] Beginner Perl Package Question In reply to
Thanks for the reference


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Wil] Beginner Perl Package Question In reply to
>>
Use CGI;
my $q = new CGI;

print $q->header;

See how simple it is with OO! :-)
<<

But what has that got to do with what we are talking about?....we aren't trying to print headers Laugh

(and you'll find it is use not Use Cool)

Last edited by:

Paul: Jul 13, 2002, 1:31 PM
> >