Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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
Debugging Perl Mojolicious application
3 direct replies — Read more / Contribute
by sduggal
on Apr 24, 2017 at 02:58

    Hi Monks!

    I have a Perl Mojolicious framework application/web app on Windows. It is using the development server for now. Does anyone have experience on debugging in such an environment using an IDE? I want to use breakpoints and step through the code. Please guide me on the IDE in case anyone has been in the same boat.

Image Conversion: SVG to PNG
5 direct replies — Read more / Contribute
by kcott
on Apr 23, 2017 at 04:12

    I'm generating some fairly basic SVG files and will want to convert them to PNG format. Each SVG file will be used to create a number of PNG files of different sizes (it's only a size change; aspect ratio, colours, etc. all remaining the same). I'd also like the solution to be as portable as possible (at least working on *nix and MSWin platforms). I've included my investigations so far, my questions are at the end.

    Here's the test SVG file, test.svg, that I've been playing with and referenced in various examples below:

    I had the ImageMagick® convert utility available, so I tried that first. This seemed to work fine; I tested at two reduced sizes:

    $ convert test.svg -resize 16x16 test016.png $ convert test.svg -resize 32x32 test032.png

    However, when I looked into installing the Image::Magick module for use in my scripts, I was amazed to find only failures (UNKNOWN: 1276; FAIL: 412) in the "CPAN Testers Matrix: PerlMagick 6.89-1". I followed the links to the previous five releases, thinking maybe there was some problem with just 6.89-1; unfortunately, all were exactly the same except for 6.86 which had just one PASS amongst hundreds of failures. I don't know if that's some sort of reporting error. I chose not to follow that path any further.

    I had previously, successfully installed GD, so I decided to look into that further. I browsed the GD::Convert, GD::SVG and SVG modules. While this looked like it might work (after a fashion), it didn't seem particularly straightforward and even the documentation says:

    "GD::SVG may provide a short-term solution while scripts are re-written to enable more direct output of SVG."

    There were also too many caveats for my liking, so I abandoned this avenue also.

    SVG has a small section on converting to PNG. It mentions Image::Magick and also Image::LibRSVG. I hadn't heard of this other module before and decided to try it. It installed without any fuss and usage was totally uncomplicated:

    $ perl -wMstrict -MImage::LibRSVG -e 'Image::LibRSVG::->new()->convert +AtSize("test.svg", "test016.png", 16, 16)' $ perl -wMstrict -MImage::LibRSVG -e 'Image::LibRSVG::->new()->convert +AtSize("test.svg", "test032.png", 32, 32)'

    Not only that, but it produced PNG files that were substantially smaller than those produced by convert (without any noticable degradation of image quality; in fact, the 16x16 one looked a little crisper). I had renamed the files I produced earlier (*.png to *_via_convert.png), here's a size comparison:

    -rw-r--r-- 1 ken staff 678 Apr 23 13:03 test.svg -rw-r--r-- 1 ken staff 124 Apr 23 14:01 test016.png -rw-r--r-- 1 ken staff 316 Apr 23 13:10 test016_via_convert.png -rw-r--r-- 1 ken staff 186 Apr 23 13:57 test032.png -rw-r--r-- 1 ken staff 415 Apr 23 13:11 test032_via_convert.png

    My only concerns with Image::LibRSVG are its age (no update in 11 years) and no successful builds on MSWin. The age may not be a huge issue: SVG has been stable for many years and, as an interface to the librsvg library, it may rely on that library being current, especially if its API hasn't changed. I randomly checked many failures on "CPAN Testers Matrix: Image-LibRSVG 0.07": all (going back to Perl 5.10) were UNKNOWNs, and all failed due to the librsvg library not being found — this may not be a problem either.

    So, after all that, I have these questions:

    • Has anyone successfully installed Image::LibRSVG on an MSWin platform? If so, were there any major hurdles to overcome? [A Super Search for Image::LibRSVG produces no results.]
    • Does anyone have any suggestions for other modules which I could try for this conversion task?

    — Ken

how to fork & join in tk?
4 direct replies — Read more / Contribute
by redss
on Apr 22, 2017 at 18:40
    Hi Monks,

    I want a Tk program to perform a subroutine that takes a few seconds when a button is pushed. But I don't want to wait on the the subroutine before returning control to the mainloop. When the subroutine finishes I want it to update the title on the button.

    So in the below example, when the button is clicked, I want the button label to immediately update to "step two", then whenever the function finishes, to update to step 3, rather than tie up control while waiting for the function to finish.

    How can I do this?

    use Tk; + + $main = MainWindow->new(); + + $button = $main->Button(-text => "step one", -command => \&fun); + $button->pack(); + + MainLoop(); + + sub fun { $button->configure(-text => "step two" ); sleep 1; $button->configure(-text => "step three" ); }
