Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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";
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
3 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:

How to use a different epoch with DateTime
5 direct replies — Read more / Contribute
by thimes
on Feb 13, 2017 at 21:00

    I am forced to use the epoch Jan 1, 2000 12:00:00. I need to convert from DateTime format to seconds, then back from seconds to DateTime format. How can I do this the easiest way?

How might I portably determine the TrueType Font filespecs for 'Generic' font specifications like 'sans-serif'?
1 direct reply — Read more / Contribute
by ozboomer
on Feb 13, 2017 at 19:38

    I have a Perl program that runs under both Windows (ActiveState v5.16.3 32-bit) and Linux (Puppy Linux v5.6.4 Slacko ... but others as well) which calls the video manipulation program 'ffmpeg' to modify the content of a video file. The current issue revolves around the adding of some text to the video image.

    'ffmpeg' is called via `` (backticks) and system()... and in both cases, the executed command is something like:-

    ffmpeg -hide_banner -i "scats.avi" \ -vf "fps=fps=pal, \ drawtext=fontsize=10: fontcolor=yellow: \ fontfile=arial.ttf: \ text='%{pts\:hms} Fr %{n}': x=8: y=h-(2*lh), \ scale=trunc(iw/2)*2:trunc(ih/2)*2 " \ -y -codec:v libx264 -pix_fmt yuv420p \ -crf 18 -vsync cfr -r pal "new.avi"

    Note that this command is fairly generic and includes some "filter clauses" that are required for .flv files, .mp4 files, etc.

    With the command as it stands, the font file "arial.ttf" must reside in the current directory... but I can fix that problem by using some perl code like:-

    $my_os = "$^O"; if ($my_os eq "linux") { $font_file = "/usr/share/fonts/default/TTF/DejaVuSans.ttf"; } elsif ($my_os eq "MSWin32") { $dir_marker = "\\"; # $font_file = $ENV{"SystemRoot"} . $dir_marker . "Fonts" . $dir_m +arker . "Arial.ttf"; $font_file = "c\\:\\\\windows\\\\fonts\\\\verdana.ttf"; }

    ...and then use '$font_file' in the 'ffmpeg' command string.

    The real problem comes from the situation where the TTF files don't exist. The files I've selected are fairly common... but is there a simple, robust, *portable* way to select some 'sans serif' -style fonts (font files) in Perl?

    Some of what is done in HTML markup comes to mind... where a font is often declared in HTML or CSS files as 'sans serif' and the user's browser is set-up to translate 'sans serif' into a specified *actual* font. Is something like that available in a Perl module? ... or can I call the O/S (system32.dll or Linux equivalent)? I'm looking for the simplest solution here... and modules like Font::TTF are as clear as mud to me :(

    I'd appreciate any thoughts on how to deal with this...


    Edit: Note the spec for $font_file under MSWin32 as shown will not work. Still trying to find a spec that will...(!)

    Edit: The pathing under Windows is horrendous... the 'normal' Perl internal magic that allows us to use '/' under Windows causes grief for 'ffmpeg'... and the 'ffmpeg' filtering items use '\' and ':' in a special way. *Ugh!* Note that the MSWin32 filespec shown *now* DOES work - it's just ugly.

Creating a Hash using only one column in an imported data file
3 direct replies — Read more / Contribute
by Koda1234
on Feb 13, 2017 at 12:29

    Hello, I'm very new to perl and I'm having a very difficult time organizing my script so that I can get it to do what I want it to do. Right now, I have a data file that is delimited by space, and has 12 columns, and 84000 rows. The only column that I care about is the 9th column. I am trying to organize the information in that column so that I can "count" the number of values given a conditional if statement (i.e. is the value in the list greater than 2.0, 3.0,...and so on.).

    My issue is this. While creating a hash, I know I am supposed to assign a "key" to a "value". How do I specify a value that is >= 2 for example, assuming i'm not going to manually calculate the values greater than 2? And how do I get the hash to pull the information from the column, in my file.

New Meditations
Working Solo and in a Team
3 direct replies — Read more / Contribute
by eyepopslikeamosquito
on Feb 13, 2017 at 04:29

    I've worked solo, and in teams, for many years now, and in many different organisations. Yet I still feel dissatisified somehow.

    While I enjoy working alone -- especially when I'm in the zone -- I have to acknowledge that individuals working alone in my organisations have produced some pretty quirky and unmaintainable code over the years. At least, that's what I've seen.

    Moreover, I write better and more relevant code not when working solo, but when receiving regular feedback from others.

    Sadly, I've found that working closely with others can be difficult and stressful, especially when personality clashes arise. The tragic human condition -- as I'm reminded every day when reading the news -- is that there are so many factors that can cause conflict in teams: gender, politics, religion, baby boomers vs generation-y, introvert versus extrovert, perl vs java, the list goes on and on.

    Pair Programming

    One of the drawbacks of being chosen to lead humanity's push to become an interplanetary species is having to give up your privacy. "You could hear everything else that happened in the dome, and they could hear you," Ms Johnston recounted.

    -- What it's like to spend a year pretending to live on Mars (news article)

    I've tried pair programming eight hours a day and hated it. Like the budding Mars astronauts, I discovered how much I value my privacy.

    High Performing Teams

    Despite my unfortunate pair programming experiences, I acknowledge the crucial importance of teamwork and effective teams. Though I've rarely experienced it personally, I know that the elusive harmonious, high performing team is certainly possible. How best to achieve it?

    According to a recent study at Google the five keys to a successful team are:

    • Psychological safety: Can we take risks on this team without feeling insecure or embarrassed?
    • Dependability: Can we count on each other to do high quality work on time?
    • Structure & clarity: Are goals, roles, and execution plans on our team clear? Do we have an effective decision-making process?
    • Meaning of work: Are we working on something that is personally important for each of us?
    • Impact of work: Do we fundamentally believe that the work we're doing matters?

    Some ideas you might try from the Google study to improve teamwork:

    • Google's gTeams exercise: a 10-minute pulse-check on the five dynamics above, a report that summarizes how the team is doing, a live in-person conversation to discuss the results, and tailored developmental resources to help teams improve.
    • Google found that teams who adopted a "new group norm" improved more than other teams. An example of a new group norm is "Kick off every team meeting by sharing a risk you took in the previous week".


    I'd love to hear of your experiences. What's been your workplace experiences, both working solo, and in a team? Which do you prefer? I'm especially interested to learn what you feel is the key to working in an effective team.


New Cool Uses for Perl
Human-visible light levels - Adafruit Breakout board with I2C
1 direct reply — Read more / Contribute
by anita2R
on Feb 12, 2017 at 14:52

    AdaFruit sells a 'breakout' board for the dual-sensor TLS2561 Light-to-Digital Converter. The board provides i2c communicatiion and 3.3 or 5 volt operation.

    Adafruit provides software suitable for the Arduino and the sensor manufacturer provides some pseudo-code and code suitable for microprocessors. As a Raspberry Pi user I needed a Linux solution, so I have produced a short Perl script to obtain light levels in Lux, from the board.

    The TLS2561 uses 2 sensors, one of which only responds to infrared, so making it possible to get an approximation of human-visible light levels, by subtraction the infrared value from the infrared + visible value, (plus some mathematical manipulations).

    Before using the script, the user needs to replace <username> and <group> with their own username and group as the script must be called as root or using sudo, and once the i2c object is created the script falls back to the user's permissions.

    The script can be called 'as is' and default values will be used, or parameters can be passed which change the sensor sensitivity and the integration (sensing) time which affects the available range from the chip's two ADC's.

    The sensitivity -s parameter takes values of 0 or 1 (normal or x16 sensitivity).
    The integration -i parameter takes values of 0, 1 or 2 (13.7mS, 101mS or 402mS).
    If the script is called with the 'verbose' -v parameter on its own or with -s &/or -i, additional information is printed, including the raw sensor values.

    The math for doing the Lux calculations comes from the TLS2561 datasheet, which hopefully I have interpreted correctly! As I don't have a Lux meter, I can't be sure, but the results under different lighting conditions appear 'reasonable'.

    Script -

    Sample calls:
    Standard sensitivity and shortest sampling duration:
    sudo -s 0 -i 0
    x16 sensitivity and longest sampling duration:
    sudo -s 1 -i 2
    Use default or last applied settings and get some additional feedback
    sudo -v

    Example output with -v
    sudo -s 1 -i 1 -v

    Timing register (old): 00010010 Timing Register (new): 00010001 Delay: 115 mS Channel 0 raw value: 4514 Channel 1 raw value: 298 Saturation value: 37177 Channel 0 scaled value: 17944 Channel 1 scaled value: 1184 Ratio is: 0.066 ******************************** * Visible light level: 520 lux * ********************************

    The breakout board from AdaFruit also includes an interrupt pin, but I have not programmed for its use, and the pin does not need to be connected. Also the adjacent 3vo pin can be left disconnected - the supply goes to the Vin pin.

    Leaving the 'Addr' pin disconnected selects the default 0x39 device address on the i2c bus.

    Any suggestions for improvements in the code or the math would be welcome.

Log In?

What's my password?
Create A New User
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-02-19 15:56 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (292 votes). Check out past polls.