On Wed, Mar 17, 2010 at 9:05 PM, Shlomi Fish <shlo...@iglu.org.il> wrote:

> On Wednesday 17 Mar 2010 16:49:01 raphael() wrote:
> > Hello,
> >
> > Is there a way to save/store downloaded data (using WWW::Mechanize) in
> > memory (temporarily)
> > rather than writing it to disk. Like store 10MB in memory and then flush
> it
> > to the hard disk when data reaches 10MB.
>
> Yes, standard Perl strings are stored inside the computer's memory. You can
> use a buffer and append to it using .= to put stuff there.
>
> Note that this memory will be lost when the process exits.
>
> Regards,
>
>        Shlomi Fish
>
> >
> > Its just a curiosity since utorrent and jdownloader (Java) do this
> > (though I cannot verify if they actually store data in memory. There are
> > options for it in its settings).
>
> --
> -----------------------------------------------------------------
> Shlomi Fish       http://www.shlomifish.org/
> The Case for File Swapping - http://shlom.in/file-swap
>
> Deletionists delete Wikipedia articles that they consider lame.
> Chuck Norris deletes deletionists whom he considers lame.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>

Thanks! However just a little coded example would have been extremely
helpful.
Here what a coded (amateurish)!

---------- CODE -----------
#!/usr/bin/env perl
# 17.03.2010

use strict;
use warnings;
use WWW::Mechanize;
use File::Basename;

# example link
#
http://interfacelift.com/wallpaper_beta/load/02188_theothersideofmanhattan_1680x1050.jpg

my $file = shift;
die "No InFile\n" unless ( $file );

open( FH, '<', $file ) or die;
my @array = <FH>; close( FH );

my $wmc = WWW::Mechanize->new
(
    agent => 'Mozilla/5.0',
    timeout => '15',
    requests_redirectable => ['GET', 'HEAD'],
    max_redirect => '9',
    autocheck => '0',
);

my %hash;
for my $url ( @array ) {
    last if ( $url =~ m/__END__/ );
    next unless ( $url =~ m/^(?:\s+)?http/ );
    my $base = basename( $url );

    $wmc->get( $url );
    $hash{$base} = $wmc->content;
}

for my $img ( keys %hash ) {
    open( my $FH, '>', $img ) or die;
    binmode( $FH );
    print {$FH} $hash{$img};
    close( $FH );
}
------------ END ------------

Got it a little. However I didn't use a concatenation() but a hash. But this
is *memory intensive*.
So the question now becomes is there a way to count how much memory a
scalar/hash/array is using?

Reply via email to