Where does the 6th process come from? [SOLVED]
4 direct replies — Read more / Contribute
by karlgoethebier
on Apr 22, 2017 at 10:24

    Good day all,

    this is just another useless waste of time for fun and perhaps learning.

    I spawn 4 processes but ps shows 6. But it should be 5, right?

    #!/usr/bin/env perl # "When I first started hobo'in, # I took a freight train to be my friend" # -John Lee Hooker # use strict; use warnings; use MCE::Hobo; use MCE::Shared; use feature qw(say); # use Sereal (); use MCE::Mutex; say "procs at start:\n", procs(); our $mutex = MCE::Mutex->new; # say $mutex->impl(); my $result = MCE::Shared->array(); my $foo = MCE::Hobo->create( \&task, "foo" ); my $bar = MCE::Hobo->create( \&task, "bar" ); my $nose = MCE::Hobo->create( \&task, "nose" ); my $cuke = MCE::Hobo->create( \&task, "cuke" ); my @hobos = MCE::Hobo->list(); say "spawned hobos:"; for (@hobos) { if ( $_->is_running() ) { say 'pid: ', $_->pid(); } } say 'pending hobos: ', MCE::Hobo->pending(); say "procs at pending:\n", procs(); $_->join() for @hobos; say "procs after join:\n", procs(); say "result:"; say for @$result; sub task { our $mutex; $mutex->lock; $result->push(shift); $mutex->unlock; } sub procs { my $procs = qx(ps aux | grep [p]erl); $procs =~ s/ //g; chomp $procs; $procs; } __END__ karls-mac-mini:MyAdaModule karl$ ./ procs at start: karl254417,30,124421128816s000S+6:03pm0:00.07perl./ spawned hobos: pid: 2549 pid: 2550 pid: 2551 pid: 2552 pending hobos: 4 procs at pending: karl254417,30,124421129264s000S+6:03pm0:00.08perl./ karl25520,00,000s000Z+6:03pm0:00.00(perl) karl25510,00,000s000Z+6:03pm0:00.00(perl) karl25500,00,000s000Z+6:03pm0:00.00(perl) karl25490,00,000s000Z+6:03pm0:00.00(perl) karl25480,00,024421123116s000S+6:03pm0:00.00perl./ procs after join: karl254417,30,124421129264s000S+6:03pm0:00.08perl./ karl25480,00,024421123136s000S+6:03pm0:00.00perl./ result: foo bar nose cuke

    Update: Added more debugging output

    Thanks for any hint and sorry if i missed something essential.

    Update 2:

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    Furthermore I consider that Donald Trump must be impeached as soon as possible

How can one create a text file in the subfolder of a folder?
4 direct replies — Read more / Contribute
by supriyoch_2008
on Apr 22, 2017 at 05:33

    Hi Perlmonks,

    My interest is to create a text file z.txt inside a subfolder y of the main folder x. I want to write in the text file using a file handle like $fh. I searched for perl examples online but did not find one which can solve my problem. I have written a script which creates folders x and y and not the file. The result in cmd shows "Cannot open file 'x/y/z.txt'". I welcome suggestions from the perlmonks to sort out this problem.

    Here goes my script

    #!/usr/bin/perl use strict; use warnings; my $x='x'; my $y='y'; my $dirname = "$x/$y"; my @folders = split /\/|\\/, $dirname; map {mkdir $_; chdir $_;} @folders; # Print output to a Text File: my $output="$dirname/z.txt"; open (my $fh,">",$output) or die "Cannot open file '$output'.\n"; print $fh "\n It's ok.\n"; close $output; print "\n Program is over.\n"; exit;

    Here goes the result of cmd:

    Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users>cd d* C:\Users\Desktop> Cannot open file 'x/y/z.txt'. C:\Users\Desktop>
