What's wrong with this Perl module? - perl

I am making a very, very simple module (it is the first I've ever wrote):
package Master::Math;
use 5.12.4;
use strict;
use warnings;
require Exporter;
our #ISA = qw(Exporter)
our %EXPORT_TAGS = (
'all' => [ qw(
max
=cut
1; # End of Master::Math
When I run use this in my program, I get the error
Invalid version format (non-numeric data) at C:/Perl/lib/Master/Math.pm line 3,
near "package Master::Math
"
syntax error at C:/Perl/lib/Master/Math.pm line 3, near "package Master::Math
require Exporter"
Compilation failed in require at C:\MainDev\myperl\max.pl line 3.
BEGIN failed--compilation aborted at C:\MainDev\myperl\max.pl line 3.
What do I need to fix this? Thanks!

You're missing a semicolon on the declaration of #ISA.
In sub max, $foo is undeclared; use foreach my $foo ....
That should get it to compile. I haven't looked beyond that.
(BTW, I didn't get the same errors you did. I used perl 5.14.0, perl -cw master-math.pm.)

Related

DateTime Won't Load Properly Unless a Module Using Moo Launches First

I'm cleaning up code and removed the now deprecated Forecast::IO module from my Perl script. When I did, I started getting this error:
[root#server cgi-bin]# perl Weather.pm
Undefined subroutine &B::perlstring called at /usr/local/share/perl5/Params/ValidationCompiler/Compiler.pm line 248.
Compilation failed in require at /usr/local/lib64/perl5/DateTime.pm line 13.
BEGIN failed--compilation aborted at /usr/local/lib64/perl5/DateTime.pm line 13.
Compilation failed in require at Weather.pm line 1340.
BEGIN failed--compilation aborted at Weather.pm line 1340.
Line 1340 where the error occurs is innocuously enough, use DateTime;.
I wasn't sure what DateTime could be up to that would cause it to be upset I wasn't using the unrelated Forecast::IO module any longer, so I started taking apart the latter module to figure out what code was preventing the error. The essential part seems to be that Moo is included in Forecast::IO and it has at least one Moo has declaration:
package Forecast::IO;
use Moo;
has testkey => ( is => 'ro' );
1;
For some reason, if that module with at least those lines exists, DateTime loads fine. Otherwise, the error I mentioned above occurs, choking on DateTime's line 13, use Carp;. Even if I put use Moo; has testkey => ( is => 'ro' ); into my own module, it fails upon use DateTime.
Even though I've used grep -r Forecast::IO to traverse the codebase to look for some errant reference and come up empty. So, I decided to try to just load the DateTime module: perl -MDateTime. That too produces the error:
[root#server cgi-bin]# perl -MDateTime
Undefined subroutine &B::perlstring called at /usr/local/share/perl5/Params/ValidationCompiler/Compiler.pm line 248.
Compilation failed in require at /usr/local/lib64/perl5/DateTime.pm line 13.
BEGIN failed--compilation aborted at /usr/local/lib64/perl5/DateTime.pm line 13.
Compilation failed in require.
BEGIN failed--compilation aborted.

Installed module not found when running program

Context
Here is a perl test script, in which I wanted to see how you can use a specific event loop with AnyEvent :
# file test.pl :
#!/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Impl::EV;
my $cv = AnyEvent->condvar;
my $wait_one_and_a_half_seconds = AnyEvent->timer (
after => 0.5, # after how many seconds to invoke the cb?
cb => sub { # the callback to invoke
print ("Hello from callback\n");
$cv->send;
},
);
# now wait till our time has come
$cv->recv;
Problem
Here is the error I get when running the above code :
$ perl test.pl
Can't locate EV.pm in #INC (you may need to install the EV module) (#INC
contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2
/usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18
/usr/local/lib/site_perl .) at /usr/local/lib/perl/5.18.2/AnyEvent/Impl/EV.pm
line 28.
BEGIN failed--compilation aborted at /usr/local/lib/perl/5.18.2/AnyEvent/Impl/EV.pm line 28.
Compilation failed in require at test.pl line 6.
BEGIN failed--compilation aborted at test.pl line 6.
Yet I installed the AnyEvent package using cpanm, and the AnyEvent/Impl/EV.pm file is present in one of the #INC path :
$ ls /usr/local/lib/perl/5.18.2/AnyEvent/Impl/
Cocoa.pm Event.pm FLTK.pm IOAsync.pm Perl.pm Qt.pm UV.pm
EventLib.pm EV.pm Glib.pm Irssi.pm POE.pm Tk.pm
Question
How do I fix this ?
Extra remark
The error message says it is looking for EV.pm, but I would have expected AnyEvent/Impl/EV.pm.
How come the use AnyEvent::Impl::EV; I wrote got turned into perl is looking for EV.pm at runtime ?
Just tried to reproduce this with cpan install AnyEvent and can confirm I get the same error.
Line 28 of 'EV.pm' is use EV 4.00;. Your use EV; is a bit of a red herring - that's not the source of the error. This module explicitly includes a 'use' line (which frankly is a bit wierd, it's 'using' itself it seems?)
I don't think that's ever going to work, unless the #INC path is changed - I can only assume that the loading of this module is handled elsewhere, without deconstructing source code.
Referencing the man page - this module gets loaded automatically as required. So you probably don't need to use it in the first place.
Edit: Just compared perl versions. Perl 5.8.5 shows the same behaviour. My 5.20.1 install doesn't.
I'm not sure upgrading perl is necessarily the right step, but it might be worth trying? I'll try and figure out why 5.20.1 works though. It's got to be something to do with handling of #INC.
Edit:
"The handling of return values of #INC filters (subroutines returned by subroutines in #INC) has been fixed in various ways. Previously tied variables were mishandled, and setting $_ to a reference or typeglob could result in crashes."
http://perldoc.perl.org/perl5200delta.html
I think that might be what the problem is.
You're certainly not alone in having this:
http://www.cpantesters.org/cpan/report/d5939816-a510-11e0-bd04-22322d9f2468
From:
http://cpansearch.perl.org/src/MLEHMANN/AnyEvent-7.08/Changes
5.29 Sun Dec 5 10:49:21 CET 2010
- convert EV backend to EV 4.00 API (so better upgrade EV too).
The error message was actually a very correct and forward pointer to what should be done : there is an EV package which needs to be installed separately :
$ sudo cpanm EV
--> Working on EV
Fetching http://www.cpan.org/authors/id/M/ML/MLEHMANN/EV-4.18.tar.gz ... OK
Configuring EV-4.18 ... OK
Building and testing EV-4.18 ... OK
Successfully installed EV-4.18
1 distribution installed
After that, everything works :
$ cat test.pl
#!/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use EV;
my $wait_one_and_a_half_seconds = AnyEvent->timer (
after => 0.5, # after how many seconds to invoke the cb?
cb => sub { # the callback to invoke
print ("Hello from callback\n");
},
);
# now wait till our time has come
EV::run();
$ perl test.pl
Hello from callback

wikiprep syntax error: Use of the encoding pragma is deprecated

I am trying to use wikiprep (https://github.com/avian2/wikiprep) to parse wikipedia dump (in Feb 2014) and generate a XXX.hgw.xml file from the dump.
I followed the usage on Github site above: installed all Perl modules as prerequisites; Build and install wikiprep successfully (wikiprep program appeared in perl5/bin directory). When I execute wikiprep, it says:
Use of the encoding pragma is deprecated at /home/tutran/perl5/bin/wikiprep line 32.
Use of the encoding pragma is deprecated at /home/tutran/perl5/lib/perl5/Wikiprep/languages.pm line 7.
syntax error at /home/tutran/perl5/lib/perl5/Wikiprep/Disambig.pm line 9, near "->import qw/ extractWikiLinks /"
Compilation failed in require at /home/tutran/perl5/bin/wikiprep line 52.
BEGIN failed--compilation aborted at /home/tutran/perl5/bin/wikiprep line 52.
This the line 9:
Wikiprep::Link->import qw/ extractWikiLinks /;
Expected output: options I can use with wikiprep. I also cannot make test for the program. I installed it on Ubuntu 14.04 LTS. My Perl version is 5.18.02.
I don't know anything about perl language so I can't do anything with the "syntax error" here!
You may need enable ut8 in your script. PLace this two lines before you start using this module:
use utf8;
use open qw( :encoding(cp866) :std );
In /usr/local/bin/ wikiprep change line 135 i'e (Wikiprep::Templates->import qw( %templates includeTemplates );) to :
Wikiprep::Templates->import(qw( %templates includeTemplates ));
and
in file
/usr/local/share/perl/5.18.2/Wikiprep/Disambig.pm
change
Wikiprep::Link->import qw/ extractWikiLinks /; to
Wikiprep::Link->import(qw/ extractWikiLinks /);

Errors using Parallel::ForkManager

I'm trying to run a Perl script that uses Parallel::ForkManager:
#!/usr/bin/perl
use Parallel::ForkManager;
use LWP::Simple;
my $pm= Parallel::ForkManager->new(10);
for my $link (#ARGV) {
$pm->start and next;
my ($fn)= $link =~ /^.*\/(.*?)$/;
if (!$fn) {
warn "Cannot determine filename from $fn\n";
}
else {
$0.=" ".$fn;
print "Getting $fn from $link\n";
my $rc=getstore($link,$fn);
print "$link downloaded. response code: $rc\n";
};
$pm->finish;
};
But I get the following errors:
File::Path version 2.06 required--this is only version 1.08 at /usr/lib/perl5/5.8.8/File/Temp.pm line 14.
BEGIN failed--compilation aborted at /usr/lib/perl5/5.8.8/File/Temp.pm line 14.
Compilation failed in require at /usr/lib/perl5/site_perl/5.8.8/Parallel/ForkManager.pm line 441.
BEGIN failed--compilation aborted at /usr/lib/perl5/site_perl/5.8.8/Parallel/ForkManager.pm line 441.
Compilation failed in require at parallel.pl line 3.
BEGIN failed--compilation aborted at parallel.pl line 3.
How can I resolve them?
Upgrade your version of File::Path.
As the error says, you're running version 1.08, but the version of ForkManager that you're running is dependent upon version 2.06.
Note also, that it appears that it is actually File::Temp, which has the File::Path dependency, while P::FM has a dependency on File::Temp.
Either way, you need a more recent version of File::Path.
Perl 5.8.8 is from 2006. Upgrade to a supported version

Why does a module compile by itself but fail when used from elsewhere?

I have a Perl module that appears to compile fine by itself, but is causing other programs to fail compilation when it is included:
me#host:~/code $ perl -c -Imodules modules/Rebat/Store.pm
modules/Rebat/Store.pm syntax OK
me#host:~/code $ perl -c -Imodules bin/rebat-report-status
Attempt to reload Rebat/Store.pm aborted
Compilation failed in require at bin/rebat-report-status line 4.
BEGIN failed--compilation aborted at bin/rebat-report-status line 4.
The first few lines of rebat-report-status are
...
3 use Rebat;
4 use Rebat::Store;
5 use strict;
...
Edit (for posterity): Another reason for this to occur, and perhaps the most common reason, is that there is a circular dependency among the modules you are using.
Look in Rebat/Store.pm for clues. Your log says attempt to reload was aborted. Maybe Rebat already imports Rebat::Store, and Rebat::Store has some package-scope check against being loaded twice.
This code demonstrates the kind of situation I mean:
# m1.pl:
use M1;
use M1::M2;
M1::M2::x();
# M1.pm
package M1;
use M1::M2;
1;
# M1/M2.pm
package M1::M2;
our $imported = 0;
sub import {
die "Attempt to reload M1::M2 aborted.\n" if $imported++;
}
sub x { print "42\n" }
1;
$ perl m1.pl
Attempt to reload M1::M2 aborted.
BEGIN failed--compilation aborted at m1.pl line 3.
The code will compile (and print 42) if you just remove the use M1::M2 line in m1.pl. In your case, you might not need to explicitly use Rebat::Store in your program.
perldoc perldiag:
Attempt to reload %s aborted.
(F) You tried to load a file with "use" or "require" that failed to
compile once already. Perl will not try to compile this file again
unless you delete its entry from %INC. See "require" in perlfunc
and "%INC" in perlvar.