Re: An Inline question, and a user's experience: Fwd: Using Inline::CPP and Math::Primes::FastSieve

[email protected] (Dana Jacobsen)
Newsgroups perl.inline
Message-ID <CAEiHgGn9XF9z0yAy9DbDNbimtZQq2BvcpoG3JXY3NUGruBHitA@mail.gmail.com>
The use vs. require issue is a little harder when I have chains of
modules, or things get loaded my Moose/Mouse roles via 'with' since
they load at runtime.  In my immediate use, looking for
Math::Prime::FastSieve is all inside a Role, so there's no real way I
can change how it is loaded.  Even if I write:

  use Math::Prime::FastSieve;
  # .... use MPFS

in my role, I get the warning.  No direct eval or require on my part.


The chain of modules example, with MyPack.pm:

  package MyPack;
  use strict;
  use warnings;
  use Math::Prime::FastSieve;
  # ...
  1;

Everything is good.  No warnings.  But along comes someone who wants
to use Mypack and creates his MyScript:

  #!/usr/bin/env perl
  no strict;
  no warnings;
  require MyPack;
  # ...

Oops, they get the warning.  Similarly if they use an eval with use or
require, and 'no warnings' doesn't work for suppression.

As you wrote, putting 'Inline->init()' after the inclusion of M::P::FS
solves the issue in both cases.  I can do this successfully:

if (eval {require Math::Prime::FastSieve; Inline->init(); 1;}) {
   # We have MPFS, do fast stuff.
} else {
   ...
}

The goal would be to have Inline handle it, or the module using Inline
(e.g. Math::Prime::FastSieve) do it.  But for now this works.

Dana


On Sat, May 19, 2012 at 12:52 PM, Sisyphus <[email protected]> wrote:
>
> ----- Original Message ----- From: "David Oswald"
>
>> I get the Inline M51 warning (DATA section not used) if anyone does a
>> require on my module. I realize this is a harmless warning, but I
>> really don't want it happening to my module when I don't even use
>> Inline. So for now I've disabled use of MPFS to avoid it.
>>
>> <snip>------------------------------------------------
>>
>> That message is coming from Inline, but I can't figure out how one
>> might squelch it.  Are there any suggestions that might help here?
>
>
> Found the time to have a further look tonight.
>
> It all happens in this section of Inline.pm:
>
> ######################################################
> #==============================================================================
> # Process delayed objects that don't have source code yet.
> #==============================================================================
> # This code is an ugly hack because of the fact that you can't use an
> # INIT block at "run-time proper". So we kill the warning for 5.6+ users
> # and tell them to use a Inline->init() call if they run into problems.
> (rare)
> my $lexwarn = ($] >= 5.006) ? 'no warnings;' : '';
>
> eval <<END;
> $lexwarn
> \$INIT = \$INIT; # Needed by Sarathy's patch.
> sub INIT {
>   \$INIT++;
>   &init;
> }
> END
>
> sub init {
>   local ($/, $") = ("\n", ' '); local ($\, $,);
>
>   while (my $o = shift(@DATA_OBJS)) {
> $o->read_DATA;
> $o->glue;
>   }
> }
>
> sub END {
>   warn M51_unused_DATA() if @DATA_OBJS;
>   print_version() if $version_requested && not $version_printed;
> }
>
> #######################################################
>
> If @DATA_OBJS doesn't get emptied out then you get the M51 warning.
>
> If you "require" Math::Prime::FastSieve, sub INIT does not get called
> (because INIT blocks can only be executed *before* runtime), therefore sub
> init doesn't get called, therefore @DATA_OBJS doesn't get emptied, therefore
> you get the M51 warning.
>
> One solution is to take the advice offered in the comments in the above code
> and have FastSieve.pm do:
>
> Inline->init();
>
> (immediately prior to the closing '1;' seems to suffice).
>
> I'm a bit nervous about messing too much with this code - I don't know what
> "Sarathy's patch" is, or why it needs the line of code that is claimed to be
> necessary. I've no idea why it does '\$INIT++;' (or even why $INIT has to
> exist) and I don't really know much about how that code fits in to the
> general scheme.
>
> It's clear that $lexwarn can now be removed and replaced by an unconditional
> 'no warnings;' at the begining of the eval() ... and I'll do that.
>
> For the moment, I think I'll just document the 'Inline->init()' solution,
> and leave it at that.
>
> Eventually I'd like to think that I'll get around to poking and prodding a
> bit, and try to come up with a better way of silencing the M51 warning when
> an Inline module is "required". (If someone else gets to it before I do,
> then that's all the better :-)
>
> Cheers,
> Rob
>
> PS If it's not obvious, the reason that 'use Math::Prime::FastSieve;' does
> *not* produce the same
> warning is that loading with "use" enables the INIT sub to get run (straight
> after compile-time,
> and before runtime). Therefore, @DATA_OBJS gets emptied out, and the M51
> warning is avoided.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.