my versus our, why is my slower when script ends (global cleanup)
1 direct reply — Read more / Contribute
by marioroy
on Apr 21, 2017 at 21:51

    Respected Monks,

    On Mac OS X and Linux (not tested on other Unix platforms), there appears to be extra overhead by Perl prior to the script exiting for variables declared with my. The extra time is noticeable after seeing the "end" output.

    Why does Perl have this odd behavior during cleanup?

    use strict; use warnings; $| = 1; my $size = 2e6; my $data = [ 'AGCTCGTTGTTCGATCCA', 'GAGAGATAGATGATAGTG', 'TTTT_CCCC', 0 ]; print "begin\n"; my %barcode_hash = map { $_ => $data } 1 .. $size; print "end\n";

    There is practically no delay after seeing "end" if I declare the variable with our.

    ... our %barcode_hash = map { $_ => $data } 1 .. $size; ...

    I made a script to capture the running time, helpful on Windows. Unix has time command and shell builtin time.

    use strict; use warnings; use Time::HiRes 'time'; my $start_time = time; system(@ARGV) == 0 or die "\"@ARGV\" failed with error: $?"; printf "%0.03f seconds\n", time - $start_time;


    perl perl my %big_hash: 2.726 seconds our %big_hash: 1.351 seconds

    Thank you, karlgoethebier for this enlightenment. I do not understand what Perl is doing during cleanup. Has anyone encountered this behavior? I tested on Windows and is nothing like seen on Mac OS X and Linux.

    Thanks, Mario

text alignment
3 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 21, 2017 at 13:13

    Hi I am new to Perl and am using Padre IDE. How do I align the text in a print statement

    print "How do I align this statement so that it does not look like a r +un on sentence and take up the whole screen? \n";
Method for reducing tedium of transferring object properties to scalars
6 direct replies — Read more / Contribute
by nysus
on Apr 21, 2017 at 12:01

    Greetings fellow Monks. It's been a few weeks. I'm happy to report I'm making good progress on a fairly extensive project of mine thanks to all the kind help found here.

    So one thing that is annoying me right now is the process of populating scalars with object properties to make code more readable. For example:

    sub function { my $self = shift; my $val1 = $self->get_value1; my $val2 = $self->get_value2; my $val3 = $self->get_value3; print "Values: $val1, $val2, $val3"; }

    For me, I find transferring object properties to scalars is less error prone and makes code easier to read than something like: print "Values: " . $self->get_value1 . ', ' . $self->get_value2 . ', ' . ', ' . $self->get_value3"; or printf "Values: %s, %s, %s", $self->get_value1, $self->get_value2, $self->get_value3";

    I know it's possible to do print "${\$self->get_value}" but that is just god awful.

    Using scalars is also useful if I have to use a property several times in the same block of code or if there are many object properties to keep track of. It saves a lot of typing and, again, makes the code easier to read. But it still can be super annoying to create these scalars. I'm wondering if there might be some useful trick out there that will spare me the oh so tedium of creating scalars for my object properties. Thanks.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

one liner question
5 direct replies — Read more / Contribute
by natxo
on Apr 20, 2017 at 05:13
    I need to get the first two fields of a log file containing a certain string starting from the end of the file. Using this I get the whole last line line containing the string:
    $ perl -ne '$l=$_ if /received/; END{print $l}' /var/log/rsync.log 2017/04/20 10:50:24 [26050] sent 25 bytes received 326 bytes total s +ize 52165551
    How could I get the date fields (2017/04/20 10:50:24)? Right now I have this:
    perl -ne '$l=$_ if /received/; END{print $l}; ($date) = $1 =~ m/^(\d+ +\/\d+\/\/d+) .*/g ; print $date, " \n" ' /var/log/rsync.log
    Which should at least get me '2017/04/20' (just starting with the regex), but I get nothing back in $date, just the original line. Is it possible to achieve in a oneliner, or do I need to write a proper script? TIA.

    EDIT: Thanks a lot for your solutions. The autosplit switch is really handy.

