Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
What basic things should I know about versioning modules?
2 direct replies — Read more / Contribute
by nysus
on Feb 23, 2017 at 06:34

    I'd like to get some general guidance on how to properly use versioning for my modules.

    My question is prompted by the Module::Starter::PBP module. It has a starter template here.

    The starter template throws an error on this line: use version; $VERSION = qv('0.0.3');. The error is Global symbol "$VERSION" requires explicit package name (did you forget to declare "my $VERSION"?)

    While I could slap a my in front of it and be on my way, I am wondering if maybe it was purposefully left off. I also don't know how this line might be used by other modules. And should I use "my" or "our?" What does qv do? I can't find it in the perldoc but I do find mention of it here on CPAN. But I never installed that module to my knowledge.

    If someone could point me to a good resource that can help explain the what's, why's, and how's of module versioning, I'd appreciate it. Thanks.

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

convert encoding
4 direct replies — Read more / Contribute
by rumpumpel1
on Feb 23, 2017 at 04:11

    from Data::Dumper I get a string like this: "Nach R\x{fc}cksprache mit". How do I convert this into: "Nach Rücksprache mit" ?

    I know this has been asked a lot of times. I spent the last hours for reading a lot of threads. The only result is total confusion and I never managed to do this conversion.

How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column
2 direct replies — Read more / Contribute
by rockyurock
on Feb 23, 2017 at 02:33

    Issue : I am able to read the variables from excel and print them in the above program but I want to read these variables(from Variables to read from Excel sheet :) value from Input data (from My input data to search from:) and put them back into excel sheet which I am not able to do. can some one pls help on the same ?

    Variables to read from Excel sheet : isim = usim = sim_invalid = pcscf_discovery_flag signaling_flag ran_type emergency_ind rat pcscf_v4 pcscf_v6 plmn is_ems_support ipv4 ipv6

    My input data to search from: 8077 8116 D type hexa usim = 1, isim = 1 8077 8116 D type hexa usim = 1, isim = 1 pcscf_v4 : num = 2, protocol_type = 0x21, port_num = 0, addr = 8088 1223 D temp sim_invalid = 0 8099 1223 XX is_ems_support = 1 #88 8099 1224 XX pdn_act_ind->pcscf_discovery_flag = 1 ind->signaling_flag = 1 some text here plmn = 405872 DefaultStatusBarPlmnPlugin: into updateCarrierLabel ipv6 = get_ipv6_prefix,temp ipv4 = _send_ipv4_notify

    MY program: #use strict; #use 5.010; use Spreadsheet::Read qw(ReadData); my $book = ReadData ('UE_NW_Parameters.xlsx'); #my @files = grep { -f } (<*.txt>,<*main_log>,<*Project>,<*properties> +); my @files = grep { -f } (<*main_log>); my @rows = Spreadsheet::Read::rows($book->[1]); foreach my $i (1 .. scalar @rows) { foreach my $j (1 .. scalar @{$rows[$i-1]}) { my $temp = "\n$rows[$i-1][$j-1]"; my @keywords = "$temp"; print @keywords; } foreach my $file (@files){ open(my $fh, '<', $file) or die $!; my @content = <$fh>; close($fh); my $l = 0; foreach my $kw (@keywords) { foreach (@content){ $l++; my $search = $kw; if ($search =~ /(\ d+)/) { printf 'Found keyword %s in file %s, line %d:% +s'.$/, $search, $file, $l, $_; last; } } } } }
Regex lookahead, lookbehind
2 direct replies — Read more / Contribute
by rahulme81
on Feb 23, 2017 at 00:49

    Happen to come across Regex lookahead, lookbehind and atomic groups in perl, though not very expert to use in first try.

    Got a thought - If I can match a string name within a single regular expression with this concept

    So I can avoid using multiple match patterns to deal with something required on this matches within my program

    My search patterns looks like as below :




    1. followed by "underscore"

    2. followed by "bar|test" (bar or test)

    3. followed by "underscore"

    4. followed by alhpnum string with dot at end (like foo10.1.1.1)

    5. followed by one or zero occurences of same alhpnum string (like foo10.1.1.1)

    6. followed by dot

    7. followed by 1-10 characters, containing at least one digit and one letter and underscore (e.g. TEST_test, TEST_test2, TEST1_test2)

    8. ends with .txt

    Thanks in advance

