[Koha-patches] [PATCH] fix 2419

Joe Atzberger joe.atzberger at liblime.com
Thu Oct 9 23:20:46 CEST 2008


Marc --

Actually there are very good reasons to use a BEGIN block.  In particular,
it *should* frequently be used with require.  If this module cannot get
Exporter, it should fail BEFORE compile time and as early as possible.

Perl timing can be tricky, especially with the unfortunate multiple
crossed-dependencies that Koha has.  See perldoc
perlmod<http://perldoc.perl.org/perlmod.html>for the best example of
special blocks timing, and in particular perldoc
Exporter <http://perldoc.perl.org/Exporter.html#Good-Practices> "Good
Practices" section for the specific rationale:

There are some caveats with the use of runtime statements like
require<http://perldoc.perl.org/functions/require.html>
> Exporter and the assignment to package variables, which can very subtle
> for the unaware programmer. This may happen for instance with mutually
> recursive modules, which are affected by the time the relevant constructions
> are executed.
>
> The ideal (but a bit ugly) way to never have to think about that is to use
> BEGIN blocks. So the first part of the "SYNOPSIS"<http://perldoc.perl.org/Exporter.html#SYNOPSIS>code could be rewritten as:
>
>   package YourModule;
>
>   use <http://perldoc.perl.org/functions/use.html> strict;
>   use <http://perldoc.perl.org/functions/use.html> warnings;
>
>   our <http://perldoc.perl.org/functions/our.html> (@ISA, @EXPORT_OK);
>   BEGIN {
>      require <http://perldoc.perl.org/functions/require.html> Exporter;
>      @ISA = qw(Exporter);
>      @EXPORT_OK = qw(munge frobnicate);  # symbols to export on request
>   }
>
> The BEGIN will assure that the loading of *Exporter.pm* and the
> assignments to @ISA and @EXPORT_OK happen immediately, leaving no room for
> something to get awry or just plain wrong.
>
This is the model that I try to follow, and explains the block in Koha that
you will see rather commonly.  Unfortunately it does not resolve *all* the
problems from recursive dependencies, but it does help keep them from
exploding spontaneously quite so often.

--joe

On Thu, Oct 9, 2008 at 11:49 AM, Marc Chantreux <marc.chantreux at biblibre.com
> wrote:

> hello,
>
> On Thu, Oct 09, 2008 at 11:04:41AM -0400, Joe Atzberger wrote:
> > Marc --
> >
> > This doesn't quite make sense, since there is already a "require
> Exporter;"
>
> Right! i didn't noticed it ...
>
> > line in the BEGIN block.
>
> and i think *that* is actually the problem
>
> - there is no reason to have a BEGIN block
> - as the BEGIN block is executed asap, i think that the exporter does
>  its job before perl loads functions
>
> i just revamped as it:
>
> use strict;
> require Exporter;
> use vars qw($VERSION @ISA @EXPORT);
> $VERSION = 3.01; # set the version for version checking
> @ISA    = qw(Exporter);
> @EXPORT = qw(
>        &GetBookSeller &GetBooksellersWithLateOrders &GetBookSellerFromId
>        &ModBookseller
>        &DelBookseller
>        &AddBookseller
> );
>
> and it works fine! Do you want me to submit another patch ?
>
> > What reason is there to prefer "use Exporter" here?
>
> you're right: there is nothing to import!
>
> --
> Marc Chantreux
> http://www.biblibre.com
> Expert en Logiciels Libres pour l'info-doc
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/koha-patches/attachments/20081009/394c1578/attachment-0002.htm>


More information about the Koha-patches mailing list