IO::Socket::SSL sometimes says 'SSL wants a read first'
4 direct replies — Read more / Contribute
by FloydATC
on Apr 20, 2017 at 03:03

    I'm trying to write an SSL proxy using IO::Socket::SSL::Intercept in order to do DNS based inspection of traffic to a specific host, and it's mostly working except I'm seeing strange intermittent failures when trying to connect to the real SSL server on behalf of the client. Here is the relevant code:

    # SSL connect to server my $retry = 10; my $server = undef; while ($retry > 0) { $server = IO::Socket::SSL->new( PeerAddr => $host, PeerPort => "https", ); if ($server && $server->connected()) { print "proxy $$ connected to server\n"; last; } else { $retry--; warn "proxy $$ connect to server failed: $!,".IO::Socket:: +SSL::errstr(); warn "proxy $$ ".($retry ? 'will retry' : 'giving up')."\n +"; } }
    And here is a typical result. Sometimes it will work the first time, sometimes it has to retry a few times:
    proxy 4730 connect to server failed: Interrupted system call,SSL wants + a read first at ./proxy line 110. proxy 4730 will retry proxy 4730 connected to server
    I'm not seeing this against all hosts, which leads me to believe that there may be something strange going on at the far end, but I'd rather have some input from people who have done this sort of thing before I start pointing fingers.

    # uname -a Linux redacted 4.6.0-kali1-amd64 #1 SMP Debian 4.6.4-1kali1 (2016-07-2 +1) x86_64 GNU/Linux This is perl 5, version 22, subversion 2 (v5.22.2) built for x86_64-li +nux-gnu-thread-multi IO::Socket::SSL 2.037 Net::SSLeay 1.77
    Interestingly, IO::Socket::SSL debugging seems to indicate it is 'ignoring' this as a 'less severe local error' but in fact the connection fails:
    DEBUG: .../IO/Socket/ done Net::SSLeay::connect -> -1 DEBUG: .../IO/Socket/ ssl handshake in progress DEBUG: ...5.22/IO/ ignoring less severe local error 'IO:: +Socket::IP configuration failed', keep 'SSL wants a read first' DEBUG: .../IO/Socket/ free ctx 44390832 open=44390832 4401 +1664 43990720 DEBUG: .../IO/Socket/ free ctx 44390832 callback DEBUG: .../IO/Socket/ OK free ctx 44390832 proxy 4824 connect to server failed: Interrupted system call,SSL wants + a read first at ./proxy line 110.
    Any thoughts on how to fix this?

    -- FloydATC

    I got 99 problems, most of them have to do with printers.

using Statistics::Regression
2 direct replies — Read more / Contribute
by Random_Walk
on Apr 19, 2017 at 10:07

    Hi Folks

    Does anyone know the above module and what it's inputs mean? The docco is rather terse ...

    use Statistics::Regression; # Create regression object my $reg = Statistics::Regression->new( "sample regression", [ "const +", "someX", "someY" ] ); # Add data points $reg->include( 2.0, [ 1.0, 3.0, -1.0 ] ); $reg->include( 1.0, [ 1.0, 5.0, 2.0 ] ); $reg->include( 20.0, [ 1.0, 31.0, 0.0 ] ); $reg->include( 15.0, [ 1.0, 11.0, 2.0 ] );

    I was expecting to give a more simple two dimensional input of X and Y values, then get back a vector of Theta values such that...

    $predictedY = $Theta[0] + $X * $Theta[1] + $X**2*Theta[2] ... $X**3*T +heta[n];

    How can I reconcile my expectations with the multidimensional data points in the example?

    Thanks in Advance,

    Pereant, qui ante nos nostra dixerunt!
Perlbrew mismatched error
3 direct replies — Read more / Contribute
by LunarCowgirl
on Apr 17, 2017 at 16:07

    The perlbrew on my current machine was using Perl 5.22.0, so I had it install the latest Perl, 5.24.1. The install was successful, and Perl seems to work fine. Perlbrew however doesn't want to work properly with the 5.24.1 it installed. I can do basic things like switch versions, but if I run list-modules or try to use cpanm or cpan to install anything, I get the following error:

    Encode.c: loadable library and perl binaries are mismatched (got handshake key 0xd700000, needed 0xd600000)

    I tried uninstalling 5.24.1 and then installing it again, but the same thing happens. Everything works fine with perlbrew under 5.22.0. Any clues as to why perlbrew won't work on this 5.24.1? Google has not been helpful.

New Meditations
Big thank you to the Perl community.
1 direct reply — Read more / Contribute
by Anonymous Monk
on Apr 24, 2017 at 06:21

    Revered Monks and dwellers of this venerated place,

    I've been here for approximately two months now and I have no words to express my gratitude. The level of information shared here is amazing. The knowledge of the monks just blows my mind. Answers given here are of very high quality and I've come to realize that PerlMonks is the best place to learn Perl

    Just wanted to take a moment to extend a heartfelt "Thank You" to all you knowledgeable and helpful folks here. Spending a few minutes here is far more enriching and fulfilling. The suggestions and advice given by you Monks has literally shaved off hours of time. I started with Perl due to its practicality and immediacy with which it naturally lends itself to real life problem solving, but now, I have started falling in love with this amazing language, all thanks to you folks.

    This language deserves so much more attention and respect than it gets. All this talk of "Perl is Dead" now simply makes me wonder what the hell is going wrong with folks. This language will never die, because 1) its a pragmatic, practical and amazing language and 2)PerlMonks.

