Can't locate WWW/Mechanize.pm in #INC - perl

I am trying to get this sample code that I found on a site to work:
#!/usr/bin/perl
use v5.10;
use WWW::Mechanize;
use WWW::Mechanize::TreeBuilder;
my $mech = WWW::Mechanize->new;
WWW::Mechanize::TreeBuilder->meta->apply($mech);
$mech->get( 'http://htmlparsing.com/' );
# Find all <h1> tags
my #list = $mech->find('h1');
# or this way
# Now just iterate and process
foreach (#list) {
say $_->as_text();
}
When I run it I get this message:
Can't locate WWW/Mechanize.pm in #INC (#INC contains: /Library/Perl/5.16/darwin-thread-multi-2level /Library/Perl/5.16 /Network/Library/Perl/5.16/darwin-thread-multi-2level /Network/Library/Perl/5.16 /Library/Perl/Updates/5.16.2 /System/Library/Perl/5.16/darwin-thread-multi-2level /System/Library/Perl/5.16 /System/Library/Perl/Extras/5.16/darwin-thread-multi-2level /System/Library/Perl/Extras/5.16 .) at test2.pl line 4.
BEGIN failed--compilation aborted at test2.pl line 4.
Does anyone have any ideas on what is happening?

WWW::Mechanize is a Perl module. You need to install it. The easiest way is to run cpanm:
cpanm WWW::Mechanize
Note that your code says use WWW::Mechanize::TreeBuilder, which means you need to install that library as well. As its name suggests, it depends on WWW::Mechanize, which will be automatically installed if you run
cpanm WWW::Mechanize::TreeBuilder

Related

Perl script doesn't see modules .pm [duplicate]

