Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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
mysql_connect_timeout laughs at me
No replies — Read more | Post response
by bbarnett
on Jan 18, 2017 at 18:42
    Nothing I do, seems to make mysql_connect_timeout actually function.

    I've read endless posts, via Google and otherwise on the subject. I've appended in many forms, mysql_connect_timeout=1 to my connect line.



    and, I've tried adding port=, tried not using identifiers (eg, just db;;3306;mysql_connect_timeout=1) and about 17 other variations.

    In all of these instances, I can hear the deep, evil laugh of mysql_connect_timeout in the depths of my mind.

    I was going to move on to using some form of timeout, but *then* read about 17.3 thousand posts about "don't do that, you'll get a segfault!', or -- 'use this method with perl 5.x or you will feel extreme pain!'.

    I believe I'm running 5.18 of perl, and it's ubuntu. I'll be able to check more directly when at work tomorrow -- that evil laugh, that never ending laugh kept me distracted, and I did not check versions prior to leaving.

    Any immediate suggestions? Ideas? A "why are you doing that, now I'm laughing too!" thoughts?

    Any help mucho appreciated.


Please help with Regexp::Common
3 direct replies — Read more / Contribute
by scorpio17
on Jan 18, 2017 at 18:26

    I'm trying to teach myself how to use Regexp::Common, and I'm having a little trouble.

    The following works as expected, and finds the number 1234 embedded in the string aaaa1234cccc:

    use strict; use Regexp::Common; while ( my $word = <DATA> ) { chomp $word; if ( $word =~ /$RE{num}{int}/ ) { print "Integer detected: \"$word\"\n"; } else { print "$word\n"; } } __DATA__ aaaabbbbcccc aaaa1234cccc ddddeeeeffff

    However, this does NOT work as I would expect:

    use strict; use Regexp::Common; while ( my $word = <DATA> ) { chomp $word; if ( $word =~ /$RE{profanity}/ ) { print "Profanity detected: \"$word\"\n"; } else { print "$word\n"; } } __DATA__ aaaabbbbcccc aaaaXXXXcccc ddddeeeeffff

    In this case, change XXXX into your favorite 4 letter offensive word. If I change the data string to this: "aaaa XXXX cccc" (i.e., add spaces around the XXXX, then it finds it).

    It seems like the profanity patterns have start of word / end of word anchors built into the patterns, and thus don't work if the word is embedded inside another string? Is there any way to control this behavior? I've gone through the docs, but so far I can't find a way.

    I'm using perl 5.14 (activestate) on Win7. Thanks for any push in the right direction.

Common module lib across Perl versions on same system
3 direct replies — Read more / Contribute
by VinsWorldcom
on Jan 18, 2017 at 15:45

    I've done a bit of searching, but I'm not getting the right results so thought I'd pose the question here.

    I use Strawberry Perl on Windows and have a few different versions installed (5.8, 5.14, 5.18 32/64, 5.22). I manage them with a simple batch script that changes a directory junction from C:\strawberry to the appropriate Perl. Works great.

    HOWEVER, when I switch from one to another, I "lose" all my modules - as expected. In other words, when I CPAN install a non-core modules to version 5.14 for example, when I switch to version 5.18, it isn't there - no shocker. ...

    But could it be?

    For some pure-perl modules, there should be *no* issue providing there are no backward-compatibility, new feature enhancements used. It'd save a bunch a time installing the same module 5 times for each of my 5 versions. I do have a %USERPROFILE%\perl directory and 'set PERL5LIB=%USERPROFILE%\perl\lib' in Command Prompt environment variables.

    But are there implications? I'd install in 1 version of Perl so that CPAN database gets updated, but the other 4 wouldn't - what craziness could ensue? Has anyone done this or is there a better way that I'm missing? My searching keeps turning up local::lib - which is bundled in vendor/lib with Strawberry, but this sounds like what I'm already doing with PERL5LIB.

