Hey guys! I am completely new to this site, and I started learning perl a couple of weeks ago. I find it fascinating! So, the reason why I am here is to get some input on the script I wrote... Criticize and critique...How can I make this script better?

#!/usr/bin/perl + + use 5.012; use warnings; use threads; use threads::shared; use Thread::Queue; use LWP::UserAgent; use Time::HiRes qw( time ); use constant THREADS => 50; use Data::Dumper; $| = 1; ######### + + # Main # + + ######### + + my $queue = Thread::Queue->new(); my @URLs = qw( http://www.google.com/)x50; my @threads; my $time = time; my $json = '{"username": "anonymous", "password":"anonymous"}'; my %query_hash = (action => 'submit or status', name => 'chris', outn +ame => 'chris', 'content-type' => 'application/json'); print"\n\ Test...\n\n"; my $counter_thread : shared; for (1..THREADS) { push @threads, threads->create(sub { my $ua = LWP::UserAgent->new; $ua->timeout(5); # short timeout for easy testing. + + while(my $task = $queue->dequeue) { my $request = HTTP::Request->new(POST=>$task); $request->header(%query_hash); $request->content($json); my $response = $ua->request($request);; print"\n\n", $counter_thread++,"th :", "Dumped Data Return +ed:\n\n ", Dumper($response->decoded_content), "\n" ; print "Response code: ", $response->code, "\tHTTP Respons +e Message: ", $response->message, " ", "\n"; } }); } $queue->enqueue(@URLs); $queue->enqueue(undef) for 1..THREADS; Pending_HTTP(); Print_File(); {$_-> join foreach @threads}; ############################################## + + # Print pending HTTP 'POST' requests to file # + + ############################################## + + sub Pending_HTTP { my $file_write = 'Out_Stats.txt'; open(FILE, '>', $file_write) or die $!; while (!($queue->pending() == 0)) { sleep(1); my $time_now = time; print FILE "Number of pending HTTP 'POST' Requests: ", $queue- +>pending(), "\tTime Elapsed: ", ($time_now - $time), "\n"; } close(FILE); } ################################# + + # Print file to standard output # + + ################################# sub Print_File { print "\n\nListing Pending HTTP Requests and Time elapsed...\n\n"; open(FILE, '<', 'Out_Stats.txt') or die $!; print "**********************************************\n\n"; my $i = 1; while(defined($_ = <FILE>)) { print "$i: ", $_; $i++; } close(FILE); }

In reply to HTTP Requests and Threading by carlriz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.