> -----Message d'origine----- > De : Charles K. Clarkson [mailto:[EMAIL PROTECTED] > Envoyé : mercredi 16 mars 2005 19:27 > À : beginners@perl.org > Objet : RE: build perl modules > > What you are describing is called "exporting" (or "importing" -- > depending on how you look at it.). There is a module which probably > came with your perl distribution. It is named Exporter.pm and it will > ease building modules like the one you describe. Read its > documentation for more details. > > > Let's begin writing our new module with a subroutine we will > probably rarely use, but hate to have to rewrite each time we need > it. The truth is, it is not hard to write, just hard to remember. > > sub Identity { > return @_; > } > > Wow. That's a long one. We are now ready to create the module. > > package SpecialSubs; > > sub Identity { > return @_; > } > > 1; > > This module is all we really need. Create a file like this > and name it "SpecialSubs.pm". Come back when your done. The "1;" at > the end is very important. It is needed on all modules. > > ========= > > Okay, you're back. Now let's create a script in our favorite > editor and run it. > > #!/usr/bin/perl > > use strict; > use warnings; > > use SpecialSubs; > > print SpecialSubs::Identity( 'foo' ); > > __END__ > > > Prefixing every sub with "SpecialSubs::" could get old quick. > When we "use" a module a series of events occur. One of those events > is running a subroutine named "import" if it is present. > > Let's go back to the module and add an "import" sub > > package SpecialSubs; > > sub import { > print "This is the import sub running automatically\n";; > } > > > sub Identity { > return @_; > } > > 1; > > We'll also change our script. > > #!/usr/bin/perl > > use strict; > use warnings; > > use SpecialSubs; > > __END__ > > Run this and you'll see that the import sub is called > automatically. We can use the import sub to "import" > subroutines into your script. > > > The caller() function from perl will tell us the details > about how this module was called. > > package SpecialSubs; > > sub import { > my( $name_space, $script, $line_number ) = caller(); > printf qq(%s "use"d by %s on line %s in package "%s".\n), > 'SpecialSubs::import()', > $script, > $line_number, > $name_space; > } > > > sub Identity { > return @_; > } > > 1; > > Running our script now reveals which package has called > SpecialSubs.pm. We can get just the name space of the caller > with this. > > package SpecialSubs; > > sub import { > my $name_space = caller(); > print $name_space; > } > > > sub Identity { > return @_; > } > > 1; > > Knowing the name space of the caller, we can import items > into it's name space. We need to use symbolic references here. > Usually, new perl programmers should avoid symbolic references. > > package SpecialSubs; > > sub import { > my $caller = caller(); > *{$caller . '::Identity'} = \&Identity; > } > > > sub Identity { > return @_; > } > > 1; > > If there were 100 subs in the module this could get a > little long and sometimes we need more control. For example, > we might need just two subs and are now stuck with 98 subs we > don't need. We also didn't do any error checking. > > Exporter.pm comes to the rescue. Using this module to > build our modules makes life more simple. > > > package SpecialSubs; > > require Exporter; > @ISA = qw(Exporter); > > @EXPORT_OK = qw( Identity ); > > sub Identity { > return @_; > } > > 1; > > > > #!/usr/bin/perl > > use strict; > use warnings; > > use SpecialSubs qw( Identity ); > > print Identity( 'foo' ); > > __END__ > > > If you'd rather use strict and warnings in the module > you'll need this. > > package SpecialSubs; > > use strict; > use warnings; > > use vars qw( @ISA @EXPORT_OK ); > > require Exporter; > @ISA = qw(Exporter); > > @EXPORT_OK = qw( Identity ); > > sub Identity { > return @_; > } > > 1; > > > OR: > > package SpecialSubs; > > use strict; > use warnings; > > our( qw( @ISA @EXPORT_OK ) ); > > require Exporter; > @ISA = qw(Exporter); > > @EXPORT_OK = qw( Identity ); > > sub Identity { > return @_; > } > > 1; > > > HTH, > > Charles K. Clarkson > -- > Mobile Homes Specialist > 254 968-8328
+1 Nicely explained ;) Someone who want to avoid writing inheritance (@ISA) from Exporter, can write something like: package SpecialSubs; use strict; use warnings; use UNIVERSAL::exports; @EXPORT_OK = qw( Identity ); sub Identity { return @_; } 1; for more info, see: http://search.cpan.org/~mschwern/UNIVERSAL-exports-0.03 José. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>