This question already has answers here:
Doesn't Perl include current directory in #INC by default?
(3 answers)
Closed 2 years ago.
I have a perl scripts (proj_perl.pl , and proj_perl_client.pl) , I also created module proj_library.pm. My .pm looks:
package proj_library;
use base 'Exporter';
our #EXPORT = qw(help);
sub help{#my code}
1;
In my .pl scripts I invoke sub help and it looks:
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use IO::Socket;
use JSON;
use proj_library qw(help); #LOOK HERE!
Then I invoke help somewhere in the code.
The problem is when I wan to run my script: ./pro_perl.pl Terminal shows me sth like this:
Can't locate proj_library.pm in #INC (you may need to install the proj_library module) (#INC
contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0
/usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30
/usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at
./proj_perl.pl line 8.
BEGIN failed--compilation aborted at ./proj_perl.pl line 8.
So I don;t know what is going on!
Add the following to tell Perl to search in the script's directory:
use FindBin qw( $RealBin );
use lib $RealBin;
Did you check that your pm is in your #INC? If you need to load a custom PM folder, check perldoc.perl.org/lib
use as:
use lib "."
or
use lib "my_libs/"

Perl module in #INC path, but can't be located though present. Other modules are found

Perl module DBD::mysql was being found and now is not. I'm getting the error message.
Can't locate DBD/mysql.pm in #INC (#INC contains: /usr/local/lib64/perl5
/usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /mnt/Lab/Data/IonTorrent/plugins/WH_v2_05/scripts/create_blinded_reports.pl line 13.
BEGIN failed--compilation aborted at /mnt/Lab/Data/IonTorrent/plugins/WH_v2_05/scripts/create_blinded_reports.pl line 13.
I looked to see where the module was located.
$ perldoc -l DBD::mysql
/usr/lib64/perl5/DBD/mysql.pm
the path /usr/lib64/perl5 is there so why can't it find it.
Here is the code.
#!/usr/bin/perl -w
use strict;
use DBI;
use DBD::mysql; #<----- this is the line that error (I eliminated the header, this is actually line 13.)
use File::Slurp;
use Data::Dumper;
my ($server,$run)=#ARGV;
## variables
my $wh_report="/mnt/Lab/Data/IonTorrent/$server/$run/4_report/wh_report_".$run.".txt";
** Also I checked that I was on the correct server.
It also compiles fine.
perl -c create_blinded_reports.pl
create_blinded_reports.pl syntax OK
I dropped use DBD::mysql; from the code and it ran fine. Thanks!

How to create own modules in Perl

Module
package ExampleModule;
use strict;
use base "Exporter";
our #export = qw/hello_world/;
sub hello_world
{
print "Hello,world";
}
1
Test:
perl -c examplemodule.pm
examplemodule.pm syntax OK
Script:
use ExampleModule;
hello_world();
exit;
but I'm getting error
Error:
Can't locate ExampleModule.pm in #INC (#INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at until.pl line 229.
BEGIN failed--compilation aborted at until.pl line 229.
What is the wrong in my Module please let me know
EDIT
package ExampleModule;
use strict;
use base "Exporter";
our #EXPORT = qw(hello_world);
sub hello_world {
print "Hello,world";
}
1;
saved with ExampleModule.pm
#!/usr/bin/perl -w
use strict;
use lib "/home/Admin/Desktop/Perl_Work_Space/ExampleModule.pm/";
use ExampleModule ;
hell_world();
exit;
saved with first.pl
getting error
[Admin#localhost Perl_Work_Space]$ perl first.pl
Undefined subroutine &main::hell_world called at first.pl line 5.
You're missing path to your module in #INC. To fix it you can tell perl where to look for modules,
use lib "lib/"; # relative or absolute path to ExampleModule.pm
use ExampleModule;
hello_world();
Also ExampleModule.pm should have package line at the top, and use but don't inherit Exporter module,
package ExampleModule;
use strict;
use Exporter "import";
our #EXPORT = qw/hello_world/;
sub hello_world {
print "Hello,world";
}
1;
In addition to the use lib ...; statement, the packagename and the filename have to have the same spelling including the capitalization. So rename the file to ExampleModule.pm.
Although I'm wondering why perl -c examplemodule.pm does not emit a warning.
And you have to use our #EXPORT = qw/hello_world/;

now function for displaying Date and Time not working through ActivePerl

I want to display the present date and time in perl using now() function. I am running this script using Active Perl software but I am getting error.
#! /usr/bin/perl
use Date::Parse;
$date = str2time(now());
print "$date\n";
I am getting error as:
C:\Users\admin\Desktop>perl test.pl
Can't locate Date/Parse.pm in #INC (#INC contains: C:/Perl/site/lib C:/Perl/lib
.) at test.pl line 3.
BEGIN failed--compilation aborted at test.pl line 3.
Please help.
You do not have module Date::Parse installed by default in ActivePerl.
You can fix this with:
ppm install Date::Parse
This works for me for ActivePerl 5.16 default install.

Trouble With CPAN Module

I've tried to install the WWW::Mechanize module with
'cpan WWW::Mechanize'
I get no errors on the 'use WWW::Mechanize' line which means its finding the files, but upon trying to instantiate it with:
$m = WWW::Mechanize->new();
I get the following problem:
Can't locate HTTP/Config.pm in #INC (#INC contains: /Library/Perl/Updates/5.10.0/darwin- thread-multi-2level /Library/Perl/Updates/5.10.0 /System/Library/Perl/5.10.0/darwin-thread-multi-2level /System/Library/Perl/5.10.0 /Library/Perl/5.10.0/darwin-thread-multi-2level /Library/Perl/5.10.0 /Network/Library/Perl/5.10.0/darwin-thread-multi-2level /Network/Library/Perl/5.10.0 /Network/Library/Perl /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level /System/Library/Perl/Extras/5.10.0 .) at /Library/Perl/5.10.0/LWP/UserAgent.pm line 746.
I'm not exactly sure what is going on. I feel like I have all the neccessary dependencies, but I can't seem to find what this particular error means.
My script is empty besides the previously mentioned lines and
use strict;
use warnings;
Has anyone run into this?
try installing it with the following command (from the shell):
perl -MCPAN -e 'install WWW::Mechanize'
follow the installation process, and answer 'Y' where needed.