go0913 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, will someone help me? I am totally new for program and Perl, and I made a small tool for myself to download some files required cookies config. Now I want to add the threads in the tool for free of "no responding", and I tried my best but failed after I read some involved info like "perldoc threads" (pod and online), and many codes. So will someone help me, I know it's easy, but it's my first step. Additionally, I want to know how to let the entry or other widgets support the right button of the mouse under Windows? Thank you so much Best regards
######### test.pl #!/usr/bin/perl use strict; use warnings; use Tk; use LWP::UserAgent; my @headers; my $mw = MainWindow->new(); $mw->resizable( 0,0 ); $mw->title("test"); my $frame = $mw->Frame(-borderwidth => 2,-relief => 'groove')->pack(); $mw->Button(-text => "Down",-command => \&d)->pack(-side => 'left'); my $cks; $frame->Label(-text => 'Cookies:')->pack(); my $cks_enter = $frame->Entry(-textvariable => \$cks,-width => 50)->pa +ck(); my $url; $frame->Label(-text => 'URL:')->pack(); my $url_enter = $frame->Entry(-textvariable => \$url)->pack(-expand => + 1, -fill => 'x'); my $filename; $frame->Label(-text => 'Filename:')->pack(); my $fnm_enter = $frame->Entry(-textvariable => \$filename)->pack(-expa +nd => 1, -fill => 'x'); MainLoop; sub d { $cks = $cks_enter->get(); chomp($cks); $url = $url_enter->get(); chomp($url); $filename = $fnm_enter->get(); chomp($filename); @headers = ( 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; EN; rv:1. +9.0.11) Gecko/2009060215 Firefox/3.5', 'cookie' => "$cks", ); my $ua = LWP::UserAgent->new(); my $req = $ua->get($url,@headers); my $cont = $req->content; open PIC, ">$filename"; binmode(PIC); print PIC "$cont\n"; close PIC; }

Replies are listed 'Best First'.
Re: how to add threads in the script
by jettero (Monsignor) on Jul 24, 2009 at 15:55 UTC
    I recommend against threads in every situation where they seem wanted. Check out POE and the Tk mainloop in particular (POE Tk Cookbook).

    Also, check out WWW::Mechanize. It may simplify a lot of what you're doing there.

    -Paul

      Thank you Paul But I think changing is not a good thing for learning, I just want to finish my initial idea even by a not smart way. Thanks any way. POE and WWW::Mechanize are nice modules, and I will play with them after I master threads and LWP.

        Even if you master threads, they'll still be awful. Friends don't let friends use threads, they suggest forks.

        -Paul

        Perl-LWP is way cool!

        I've built maybe a dozen gizmos that cruise around various websites, some with SSL secure logins, etc. Some sites have bogus search engines and I just have to suck their database dry by navigating through their webpages, generating 10's of thousands of requests via their bogus search engine.

        I would caution you about overwhelming the other guy. It is possible for you to be "blacklisted" if you generate too much traffic too fast on a site.

        The thread model is the very most complex thing that you can do, but it is the highest performance. The fork() model is slower but not by much. Instead of a multi-thread process, that whacks the doo-doo out of a site, as fast as you can, maybe run 10 processes at a more "leisurely" pace that beats on 10 sites at once.

        Update:Got some down votes on this post...I'll try a clarification...the main idea is not use the most complex multi-processing model when something easier will do and second depending upon what kind of site you are accessing, how many times per second you do that makes a difference. Some my LWP programs access some sites that are "small" in terms of bandwidth and processing power but have significant size DB's. I can't crash Google.com with a single machine, but on some "small" sites, too fast a flood of requests can be "disruptive" to say the least. I'm just saying to be a "good citizen" out there on the web. Don't unleash a maximally performant web query engine on a website that can't take it. I think this is just common courtesy and is something to be considered.

Re: how to add threads in the script
by go0913 (Novice) on Jul 25, 2009 at 11:01 UTC
    Now I modify as some perlmonks threads
    #!/usr/bin/perl use strict; use warnings; use threads; my $cks; my $url; my $filename; my $cks_enter; my $url_enter; my $fnm_enter; sub dd{ my $thr1 = threads->new(\&d); $thr1->detach(); } use Tk; my $mw = MainWindow->new(); $mw->resizable( 0,0 ); $mw->title("test"); my $frame = $mw->Frame(-borderwidth => 2,-relief => 'groove')->pack(); my $bttn = $mw->Button(-text => "Down",-command => \&dd)->pack(-side = +> 'left'); $frame->Label(-text => 'Cookies:')->pack(); $cks_enter = $frame->Entry(-textvariable => \$cks,-width => 50)->pack( +); $frame->Label(-text => 'URL:')->pack(); $url_enter = $frame->Entry(-textvariable => \$url)->pack(-expand => 1, + -fill => 'x'); $frame->Label(-text => 'Filename:')->pack(); $fnm_enter = $frame->Entry(-textvariable => \$filename)->pack(-expand +=> 1, -fill => 'x'); MainLoop; sub d { #$bttn->configure(-state=>'disabled'); $cks = $cks_enter->get(); chomp($cks); $url = $url_enter->get(); chomp($url); $filename = $fnm_enter->get(); chomp($filename); use LWP::UserAgent; my @headers = ( 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; EN; rv:1. +9.0.11) Gecko/2009060215 Firefox/3.5', 'cookie' => "$cks", ); my $ua = LWP::UserAgent->new(); my $req = $ua->get($url,@headers); my $cont = $req->content; open PIC, ">$filename"; binmode(PIC); print PIC "$cont\n"; close PIC; }
    When I empty the cookies, and enter a url,click down. It downloads the jpg, but ouccrs some errors:
    Attempt to free non-existent shared string '_TK_RESULT_', Perl interpreter: 0x2e3642c at C:/Perl/lib/Tk.pm line 252.
    Attempt to free non-existent shared string '_TK_RESULT_', Perl interpreter: 0x2e3642c at C:/Perl/lib/Tk/Widget.pm line 98 during global destruction.
    Free to wrong pool 2e33d20 not 284fb0 at C:/Perl/lib/Tk/Widget.pm line 98 during global destruction.
    So it maybe something with share? and how to finish it?
    Thanks
    Best Regards