New Cool Uses for Perl
Parallel::ForkManager + MCE::Shared demonstration
1 direct reply — Read more / Contribute
by marioroy
on Apr 23, 2017 at 22:26

    Respected Monks,

    In preparation for the upcoming MCE 1.828 and MCE::Shared 1.825 releases, am testing various modules with MCE::Shared. One of which is Parallel::ForkManager.

    Discipulus introduced me to zentara recently. zentara am pleased to meet you. Discipulus, imho, folks may choose any parallel module of their liking. It doesn't need to be MCE and the reason for this thread. I like Parallel::ForkManager too.

    Some time back, zentara wrote a Parallel::ForkManager + IPC::Shareable demonstration. Fast forward 2.5 years and here's another way. MCE::Shared provides users of Parallel::ForkManager with threads-like sharing capabilities. Below is zentara's example updated with MCE::Shared bits.

    Not to worry, MCE::Shared performs reasonably well.

    #!/usr/bin/perl # Based on Parallel::ForkManager + IPC::Shareable by zentara. # Found here: use strict; use Parallel::ForkManager; use MCE::Mutex; use MCE::Shared; my $mutex = MCE::Mutex->new(); my $parent_share = tie my %final_parent_hash, 'MCE::Shared'; my $fork_manager = new Parallel::ForkManager(5); $fork_manager->set_waitpid_blocking_sleep(0); foreach my $child ( 1 .. 10 ) { my $pid = $fork_manager->start($child) and next; # Optional, to have the shared-manager assign a data channel. # Helpful when involving heavy IPC usage, not the case here. # Increase 20 to 2000 to see perf-increase from calling ->init. MCE::Shared->init(); for my $id ( 1 .. 20 ) { my $key = $child . '-' . $id; # $mutex->lock; # mutex not necessary when storing unique keys # $parent_share->set($key => qq{|Kid $child pushed $id}); # OO $final_parent_hash{$key} = qq{|Kid $child pushed $id}; # $mutex->unlock; } $fork_manager->finish($child); } print "Waiting for Children...\n"; $fork_manager->wait_all_children; foreach my $child ( 1 .. 10 ) { for my $id ( 1 .. 20 ) { my $key = $child . '-' . $id; if (! exists $final_parent_hash{$key} ) { print "Missing data for Kid $child , data $id\n"; } else { print "$key = $final_parent_hash{$key}\n"; } } }

    The following are recommended modules for MCE::Shared.

    ## MCE::Shared 1. Sereal::Decoder 3.015+ 2. Sereal::Encoder 3.015+ 3. Sereal (ok for completeness, but MCE::Shared doesn't load this) ## MCE::Shared applies to Condvar, Handle, and Queue 1. IO::FDPass 1.2+

    Q. Why is Sereal beneficial?

    A. The main reason is for extra performance. To ensure minimum memory consumption, there's no reason to load the Storable module if Sereal is available in Perl. This is handled transparently.

    Q. Why is IO::FDPass beneficial?

    A. Being able to construct a shared condvar, handle, or queue while the shared-manager is running greatly adds to the ease-of-use. These involve handles behind the scene. Basically, am able to send the relevant fd descriptors to the shared-manager. Without FDPass, one must be careful to construct Condvar, Handle, and Queue first before other shared objects and later starting the shared-manager manually. Note: MCE and MCE::Hobo starts the shared-manager if not already started.

    Q. What is MCE::Shared->init all about?

    A. For MCE, MCE::Hobo, and threads (via CLONE), MCE::Shared->init() is called automatically. It assigns the worker 1 of 12 data channels for use during IPC. Calling init is totally optional. If the worker is sending data one time, probably not necessary. On the other hand, if doing lots of IPC, then yes worth it.

    For further reading, see also this thread made by karlgoethebier or this reply regarding performance characteristics (TIE and Mutex or OO). Basically, performance is possible. And so is fun.

    Regards, Mario.

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 rifling through the Monastery: (6)
As of 2017-04-24 17:27 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (443 votes). Check out past polls.