TkDND with PAR::Packer
3 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 22, 2017 at 23:29

    Hello Monks, I'm having a problem getting a script with a Tkx and TkDND front end to work using pp. I want to distribute the script to some of my colleagues at work and can't. pp builds and I can distribute a program and it runs but doesn't display the main window. I did find one work around but it's not practical. Putting the TkDND folder into System\Library\Tcl\8.5\TkDND makes the main window visible on other computers running the package. But alas our work computers don't allow folders to be copied to System with out admin login in. Not about to do this for the 20 computers I want to install on. I found the following on line:

    use File::Spec::Functions; BEGIN { if (exists $ENV{PAR_PROGNAME}) { use Config (); $ENV{PERL_TCL_DL_PATH} = catfile( $ENV{PAR_TEMP}, 'tkkit.' . $Config::Config{dlext}, ); } }

    and added it to my script but it looks to be a windows fix that did nothing for me. I also tried to force the TkDND piece into PAR PACKER using:

     pp -o sf -z 9 -f PodStrip -M utf8 -M Tk::Text -l /System/Library/Tcl/8.4/tkdnd2.8/libtkdnd2.8.dylib -l  /System/Library/Tcl/8.4/tkdnd2.8/pkgIndex.tcl -l  /System/Library/Tcl/8.4/tkdnd2.8/tkdnd_compat.tcl -l /System/Library/Tcl/8.4/tkdnd2.8/tkdnd_generic.tcl -l  /System/Library/Tcl/8.4/tkdnd2.8/tkdnd_macosx.tcl -l  /System/Library/Tcl/8.4/tkdnd2.8/tkdnd_unix.tcl -l  /System/Library/Tcl/8.4/tkdnd2.8/tkdnd_windows.tcl -l  /System/Library/Tcl/8.4/tkdnd2.8/tkdnd.tcl

    The package gets bigger but still doesn't run. I've wasted two days and am looking for the wisdom of the monks for help. Thanks in advance for helping me to solve this and get some sleep.

Unlink+Open: File creation timestamp problem
1 direct reply — Read more / Contribute
by Rantanplan
on Feb 22, 2017 at 20:21


    I am having a very strange problem with the combination of unlink and open:

    my $myfile1 = 'D:\mypath\myfile1'; my $myfile2 = 'D:\mypath\myfile2'; use strict; use warnings; unlink $myfile1; my $myhandle; unlink $myfile2; open $myhandle, '>', $myfile2; close $myhandle;

    As expected, myfile1 gets deleted.

    As expected, myfile2 gets erased. It contains 0 Bytes.

    However, the creation date and time of myfile2 does not get set to the time the script runs. The new myfile2 has the creation date and time of the old myfile2.

    Why is this so?? And what can I do that the open command sets the creation date as expected to the time at which the command is executed at?

