Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Seekers of Perl Wisdom

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

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
Threads From Hell #2: How To Parse A Very Huge File
3 direct replies — Read more / Contribute
by karlgoethebier
on May 23, 2015 at 16:04

    Hi all,

    for learning purposes i started to think about how to parse a very huge file using the multithreading capabilities of Perl.

    As i like trivial examples, i started out with something trivial and created some huge file at first:

    karls-mac-mini:monks karl$ ls -hl very_huge.file -rw-r--r-- 1 karl karl 2,0G 23 Mai 19:38 very_huge.file karls-mac-mini:monks karl$ tail very_huge.file Lorem ipsum kizuaheli Lorem ipsum kizuaheli Lorem ipsum kizuaheli Lorem ipsum kizuaheli Lorem ipsum kizuaheli Lorem ipsum kizuaheli Lorem ipsum kizuaheli Lorem ipsum kizuaheli Lorem ipsum kizuaheli nose cuke karl karls-mac-mini:monks karl$ wc -l very_huge.file 100000001 very_huge.file

    By RTFM i figured out this using MCE::Grep:

    #!/usr/bin/env perl use strict; use warnings; use MCE::Grep; use Data::Dump; use Time::HiRes qw (time); MCE::Grep::init( { max_workers => 4 } ); my $start = time; open( my $fh, '<', 'very_huge.file' ); my @result = mce_grep { /karl/ } $fh; close $fh; printf "Took %.3f seconds\n", time - $start; dd \@result; __END__ karls-mac-mini:monks karl$ ./ Took 29.690 seconds ["nose cuke karl\n"]

    Good old grep performs very much better easily:

    karls-mac-mini:monks karl$ time grep karl very_huge.file nose cuke karl real 0m2.563s user 0m2.176s sys 0m0.309s

    I don't know if this trivial exercise is peinlich parallel, but i'm wondering how to:

    • do this "by hand" (without using MCE::Grep)
    • ...and improve the performance

    Thank you very much for any hint and best regards,


    Ouch! Perhaps more RTFM would have helped:

    PID Prozessname Benutzer % CPU Physikal. Speic Virt. S +peicher 1065 perl karl 12,7 10,3 MB 2, +33 GB 1068 perl karl 83,7 3,9 MB 2, +33 GB 1069 perl karl 84,6 3,9 MB 2, +33 GB 1070 perl karl 83,5 3,9 MB 2, +33 GB 1071 perl karl 84,0 3,9 MB 2, +33 GB


    «The Crux of the Biscuit is the Apostrophe»

Limited idea in perl network programming IO::Socket::INET threads
No replies — Read more | Post response
by Muskovitz
on May 23, 2015 at 10:41

    Hello monks! im here again because i can't really make a messenger in perl i have a very limited amount of networking in perl i was trying to create a messenger in perl where you can select who you want to chat e.g i want to chat John so i just want to click his name and start chatting. So here's my code i push all the filehandles in 1 array which it turns to group chat. But i don't want group chat but i don't know what to do with this filehandles!

    #!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; use threads; use threads::shared; my @clients : shared; @clients=(); my $socket=new IO::Socket::INET( LocalAddr=>'localhost', LocalPort=>7777, Proto=>'tcp', Timeout=>7200, Listen=>7, Reuse=>1 ) || die "$!\n"; while(1){ my $client; $client=$socket->accept; my $peerhost=$client->peerhost; my $peerport=$client->peerport; print "$peerhost:$peerport\n"; my $f_client=fileno $client; push(@clients,$f_client); my $thr=threads->create(\&accept,$client,$peerhost,$f_client); } sub accept{ my ($client1,$peerhost1,$f_client1)=@_; if($client1->connected){ while(<$client1>){ foreach my $fn(@clients){ open(my $fh, ">&=$fn") || die "$!\n"; print $fh "$_"; } } } }
Conditional use of modules
2 direct replies — Read more / Contribute
by tkguifan
on May 23, 2015 at 07:08
    I'm planning to upload my GUI to GitHub but for portability reasons I want to make it depend solely on core perl + Tk and make using other modules conditional. For example the GUI has functions that copy some content to the Windows clipboard, but this is certainly not portable ( it requires an operation system specific module and even the very concept of a clipboard is not portable between operation systems ). What is the prudent way to test whether the clipboard module is present in the system and make the use of the module conditional on this?
    use Win32::Clipboard;