Feature Idea: qr//e
2 direct replies — Read more / Contribute
by haukex
on Jan 18, 2017 at 08:47

    Dear Monks,

    As I was thinking about this node about dynamically building regexes, I had the idle thought that it might be nice if qr// supported qr//e, analogous to s///e it would eval the inside of the construct before parsing it as a regex. Now this is really just a very minor itch, and I don't yet have any idea of how much sense it makes or how difficult it might be to implement, but I still thought I'd bounce it off of you.

    # The "normal" solution my $regex = join '|', map {quotemeta} qw/. | %/; $regex = qr/$regex/; # The "hacked" solution my $regex2 = qr{@{[ join '|', map {quotemeta} qw/. | %/ ]}}; # Wouldn't this be a bit nicer? my $regex3 = qr{ join '|', map {quotemeta} qw/. | %/ }e;

    Thoughts? Maybe the "normal" and/or "hacked" solutions above are good enough, and the effort required to implement qr//e isn't worth it? Other potential problems I haven't noticed yet?

    -- Hauke D

ActiveState new vs old?
3 direct replies — Read more / Contribute
by enrgyxprt
on Jan 18, 2017 at 07:59
    Its been a while since I installed ActiveState perl, perl -V shows I have V5 something... So I updated all using PPM.. Now I am thinking about updating to the newest activestate community version... Any downsides ?
Last service name comes up empty in NMAP Parser
2 direct replies — Read more / Contribute
by Noosrep
on Jan 18, 2017 at 05:53

    I have an XML file that is generated as follows:

    <port protocol="tcp" portid="139"><state state="open" reason="syn-ack" + reason_ttl="64"/><service name="netbios-ssn" method="table" conf="3" +/></port> <port protocol="tcp" portid="445"><state state="open" reason="syn-ack" + reason_ttl="64"/><service name="microsoft-ds" method="table" conf="3 +"/></port> <port protocol="tcp" portid="1984"><state state="open" reason="syn-ack +" reason_ttl="64"/><service name="bigbrother" method="table" conf="3" +/></port>

    With following code, I parse it:

    my $fct_openPort = (join ',',$fct_host_obj->tcp_ports('open'))."\n" ; my @fct_ports = split ',', $fct_openPort; for my $fct_port (@fct_ports) { my $fct_svc = $fct_host_obj->tcp_service($fct_port); my $fct_service = $fct_svc->name() ; print "\nservice : $fct_service \n" ; print "port : $fct_port \n" ; print "service and port = $fct_port:$fct_service \n" ; }

    However, the last open port has no service!

    service : netbios-ssn port : 139 service and port = 139:netbios-ssn service : microsoft-ds port : 445 service and port = 445:microsoft-ds service : port : 1984 service and port = 1984 :

    Does anybody have any why the last service isn't resolved? When I change

    my $fct_svc = $fct_host_obj->tcp_service($fct_port);


    my $fct_svc = $fct_host_obj->tcp_service("1984");

    it does work

whats wrong with this code?
6 direct replies — Read more / Contribute
by purnak
on Jan 18, 2017 at 03:53
    Sorry if im being stupid, but i just dont understand what is wrong with this piece of code?
    my @array=(1..10); print "@array\n"; foreach(@array){ shift (@array); print "@array\n"; }
    I expect the output to be..
    1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 3 4 5 6 7 8 9 10 4 5 6 7 8 9 10 5 6 7 8 9 10 6 7 8 9 10 7 8 9 10 8 9 10 9 10 10
    Instead the output stops at...
    1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 3 4 5 6 7 8 9 10 4 5 6 7 8 9 10 5 6 7 8 9 10 6 7 8 9 10
