#!/usr/local/bin//perl -w
use strict;
use diagnostics;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
$mech->quiet(0);                #turn on warnings

my $host = "search.cpan.org";
my $url = "http://$host/~petdance/WWW-Mechanize-1.16/";
$mech->get($url);
my $imageref = $mech->find_image( url_regex => qr/cpan-10/ );

my $ref_type;
my $img_link;
if ( $mech->success ) {
        $img_link=$imageref->url();
        $ref_type=ref $imageref;
        print "Found the image the type of reference is : $ref_type\n";
        print "the image link is: $img_link\n";
        print "url to image is: http://$host$img_link\n";

        # view that url in a web browser and/or
        # download here will get/put to hard drive

        $url = "http://$host$img_link";

   ####   I addedd   the following  ##############
        print "Getting page $url\n";

        if ( $mech->success ) {
                print "Get url - Succesful\n";
                my $filename = './pic.jpg';

		# binary copy of contents to a file
                open my $fh, ">", $filename or die $!;
                binmode $fh;
                print $fh $mech->content;
                close $fh;
        }
        else
        {
                print "Get url - failed\n";
        }
   ###############################################
}
else
{
    print "Did not find the image\n";

} # end