a full-featured control for uploading files and a couple other tidbits
2 direct replies — Read more / Contribute
by Datz_cozee75
on May 23, 2015 at 03:10

    Hello Monks,

    I've been rather ambitious with perl projects lately, porting my html template from a linux laptop to a windows 8.1 laptop with a cygwin installation. I was stunned with how little resistance I encountered, but I do have a couple problems. First, I'd like to address the main topic of this post, which is creating a sensible control for uploading binary content to my site. Let me show you what I have but preface it by saying that I'm without perltidy at the moment (see question 2).

    sub upload { use strict; use warnings; use 5.010; use Net::FTP; use Data::Dumper; my ($rvars, $rftp) = @_; my %vars = %$rvars; # get local files into an array my @filetypes = qw/jpg jpeg png ogv mp4 m4v webm/; my $pattern = join '|', map "($_)", @filetypes; my @matching; opendir my $eh, $vars{"to_vids"} or warn "can't open vids $!\n"; while (defined ($_ = readdir($eh))){ next if m/~$/; next if -d; if ($_ =~ /($pattern)$/i) { push(@matching, $_); } } closedir $eh; # stat local files, create hash #my %stat = map { # lstat($_) or die "Can't lstat $_: $!"; # $_ => { # s => ( -s _ ), # } #} @matching; #my $hashref = \%stat; #print Dumper($hashref); $rftp->cwd("/pages/eh5v.files") or warn "cwd failed in main $!\n"; my $rlist = $rftp->ls(); say "list is @$rlist"; $rftp->binary or warn "binary failed$!\n"; for ( @matching) { #say "matching is @matching"; my $a = file($vars{"to_vids"},$_); my $sa = $a->stringify; #$rftp->put($sa, $_) or warn "put failed $@\n"; } $rftp->ls(); return $rftp; }

    The relevant part of the output is:

    loading vids list is . .. kitchen.jpg kitchen.m4v kitchen.webm stairs.jpg stairs.m4 +v

    What am I trying to do? I want a control to loop through the above files, and first see whether it exists on the remote server. If not, we upload it. If yes, then I'm interested in whether they are either a differing size or creation date. That's what the commented lstat part was to address, but I got errors out of that, because I didn't build the full path. (I think.) If they are different, I want a prompt to overwrite with the default being yes. If they are not different, then I want a prompt to overwrite with the default being no. It would be cool if this could work on a five-second timer. Furthermore, I would like not to create race conditions. And finally, I'd like to have a portion of code to deal with error handling such as time-outs, or directory doesn't exist. The directory, of course "should" exist, but that's one of those words....

    So that's very ambitious, and I'm not completely-wed to any part of the spec if it's ill-advised, but I thought I'd throw the whole thing out there in the original post. If I get it right once, it's eminently re-usable.

    I have 2 other questions that are unrelated to the above. I don't think they deserve their own threads. To the extent that a subthread lasts more than one level, maybe we can rename it appropriately. How do I get CPAN to kickstart?

    $ perl -MCPAN -e shell Set up gcc environment - 3.4.5 (mingw-vista special r3) cpan shell -- CPAN exploration and modules installation (v1.9800) Enter 'h' for help.

    This hangs. I'm running the 64-bit cygwin download that is no more than 2 weeks old on a 64-bit windows 8.1 machine. Do I have cpan and not know it? I don't think so:

    Fred@Psiborg ~/pages2/krov $ sudo cpan -bash: sudo: command not found Fred@Psiborg ~/pages2/krov $ cpan -bash: cpan: command not found Fred@Psiborg ~/pages2/krov $ cpan My :: Module -bash: cpan: command not found Fred@Psiborg ~/pages2/krov $

    Finally, I reached the level of Scribe recently, and I think it would behoove me to have a better brand name. To this end, I'd like to change my username to something cooler and more-identifiable than my current one, which is just an arbitrary 'nym. How do I do that?

CAM::PDF populating form fields
1 direct reply — Read more / Contribute
by PeterPerl2011
on May 22, 2015 at 22:26
    I have code that successfully populates fields in a pdf from a database, using CAM::PDF. However, the text is invisible. That is, when you open the output pdf, the fields appear to be blank. If you click in a field, you see the text that should be there. When you click away, the text disappears again. The text only appears in the field with cursor.

    Anyone else ever see that?

    my $pdf = CAM::PDF->new($file); $pdf->fillFormFields(%form); $pdf->cleanoutput($outfile);
Again Fuzzy regex !!!!
4 direct replies — Read more / Contribute
by samman
on May 22, 2015 at 17:20

    Help Me perl Monks !!!!!! I know that you have asked this question a million times before but I didn't find an answer full my needs > I want to fuzzy search a short string ( up to 25 letter ) against a long string (very very long ) with miss match >> Yes I tried Xor :

    my $str = $Genome; my $match = "ATCGACTACGACTCGACT"; my $len = length($match); for my $i ( 0 .. length($str) - $len ) { my $s = substr $str, $i, $len; my $x = $s ^ $match; $x =~ tr/\0//d; # equal characters xor to \0 print "'$s' at offset $i matched\t".length($x)."\n" unles +s length($x) > 5; }

    but it was slow but I found this code which has been writen was in c but I couldn't handle it , to extract what he did and rewrite it in perl, I want to do the same like what he did but in perl >>> as it was faster , could you please tell me how to do like it in perl !!!!!!!

Memory leaks in DBD::Firebird
3 direct replies — Read more / Contribute
by Steve_BZ
on May 22, 2015 at 12:32

    Hi Monks,

    There seems to be a memory leak in DBD::Firebird (or one of its dependencies). Every call after the first one seems to leak 1 Scalar Value (SV).

    Here is a code sample which will create a table, query it 6 times and drop it.

    I have upgraded to the latest version of DBD::Firebird and I'm on Kubuntu 14.04 lts with Firebird 2.5.

    Update: Thanks to sierpinski for pointing out that there was no question here. Has anyone seen this before? I imagine it might be the driver, but I guess it could be Firebird too. I'd like to see where the problem is. If it's fixable, we should fix it, otherwise I'll need to workaround it. Suggestions welcome.

    Here is the code, you just need to create an empty database file (I use Flamerobin for this) and modify the dsn in the code appropriately.

    Thanks for your help

    Code follows



issue with perl debugger when using weak reference
No replies — Read more | Post response
by ssaraogi
on May 22, 2015 at 11:51
    I am using Test::MockModule in one of my unit tests which fails in the debug mode (perl -d <test>) but passes in the normal mode (perl <test>)

    After some debugging, i found it to be a weak reference not getting destroyed in the debug mode.

    I tried searching for similar issues but could not find anything related so far. I am not able to further figure out what the issue is here and needed help.
    What could be the issue here? Is this a known issue?

    Following code reproduces this issue:

    >> ./
    Use of uninitialized value $weak_objref in concatenation (.) or string at line 48.

    >> perl -d ./
    #!/home/utils/perl-5.10/5.10.1-nothreads-64/bin/perl use strict; use warnings FATAL => 'all'; use Scalar::Util qw(weaken); my $package_name = 'Package'; my $method_name = 'method'; my $weak_objref; { # This function creates a weak object reference and returns a code + reference sub create_weak_objref { # create any object my $self = bless {}, $package_name; # make a weak reference to the above created object $weak_objref = $self; weaken( $weak_objref ); # return a code reference which mucks around with the above cr +eated object return sub { # Need following to be able to use the string $resolved_me +thod_name later as symbol reference no strict 'refs'; # Both the following steps needs to be done to be able to +reproduce bug in the debug mode # Skipping any of them or moving any out to the parent met +hod does not reproduce the bug # set any arbitrary data inside the object $self->{_arbit_data} = 1; # update packages symbol table to create method 'method_na +me' my $resolved_method_name = "${package_name}::{${method_nam +e}}"; *{$resolved_method_name} = sub {}; }; }; my $coderef = create_weak_objref(); # call the returned code reference $coderef->(); } # Weak reference should be undefined here - but it does get printed in + the debug mode :( print "$weak_objref\n"; 1;
Strawberry Perl and alarm() on Windows
2 direct replies — Read more / Contribute
by bloonix
on May 22, 2015 at 11:35
    Hello together,

    today I tested Strawberry Perl under Windows 8 and it seems that alarm() works. I am confused.
    #!perl.exe use strict; use warnings; print scalar localtime, "\n"; eval { $SIG{__DIE__} = sub { alarm(0) }; $SIG{ALRM} = sub { die "timeout" }; alarm(3); sleep 5; alarm(0); }; print $@ if $@; print scalar localtime, "\n";
    The output:
    Fri May 22 17:21:27 2015
    timeout at c:\test\ line 9.
    Fri May 22 17:21:30 2015
    Is that a new feature in newer Windows versions or does Strawberry Perl implements a workaround for alarm?

    Regards, Jonny
OT: Windows Non-Full-Screen Remote Desktop
1 direct reply — Read more / Contribute
by marinersk
on May 22, 2015 at 09:15

    Apologies for the off-topic post, but I asked this question at StackOverflow and, other than a mysterious drive-by downvote, I have had no response.

    I'm hoping someone here, with an eye toward automation in any environment where Perl runs, might have bumped into this and knows how to work around it.

    I'm trying to launch Remote Desktop in a way that permits my local keyboard-driven macros to manipulate the window without having to first reach for the mouse and click on the "Restore Down" button.

    I've tried a few variants of the command line invocation (both from Perl and from the actual command line) and I just can't seem to get it to launch in the mode I want without creating an RDP file. If that's the answer I have to have, then so be it, but I was hoping for one less layer of kludgeness here.

    My basic invocation technique is:

    mstsc /v:%SFS_FNDKEY% /w:1024 /h:768

    Adding the /f parameter forces it to occupy the full screen, as expected, but leaving the /f off leaves it in this odd hybrid state; it does not take up the full screen, but considers itself to be in full-screen mode, complete with the "Restore Down" button which needs to be clicked before my local keyboard-driven macros can assume control of the window.

    Any cluebats out there? I know it's likely a fairly odd request, since most people want full-screen mode, but being different is nothing new over here. : -)

Add your question
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (4)
    As of 2015-05-24 03:53 GMT
    Find Nodes?
      Voting Booth?

      In my home, the TV remote control is ...

      Results (470 votes), past polls