Handle DBI connection lost
2 direct replies — Read more / Contribute
by bash
on Feb 22, 2017 at 14:17
    I have script that is connected to PostgreSQL and run 24x7.
    Some times we need to restart PostgreSQL or some connection problem can occur, so script is needed to reconnect on this situations.

    There are two ways:
    1) check connection before every statement execution
    2) check connection on errors

    I decided to go by way 2, because I don't want to add extra overhead by calling ping() every time we execute statement
    So my problem is how to be sure that DBI error is about "connection".
    Here is my prototype based on 'Active' attribute and on state() method.
    eval { $dbh->do(...); }; if (my $e = $@) { if ($dbh->{Active} != 1 || $dbh->state =~ /S8\d{3}/){ # reconnect and retry } else { # not connection related error } }

    Is it correct way?
    And how to emulate connection lost for testing (maybe fork and close handler from child)?
shared scalar freed early
4 direct replies — Read more / Contribute
by chris212
on Feb 22, 2017 at 11:11

    I have a script that uses threading. It has a shared variable ($ret) that is used to indicate if an error has occurred. All the threads check that variable so that work will stop in the event of a fatal error.

    Unfortunately I cannot post the script. I'm not able to create a test script to replicate the issue since it is very intermittent and only seems to happen with long runs (over 4 hours). It doesn't use much memory, so it doesn't seem to be a memory leak.

    Basically the script will start a new thread to handle writing output, and the main thread will start possibly millions of threads to process 500 records each as they are read using a semaphore to limit the number of concurrent threads. All threads check the value of the shared scalar. They can all modify the value, but didn't when these crashes occur.

    >> /polaris_stg_root/dev/app/smartload/components/correctaddress_debug +/ $Devel::Trace::TRACE = 0; SV = PVMG(0x10a6350) at 0xf68f68 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0xd5fb30 MG_VIRTUAL = 0x7f361a0c9320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0xee8f08 "" SV = PVMG(0x114ec30) at 0x114d558 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0x1150a70 MG_VIRTUAL = 0x7f361a0c9320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0xee8f08 "" Attempt to free unreferenced scalar: SV 0xee8f08, Perl interpreter: 0x +ee6410. >> /polaris_stg_root/dev/app/smartload/components/correctaddress_debug +/ exit($ret) if($ret == -1); # already fa +iled, don't compare counts or print stats panic: attempt to copy freed scalar ee8f08 to f68f68 at /polaris_stg_r +oot/dev/app/smartload/components/correctaddress_debug/correctaddress_ line 884. Attempt to free unreferenced scalar: SV 0xee8f68, Perl interpreter: 0x +ee6410. Attempt to free unreferenced scalar: SV 0xee8f08, Perl interpreter: 0x +ee6410.

    One dump is the $ret variable from before the output thread returns. The other is from the main thread after all the input is read. I should have dumped it after the output thread is joined and will if I can make it crash again. The main thread does not have any references to $ret between dumping it and line 884.

split string using optimized code or perl one liner
5 direct replies — Read more / Contribute
by madtoperl
on Feb 22, 2017 at 08:44
    Hi Monks,
    I am trying to reverse a string based on dot symbol and print the result. Below code is working fine. Do we have more efficient way of reducing the lines of code. Probably an one liner, please share your thoughts/a better version of code..
    #!/usr/bin/perl use strict; use warnings; my $string = ''; my @array = (); @array = split /\./, $string; @array = reverse @array; print "Reversed data ==> @array\n";
Perl modules that I can use for Multithreading
6 direct replies — Read more / Contribute
by elpis
on Feb 22, 2017 at 08:12

    I want to parallelize a code written in Perl. The code loops through multiple files and calls a subroutine for each file. I also need to share some readonly local data-structures with the subroutine.

    sub process_in_parallel { my $readOnlySchema = foo(); foreach my $file (@files) { validate_the_file($file,$readOnlySchema); } }

    I am pretty new to perl programming and hence need a lot of advice here. What are the perl modules that the perl monks can recommend for this scenario?

    I tried some of the following:

    - threads : The problem with this is managing the threads. Is there an efficient thread Manager or thread pool library that can help me with this? I am also not sure if I can share the readOnly object easily.

    -Parallel::ForkManager : The problem with this is that it forks processes rather than threads and is increasing the time of execution in my case.

    Can you please suggest other libraries also?

    I have the same question posted here also :

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
    [Corion]: choroba: Yeah - that's why the "old" airports of Berlin were cool, as they'd been built before ;)

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (10)
    As of 2017-02-23 12:26 GMT
    Find Nodes?
      Voting Booth?
      Before electricity was invented, what was the Electric Eel called?

      Results (346 votes). Check out past polls.