LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   saving results of a perl Mechanize::FireFox task - HowTo (https://www.linuxquestions.org/questions/programming-9/saving-results-of-a-perl-mechanize-firefox-task-howto-4175467824/)

sayhello_to_the_world 06-29-2013 09:53 AM

saving results of a perl Mechanize::FireFox task - HowTo
 
good day dear Linux-experts and coding-friends


i run a tiny little script with perl Mechanize :: Firefox



Code:


#!/usr/bin/perl
use strict;
use warnings;

use WWW::Mechanize::Firefox;

my @urls = qw(
        http://www.google.com
        http://www.yahoo.com
        http://www.cnn.com
        http://www.bing.com
        http://www.nbcnews.com
);

my $temp = '/tmp';
my $mech = WWW::Mechanize::Firefox->new('create');

foreach my $url (@urls){
        my ($name) = $url =~ /www\.(\w+)\.com/;
        print "creating $name.png\n";
       
        $mech->get($url);
        sleep(5);
        my $png = $mech->content_as_png(undef, undef, {width => 240, height => 240});
       
        my $file = "$temp/$name".".png";
        open my $fh, ">", $file or die "couldnt create $file";
        binmode $fh;
        print $fh $png;
        close $fh;
}

print "done\n";


Well if i run this above mentioned code it gives the following output in the terminal


Code:


martin@linux-70ce:~> cd perl
martin@linux-70ce:~/perl> ls
mech1.pl  mech2  mech2.pl  mech3.pl
martin@linux-70ce:~/perl> perl mech3.pl
creating google.png
creating yahoo.png
creating cnn.png
creating bing.png
creating nbcnews.png
done
martin@linux-70ce:~/perl> ^C
martin@linux-70ce:~/perl>


whats the problem with this: well the above mentioned code will write the results to the /tmp directory, at the root level of the drive. If we reboot, they'll be gone, but it's a place any process can write to regardless of user. That is the good thing.

what is needed? We have to do some changes: Well we have to do a change with the the directory to whatever location we want.

well i try to get the files created in a folder in /perl


How to do that!?

Sergei Steshenko 06-30-2013 05:28 AM

Do you understand the meaning of

Code:

my $temp = '/tmp';
in your code ?

sayhello_to_the_world 06-30-2013 10:00 AM

hello dear Sergei

many thanks - guess that we can define some kind of target "folder" and subesquently file where the results are written to?!


Quote:

Originally Posted by Sergei Steshenko (Post 4981118)
Do you understand the meaning of

Code:

my $temp = '/tmp';
in your code ?


can we!?

Code:

my $temp = '/tmp';
- guess that this is saying that it is going to the tmp in root - isnt it!?

Well - if we want to have another folder for the results. Eg. some folder in perl/ where the the code resides in!?

love to hear from you

greetings
say

chrism01 07-01-2013 01:29 AM

It'd be quicker to take his hint and just try it :)

Here's some useful links
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials


All times are GMT -5. The time now is 01:18 AM.