1

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?

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
John Smith
  • 99
  • 1
  • 2
  • 13
  • 2
    See [What's the easiest way to install a missing Perl module?](http://stackoverflow.com/questions/65865/whats-the-easiest-way-to-install-a-missing-perl-module) – ThisSuitIsBlackNot Feb 25 '14 at 18:59
  • 1
    You should try searching a little harder before asking a question. A little Google searching will help you recognize you're most likely missing the WWW:Mechanize module. – mbroshi Feb 25 '14 at 18:59

1 Answers1

6

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
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • 1
    WWW::Mechanize::TreeBuilder too (in fact, that presumably has WWW::Mechanize as a dependency, so just do WWW::Mechanize::TreeBuilder) – ysth Feb 25 '14 at 18:59