Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

The Monastery Gates

( #131=superdoc: print w/replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Perl modules that I can use for Multithreading
5 direct replies — Read more / Contribute
by elpis
on Feb 22, 2017 at 08:12

    I want to parallelize a code written in Perl. The code loops through multiple files and calls a subroutine for each file. I also need to share some readonly local data-structures with the subroutine.

    sub process_in_parallel { my $readOnlySchema = foo(); foreach my $file (@files) { validate_the_file($file,$readOnlySchema); } }

    I am pretty new to perl programming and hence need a lot of advice here. What are the perl modules that the perl monks can recommend for this scenario?

    I tried some of the following:

    - threads : The problem with this is managing the threads. Is there an efficient thread Manager or thread pool library that can help me with this? I am also not sure if I can share the readOnly object easily.

    -Parallel::ForkManager : The problem with this is that it forks processes rather than threads and is increasing the time of execution in my case.

    Can you please suggest other libraries also?

cgi progressbar
2 direct replies — Read more / Contribute
by bigup401
on Feb 21, 2017 at 07:03

    i have tried to look around the proper way of progressbar in html. bt no good information i have been playing around with progrssbar in commandline bt not in cgi. as they say perl is good for open source bt web apps not easy too much time to take and alot tests

    any idea or simple. i appreciate

    if (execute code){ # execute code to run } while (the code is runing ) { # show the progress .. show the progressbar from 1 } elsif (the code has finished running) { #stop the progressbar at 100% } else (the code has not executed) { # dont let the progressbar to run } print "Content-type: text/html\n\n"; print <<START_HTML; <!DOCTYPE html> <html> <style> #myProgress { width: 100%; background-color: #ddd; } #myBar { width: 1%; height: 30px; background-color: #4CAF50; } </style> <body> <h1>JavaScript Progress Bar</h1> <div id="myProgress"> <div id="myBar"></div> </div> <br> <button onclick="move()">Click Me</button> <script> function move() { var elem = document.getElementById("myBar"); var width = 1; var id = setInterval(frame, 10); function frame() { if (width >= 100) { clearInterval(id); } else { width++; elem.style.width = width + '%'; } } } </script> </body> </html> START_HTML
Solved : decimal to hex in an array
3 direct replies — Read more / Contribute
by t-rex
on Feb 21, 2017 at 05:31

    hello monks,

    I have an array where I need to convert every even index element to hex , I have written a code but the output is not what i expected

    my @a = "1 10 5 345 2 12"; my $indx=1; foreach my $ind (@a) { print "IND = $ind and index = $indx\n"; if ($ind%2==0) { $ind = sprintf("0x%x",$ind); } $indx++; } print "final conversion input array = @a\n";

    actual output : 1 0xa 5 345 0x2 0xc

    expected output : 1 0xa 5 159 2 0xc

    please let me know where I am going wrong ?

    edit : i got my error , just because of same names i made a silly mistake and couldn't see , sorry for the trouble guys

http post
2 direct replies — Read more / Contribute
by bigup401
on Feb 19, 2017 at 09:18

    i got some issue when i execute my code i get this Response Code: 411

    #!/usr/bin/perl use strict; use HTTP::Request::Common qw(POST); use LWP::UserAgent; my $ua = new LWP::UserAgent; my $senderid = "demo"; my $num = "447777777777"; my $sms = "demo"; my $rep = HTTP::Request->new(POST => "http://api.website.com/api/v1/sm +s/send/?apiKey=edftr44456&message=$sms%20HTTP%20API&from=$senderid&to +=$num"); $rep->content_type("application/x-www-form-urlencoded"); $rep->content_type("Content-Type' => 'application/json"); my $repobj = $ua->request($rep); my $repcode = $repobj->code; print 'Response code: ' . $repcode . "\n";
Perl 6 OOP: before and after methods "CLOS style"
7 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 18, 2017 at 18:37

    I'm learning Perl 6 and I'm constantly blown away by amazing Perl 6 features, it's something I experienced only while I was learning a bit of Common Lisp.

    I wish to know if in the Perl 6 MOP there is something similar to CLOS before/around/after methods.

    Also, given that my experience with programming is mainly quick procedural scripts (and, sadly, one big system in classic asp and vb for work) , which books/resources can give me a good grasp of Perl 6 OOP philosophy? For CL I loved the CLOS book by Kleene, but Perl 6 approach seems different (Smalltalk-ish???).

    Thank you very much for your wisdom.

How to detect non printable characters and non white space characters? [RESOLVED]
3 direct replies — Read more / Contribute
by thanos1983
on Feb 17, 2017 at 04:49

    Hello again Monks,

    Lately I have been bombing the forum with questions but no matter how much I experiment with my code I can not figure out the solution(s) to my problems this is why I keep asking questions over and over.

    To the question, I have a hash of hashes with hundreds values on each hash. I want to use a negative condition on both detecting white space character or non printable character. In such a case I want to delete the element from the hash (if does not contain white space character or non printable character).

    I tried the conditions separately and they work just fine, at least based on my experimentation examples. I need to combine them in a nested if because a value with no spaces it does not mean that it does not contain special characters.

    Based on my research in order to detect non printable characters you can use either this /[^[:print:]]/g or this /[^[:ascii:]]/ regex expression found here (Finding out non ASCII Characters in the text)

    Sample of code:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $str = 'a[bdy]dfjaPÑsdafÜ'; my $str_2 = 'WAP'; my $hoh_ref = { hash_1 => { a => 'a[bdy]dfjaPÑsdafÜ', b => 'WAP' }, hash_2 => { c => 'Te st' } }; print Dumper $hoh_ref; foreach my $key (sort keys %{$hoh_ref}) { foreach my $value (keys %{$$hoh_ref{$key}}) { # If not white space character or non printable character remove e +lement if ($$hoh_ref{$key}{$value} !~ /[^[:print:]]/g || $$hoh_ref{$key}{$value} !~ /\s/) { delete $$hoh_ref{$key}{$value}; } elsif ($$hoh_ref{$key}{$value} =~ /[^[:print:]]/g) { while ($$hoh_ref{$key}{$value} =~ /[^[:print:]]/g) { print "Non Printable Characater:\t$&\n"; } } } } print Dumper $hoh_ref; __END__ $VAR1 = { 'hash_2' => { 'c' => 'Te st' }, 'hash_1' => { 'b' => 'WAP', 'a' => 'a[bdy]dfjaPÑsdafÜ' } }; $VAR1 = { 'hash_2' => {}, 'hash_1' => {} };

    Desired output would be:

    $VAR1 = { 'hash_2' => { 'c' => 'Te st' }, 'hash_1' => { 'a' => 'a[bdy]dfjaPÑsdafÜ' } };

    Thanks in advance for your time and effort.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Critical sections; In the Perl interpreter
4 direct replies — Read more / Contribute
by Wiggins
on Feb 16, 2017 at 13:45
    My interest is in critical sections in the Perl interpreter itself.
    I was asked by a friend, why after starting 150 web spider threads, his system would bog down until it seem to stop, but was still running?

    I proposed that each of those threads was a separate process, with each having a full Perl interpreter. But they were all sharing the same .so libraries, and the number of critical sections(CS) that the interpreter had to go through would create single file bottle-neck after bottle-neck.

    Are there CS in the interpreter? Are there certain functional areas that have significantly more CS than others

    It is always better to have seen your target for yourself, rather than depend upon someone else's description.

Does "preallocating hash improve performance"? Or "using a hash slice"?
4 direct replies — Read more / Contribute
by vr
on Feb 15, 2017 at 14:39

    I was reading this article: http://www.sysarch.com/Perl/sort_paper.html (actually, mentioned here: Re: Sorting geometric coordinates based on priority), and found this:

    If the approximate size of the data set is known, preallocating the hash improves performance.

    keys my %cache = @in; $cache{$_} = KEY($_) for @in;

    The following sets up the cache more efficiently, using a hash slice:

    keys my %cache = @in; @cache{@in} = map KEY($_) => @in;

    I liked the idiom

    keys %h = @a; @h{ @a } = ... ; # do something useful

    and thought it would be nice to remember and use it sometimes. While, of course, I was using hash slices before, but rather because they look so concise and, somehow because of this, I felt that code, as a result, must, indeed, be more efficient. And now additional optimization through "magical" use of keys as lvalue, forcing scalar context on array. Actually, keys mentions this optimization, but I missed it before:

    Used as an lvalue, keys allows you to increase the number of hash buckets allocated for the given hash. This can gain you a measure of efficiency if you know the hash is going to get big.

    Then I thought it strange that assigning to large hash slice still requires this "preallocation". Then I ran this test:

    use strict; use warnings; use Benchmark qw/ cmpthese /;; for my $count ( 100, 1_000, 10_000, 100_000 ) { cmpthese( -5, { 1 => sub { my @a = map { log } 2 .. $count; my %h; keys %h = @a; $h{ $_ } = log for @a; return \%h }, 2 => sub { my @a = map { log } 2 .. $count; my %h; $h{ $_ } = log for @a; return \%h }, 3 => sub { my @a = map { log } 2 .. $count; my %h; keys %h = @a; @h{ @a } = map { log } @a; return \%h }, 4 => sub { my @a = map { log } 2 .. $count; my %h; @h{ @a } = map { log } @a; return \%h }, }) }

    log is here to imitate at least some payload (useful work), and to create longer hash keys (if it matters). Returning a reference so that Perl doesn't sniff we don't need this hash and won't skip any work. And that's because of results:

    Rate 4 3 2 1 4 1507/s -- -3% -4% -5% 3 1549/s 3% -- -2% -3% 2 1576/s 5% 2% -- -1% 1 1593/s 6% 3% 1% -- Rate 1 2 4 3 1 140/s -- -3% -4% -4% 2 145/s 3% -- -0% -1% 4 145/s 4% 0% -- -0% 3 146/s 4% 1% 0% -- Rate 4 2 3 1 4 12.1/s -- -7% -8% -9% 2 12.9/s 7% -- -2% -3% 3 13.1/s 9% 2% -- -1% 1 13.3/s 10% 3% 1% -- s/iter 4 1 2 3 4 1.39 -- -3% -3% -3% 1 1.35 3% -- -0% -0% 2 1.35 3% 0% -- -0% 3 1.35 3% 0% 0% --

    No meaningful difference at all. So, are my tests flawed, or claims about efficiency of slices and preallocation don't hold any water?

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
NodeReaper files his shovel

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2017-02-22 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (330 votes). Check out past polls.