Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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
Array of variables
5 direct replies — Read more / Contribute
by Michael W
on Apr 27, 2017 at 11:38
    my @Variables = ( $Map_Request_Date,$Map_Due_Date,$Map_Cutover_Date,$ +Map_Complete_Date,$Map_Approved_Date); foreach $Date_Ref (@Variables) { print $Date_Ref; $Date_Ref =~ s/ +/ /; #When day is a single digit it creates two wh +ite spaces ($Month,$Day,$Year,$Time)= split / /, $Date_Ref,4; my %Months = ( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' + => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' + => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' +=> '12' ); if (length($Day) == 1) {$Day = "0$Day";}#Add 0 to the front of sing +le digit days @Variable[$X]=$Year."-".$Months{$Month} ."-" . $Day ; $X=$X+1; }

    Working on a date issue from sql to html format

    previous only used an array of variables to read from

    this time I want to place the value back into the array of variables

    this line: @Variable$X=$Year."-".$Months{$Month} ."-" . $Day ;

    all the code works until I try to write the new variable back in

Randomly reassign hash keys
6 direct replies — Read more / Contribute
by cormanaz
on Apr 26, 2017 at 12:47
    Good day bros. I would like to create a hash, then randomly reassign the keys (to support a randomization test). I know I can get a shuffled list of keys with
    use List::Util qw(shuffle); ... foreach my $k (shuffle keys %foo) { ... }
    but accessing that would leave the keys associated with the same values. I want to randomly reassign the keys to different values already in the hash. So if the original was
    %hash = ( "a" => 1, "b" => 2, "c" => 3, "d" => 4 };
    I could end up with
    %hash = ( "c" => 1, "d" => 2, "a" => 3, "b" => 4 };

    I can imagine how to do that in an inelegant way, but was wondering if there is an elegant way.

Malformed JSON Error Piwigo
1 direct reply — Read more / Contribute
by kiteboywales
on Apr 26, 2017 at 09:43

    Hi all - I looking for advise regarding the following error

     malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "<br />\n<b>Warning</...") at /usr/share/perl5/JSON.pm line 171

    Hope you dont mind asking - I get this error when uploading to something called PIWIGO. An online image gallery. The perl script itself is available here http://piwigo.org/ext/extension_view.php?eid=606

    While images do upload fine I get the above error and then the uploading stops. If I run the script again it can be seen that the image it seemed to fail on did upload and then it moves onto the nexxt image and then again the error pops up again.

    If someone has any ideas that would be great....thanks for any advice

Re-dimensioning an HTML table with Perl ?
12 direct replies — Read more / Contribute
by TheDonald
on Apr 25, 2017 at 13:08
    I'm staring down the abyss of something that I would rather not do manually. So it occurred to me that Perl is probably just the sort of thing for such a random requirement. What I need to do is "re-dimension" (what is the correct word ?) an HTML table. The table is currently 5x12, and I want to turn it into, say, 7x9. The thought of lifting and shifting dozens of cells by hand is filling me with dread ! I've no ideas where to start in terms of automating this, let alone how to code it (I've only recently joined the long road to Perl wisdom !).
"my" declaration problem
6 direct replies — Read more / Contribute
by Hosen1989
on Apr 25, 2017 at 07:17

    Dear ALL,

    I was debug some old dirty script of mine (which without strict nor warnings ^_^), and faced this issue:

    As you can see in case(1), variable $me had been declared more than once, and to output of this script is: []

    #----------[ CASE(1) ]----------# my $v_place = 'home'; my $me = 'moving'; my $me = 'at home' if($v_place =~ m/^home/); my $me = 'at work' if($v_place =~ m/^work/); print "[$me]"; __END__ output: [] #-------------------------------#

    But after correct the declaration problem as in case(2), we got the correct output: [at home].

    #----------[ CASE(2) ]----------# my $v_place = 'home'; my $me = 'moving'; $me = 'at home' if($v_place =~ m/^home/); $me = 'at work' if($v_place =~ m/^work/); print "[$me]"; __END__ output: [at home] #-------------------------------#

    Now,can any monk explain me just what happen here?

    BR

    Hosen

'Missing version identifier' error while connecting to Hive, using Thrift module of Perl
1 direct reply — Read more / Contribute
by Omnitrix
on Apr 25, 2017 at 07:03

    I tried to connect to Hive using Thrift::API::HiveClient2. Wrote the following:

    #!usr/bin/perl use strict; use warnings; use Thrift::API::HiveClient2; my $port = 12345; my $host = '123:456:789:987'; my $client = Thrift::API::HiveClient2->new( host => $host , port => $port, ); $client->connect() or die "Failed to connect\n"; my $query = "hive query;"; my $rh = $client->execute($query);

    An I am getting the following error->

    Thrift::TException error: Missing version identifier (code 0) (in cl +eanup) Thrift::TException error: Missing version identifier (code 0) at (eval + 144) line 31.

    My Hive client is HiveClient2. What am I missing?

Debugging Perl Mojolicious application
4 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
6 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 # https://www.youtube.com/watch?v=1kQlRQRGdfQ 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$ ./hobo.pl procs at start: karl254417,30,124421128816s000S+6:03pm0:00.07perl./hobo.pl spawned hobos: pid: 2549 pid: 2550 pid: 2551 pid: 2552 pending hobos: 4 procs at pending: karl254417,30,124421129264s000S+6:03pm0:00.08perl./hobo.pl 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./hobo.pl procs after join: karl254417,30,124421129264s000S+6:03pm0:00.08perl./hobo.pl karl25480,00,024421123136s000S+6:03pm0:00.00perl./hobo.pl 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 x4.pl 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 x4.pl:

    #!/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>x4.pl 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;

    Results:

    perl timeit.pl perl test.pl 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

New Meditations
Big thank you to the Perl community.
2 direct replies — 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: http://www.perlmonks.org/?node_id=1104697 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.

New Monk Discussion
The Cookies account
3 direct replies — Read more / Contribute
by Lady_Aleena
on Apr 26, 2017 at 04:47

    A little over four years ago, I created the Cookies account for fun. I was known for my chatterbox antics with cookies; so on April Fools 2013, I decided to have some fun. Two years ago I went in for surgery, which is scary. I was thinking something could happen, and Cookies would be left without someone to maintain the account.

    Some users still play with the buttons from time to time. I would not want the account to die just because I did. So, if anything happened to me, would there be someone who would be willing to take over this very silly account?

    P.S. I am always willing to add more flavors and options to the account. Just let me know.

    P.P.S. This thread can be for anything related to the Cookies account that might merit general discussion, too. 8)

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena, the Cookie Lady
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2017-04-27 20:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (514 votes). Check out past polls.