Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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
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 => " +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"
6 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: (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?

Array confusion.
8 direct replies — Read more / Contribute
by anonypl
on Feb 14, 2017 at 16:46


    I'm trying to re-learn perl after a long hiatus. Trying to reverse the array contents without using the "reverse" function, just to see how much I understand things. Looks like I have totally forgotten everything.

    Here is what I am trying:

    use warnings; use strict; use diagnostics; my @arr = ('a'..'z'); print "@arr\n"; foreach my $thing (@arr) { my $ele = pop @arr; my $stuff = unshift @arr, $ele; print "$ele "; }

    And here is the output:

    C:\perlscripts>perl a b c d e f g h i j k l m n o p q r s t u v w x y z z y x w v u t s r q p o n m l k j i h g f e d c b a C:\perlscripts>

    What I fail to understand is, iterating the array using $thing variable but not using it at all in the loop still gives the expected result.

    Tried printing out the $thing contents, by using print "[$thing]".

    use warnings; use strict; use diagnostics; my @arr = ('a'..'z'); print "@arr\n"; foreach my $thing (@arr) { print "[$thing]"; my $ele = pop @arr; my $stuff = unshift @arr, $ele; print "$ele "; }

    Here's the output

    C:\perlscripts>perl a b c d e f g h i j k l m n o p q r s t u v w x y z [a]z [a]y [a]x [a]w [a]v [a]u [a]t [a]s [a]r [a]q [a]p [a]o [a]n [a]m +[a]l [a]k [a]j [a]i [a]h [a]g [a]f [a]e [a]d [a]c [a]b [a]a C:\perlscripts>

    What am I missing? Something very basic and "duh" I guess..

Tcl DND working but can't get original Tcl::Tk GUI code to work
1 direct reply — Read more / Contribute
by Anonymous Monk
on Feb 14, 2017 at 11:42

    Hello monks who are always smarter than me. I have a script that I added a very simple Tcl::Tk GUI to a little while back. I've been trying to take it to the next level with drag and drop. I added the DND module to my Tcl installation and have drag and drop working but only using Tcl as you can see below. The code is from an example on line that I modified a little bit. It's my first time using the interp command. I thought EOS was to end that and I can drop my older GUI code in but I simply get a grey GUI with the spinning wheel and it never resolves. If I uncomment block 1 I get a new window with the list box. If I uncomment block 2, I get the word test in the GUI. I just don't see how to get the list box into the new GUI that has DND working. Any help would be greatly appreciated. My ugly code, as I'm still trying to work out this problem, is below:

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-02-21 05:53 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (307 votes). Check out past polls.