binding listbox with perl tkx
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 17, 2017 at 12:15
    hello, i am using perl 5.22 with tkx on Windows 10. with tk, i succeed to bind buttonrelease-1 to a new_tk__listbox. but with tkx i do not succeed to bind the button to a new_tkx_Scrolled("listbox". that is to say, when i clic, Nothing happens. if somebody can look at this problem, that would be very fine for me, thanks by qadvance. here is my little test program.
    use Tkx; use Tkx::Scrolled; use strict; my $lbox; my $mw= Tkx::widget->new("."); $mw->g_wm_minsize(200,200); $mw->g_wm_resizable(1,1); $mw->g_wm_focus(); display_main(); Tkx::MainLoop; sub clic { #$box_value=$sous_lbox2->get($sous_lbox2->curselection()); my $val=$lbox->curselection(); print "val=$val\n"; return if ($val eq ""); my $value=$lbox->get($lbox->curselection()); print "$value\n"; } sub display_main { # $lbox=$mw->new_tk__listbox(-height=>5,-selectmode => "single"); $lbox=$mw->new_tkx_Scrolled("listbox",-scrollbars=>"se",-height=>5,- +selectmode => "single"); $lbox->g_pack( -anchor=>'n', -padx=>3, -pady=>3, -expand=>1, -fill=>'both'); foreach my $key ("aa","bb","cc") { $lbox->insert('end', "$key- "); } $lbox->g_bind('<ButtonRelease-1>' , sub { clic() }); }
Installing Perl v24 does not work!
4 direct replies — Read more / Contribute
by Wooden Shoe
on Jan 17, 2017 at 09:34
    I dowloaded Perl from an NCBI website --> Applications of the E-Utilities. The version turned out to be v18. Later I found that the latest version is recommended. This turned out to be version 24. I downloaded this version, but cannot get it to install. E.g
    $ Perl -v This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-th +read-multi-2level (with 2 registered patches, see perl -V for more de +tail) Copyright 1987-2013, Larry Wall $ /Users/michael.bunnemeyer/Downloads/perl-5.24\ 5.0/installperl ; exi +t; -bash: /Users/michael.bunnemeyer/Downloads/perl-5.24 5.0/installperl: +./perl: bad interpreter: No such file or directory logout Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. Deleting expired sessions...24 completed. [Process completed]
    Apparently v24 is not installed, as per $ perl -v Wooden shoe
How to optimized mysql insert into table
4 direct replies — Read more / Contribute
by Mjpaddy
on Jan 17, 2017 at 09:32

    Hello Mongers

    I'm attempting to dump records into table from a txt file with validation and substring. This file have 170568 records.

    From normal reading files and taking out substring out of each line from file and preparing insert statement to dump records took me arround ~14400 sec i.e. ~4 hr & more

    Is there any way to optimized this process like multithreading or something but I never use it before

    For time being I not put my data instead I build scenario

    Input file is like

    00032463000000001000000010000010014110662004071664629003128182 O# 00032463000000002000000010000010014110662004071664729003070909 O# 00032463000000003000000010000010014110662004071664829003861044 O# 00032463000000004000000010000010014110662004071664929003005555 O#

    Sample Code

    use strict; use warnings; use DBI; my $start = time(); my $data; my ($version,$jobnumber); my $file = "C:/temp/dump.txt"; open (FH, "$file") or die "$!"; while(<FH>){ $data .= $_; } $version = substr($data,0,3); $jobnumber = substr($data,4,5); my $databaseName = "basedb"; my $connectHost = ""; my $DSN = "DBI:mysql:dbname=$databaseName;host=$conn +ectHost"; my $db_username = 'root'; my $db_password = 'root'; my $dbh = DBI->connect($DSN,$db_username,$db_passwor +d,{RaiseError=>1, PrintWarn=>0,A +utoCommit=>0}) || die "error connecting: $!"; my $dump_sql = "INSERT INTO temp (version,jobnumber) VALUES(?,?);"; my $dump_sth = $dbh->prepare($dump_sql) or die "$DBI::errstr"; eval{ $dump_sth->execute($version,$jobnumber) or die "$DBI::errstr"; $dbh->commit; }; if($@){ $dbh->rollback(); print "$@\n"; } my $end = time(); my $elapsed = $end - $start; print "Job took $elapsed sec to execute.\n";

    I think I clear my question Please give me suggestion.

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2017-01-19 00:56 GMT
    Find Nodes?
      Voting Booth?
      Do you watch meteor showers?

      Results (166 votes). Check out past polls.