Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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
shared scalar freed early
2 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
5 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?

Invalid or bad combination of options ('key_path') [RESOLVED]
No replies — Read more | Post response
by thanos1983
on Feb 22, 2017 at 05:42

    Hello Monks,

    I am using the Net::OpenSSH::Parallel module to create parallel ssh connections. Note that the module is using the Net::OpenSSH to create ssh sessions. So far I have successfully used in the past for multiple scripts by using password => $password in public the username and password to automate ssh connections

    As stated on the documentation: Note that using password authentication in automated scripts is a very bad idea. When possible, you should use public key authentication instead. it is much safer to use ssh key authentication.

    I am able to ssh to my localhost through the typical way

    ssh -p 22 user@host</p> without password as I have set up the ssh key +authentication.</p> <p>When I am using the module <code>Net::OpenSSH::Parallel
    to automate the connections without password but with ssh-key on debug mode -1 $Net::OpenSSH::Parallel::debug = -1;, I keep getting this error:

    I wrote a small script to demonstrate the problem that I am having:

    If someone searches for the same error online he will find multiple links for similar errors but none of them providing the solution to the problem, example links (Re: net::openssh error in CGI: Invalid or bad combination of options ('key_path')). The only useful link that I found is (how to use Net::SSH::Perl with public keys?).

    The error is coming from the module Net::OpenSSH (github link) at line 69.

    Anyone ever encounter this problem, and if so does anyone know how to overcome the problem?

    Thanks in advance for your time and effort reading and replying to my question.

    Update: ok I just found the solution to my problem (stupid of me). I just thought that might provide some help to someone in future that is the only reason that I am posting this.

    Solution: simply since you obviously using ssh-key what is the purpose to use password. Simply comment out the password input when you add another user or simply do not provide it.

    End of update

    Seeking for Perl wisdom...on the process of learning...not there...yet!
How to search keywords In sequence or according to timing from a text files In Perl ?
3 direct replies — Read more / Contribute
by rockyurock
on Feb 22, 2017 at 03:28
    In my below program , I want to search all the keywords provided In text file named - Success_MessageFlow.txt from a list of file named with *main_log and I am getting the output In each different file named Message_Flow.$l.txt according to the no of files present In a directory My keyword file sample contains :
    Hello Sample Hello Sample#Step-9:Hello# Bye Sample Bye Sample#Step-3:Bye#
    My Input file content sample Is :
    12-26 13:35:06.057570 236 578 D Hello Sample Hello Sample 12-26 13:35:05.623529 236 578 D Bye Sample Bye Sample
    My output generated Is :
    *******#Step-9:Hello Sample Hello Sample#******* Hello Sample Hello Sample in file main_log, line 1:12-26 13:35:06.0575 +70 *******#Step-3:Bye Sample Bye Sample#******* Bye Sample Bye Sample in file main_log, line 1:12-26 13:35:05.623529
    Issue : Though both Step-9 and Step-3 are searched correctly but I want searching to be In order of timing or Step-3 should be printed In my output file before Step-9 as step-3 keyword occurrence was before (12:35:05) than the Step-9 (12:35:06) Can anyone please help to suggest any modification in my program that can help ? My Program :
    printf "\n Starting success message flow\n\n"; # Opening Keyword File here open( my $kw, '<', 'Success_MessageFlow.txt') or die $!; my @keywords = <$kw>; sort @keywords; chomp(@keywords); # remove newlines at the end of keywords # post-processing your keywords file for adding comments my $kwhashref = { map { /^(.*?)(#.*?#)*$/; defined($2) ? ($1 => $2) : ( $1 => undef ) } @keywords }; # get list of files in current directory my @files = <main_log*>; my $l = 0; # loop over each file to search keywords in foreach my $file (@files) { print "\n Processing with file $file \n"; open(my $fh, '<', $file) or die $!; my @content = <$fh>; sort @content; close($fh); $l++; open my $out_file, '>', "Message_Flow.$l.txt" or die "$!"; foreach my $kw (keys %$kwhashref) { my $search = quotemeta($kw); # otherwise keyword is used as re +gex, not literally foreach (@content) { # go through every line for this keyword if (/$search/) { printf $out_file "\n*******$kwhashref->{$kw}** +*****\n"."\n" if defined($kwhashref->{$kw}); printf $out_file '%s in file %s, line %d:%s'.$ +/, $kw, $file, $l, $_; } } } } printf "Check the output generated In file IMS_Reg_Message_Flow.txt\n" +;
Perl5 Perl6 forking
6 direct replies — Read more / Contribute
by reverendmred
on Feb 21, 2017 at 21:22
    I'm returning to Perl after an absence of a couple years to pick up a lot of code I left in beta. Now, Perl6 has changed all the rules.

    Languages need to evolve, but when C evolved into different varieties, ultimately, it was called a different language. The same seems to be true of Perl.

    Is there any way to ensure a switch in Perl 6 so you can choose between languages? Once long ago, we used the top of the file to point the script at the executable. Can we have Perl6 provide something like this to preserve legacy code. It would also be nice for CPAN modules to provide a fork so we can keep alive old stable code.

    I know you must now be thinking 'oh my god, you can't have two languages in one application' but actually, you can. When last left Perl behind and returned to it in the early 00s, it had switched from a procedural scripting language to a very fake OO language. I say very fake, because I worked on a bioinformatics project coming from a pure 00 environment and in my nw position I got criticized for crappy perl code and not doing good OO -- which only showed me my colleagues knew nothing about Perls history and had absolutely no idea what OO really was.

    The point is, if you had good OO like Perl5 code, there should be no problem running it alongside new Perl6 code as long as you treat them as separate languages and you have a fork of the legacy CPAN code. What you need to make sure is that the interfaces between your Perl5 and Perl6 code are stable and work. (Of course, I hate to say, but that old bioinformatics project was not good OO and violated every aspect of encapsulation).

    So, is there a way we can fork projects and modules on CPAN? This would be really, really useful.


"make test" catches a C undefined symbol, but "prove" doesn't
4 direct replies — Read more / Contribute
by stevieb
on Feb 21, 2017 at 20:29

    I'm having a head-scratching moment here. Precursor: this isn't a failure due to anything missing; I'm failing to understand why one thing succeeds and the other doesnt. I'm finding issues installing some of my modules with cpanm and cpan (XS based modules), then, when I pull down the repo locally and to a perl Makefile.PL and make all is well. Then when I do make test, it fails miserably (I have boldfaced the actual error so you don't have to wade through the other stuff if it isn't necessary):

    spek@sequoia ~/repos/rpi-dac-mcp4922 $ make test PERL_DL_NONLAZY=1 "/home/spek/perl5/perlbrew/perls/perl-5.24.1/bin/per +l" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Har +ness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/00-load.t ....... 1/? Bailout called. Further testing stopped: # Failed test 'use RPi::DAC::MCP4922;' # at t/00-load.t line 10. # Tried to use 'RPi::DAC::MCP4922'.
    # Error: Can't load '/home/spek/perl5/perlbrew/perls/perl-5.24.1/ +lib/site_perl/5.24.1/x86_64-linux/auto/WiringPi/API/' for modul +e WiringPi::API: /usr/local/lib/ undefined symbol: shm +_open at /home/spek/perl5/perlbrew/perls/perl-5.24.1/lib/5.24.1/XSLoa line 96.
    # at /home/spek/perl5/perlbrew/perls/perl-5.24.1/lib/site_perl/5.24.1 +/x86_64-linux/WiringPi/ line 9. # Compilation failed in require at /home/spek/repos/rpi-dac-mcp4922/bl +ib/lib/RPi/DAC/ line 9. # BEGIN failed--compilation aborted at /home/spek/repos/rpi-dac-mcp492 +2/blib/lib/RPi/DAC/ line 9. # Compilation failed in require at t/00-load.t line 10. # BEGIN failed--compilation aborted at t/00-load.t line 10. Use of uninitialized value $RPi::DAC::MCP4922::VERSION in concatenatio +n (.) or string at t/00-load.t line 13. # Testing RPi::DAC::MCP4922 , Perl 5.024001, /home/spek/perl5/perlbrew +/perls/perl-5.24.1/bin/perl # Looks like you failed 1 test of 1. FAILED--Further testing stopped. Makefile:1029: recipe for target 'test_dynamic' failed make: *** [test_dynamic] Error 1

    However, prove t/*.t doesn't catch the problem and goes on its merry way:

    spek@sequoia ~/repos/rpi-dac-mcp4922 $ prove t/*.t t/00-load.t ....... 1/? # Testing RPi::DAC::MCP4922 2.36.1, Perl 5.024 +001, /home/spek/perl5/perlbrew/perls/perl-5.24.1/bin/perl t/00-load.t ....... ok t/manifest.t ...... skipped: Author tests not required for installatio +n t/pod-coverage.t .. skipped: Author tests not required for installatio +n t/pod.t ........... skipped: Author tests not required for installatio +n All tests successful. Files=4, Tests=1, 1 wallclock secs ( 0.05 usr 0.00 sys + 0.23 cusr + 0.02 csys = 0.30 CPU) Result: PASS

    I know what the problem is, where the problem is (at least iirc without looking at the code yet), so I just want to know why one fails and the other doesn't.

scraping an ASP page
2 direct replies — Read more / Contribute
by actionphotos
on Feb 21, 2017 at 15:15
    Hello, I am looking for a way to continuously (maybe every 10 minutes), scrape an ASP page to look for schedule changes where it can then notify me of the change. What is confusing me is that all pages show the same URL, so I am not sure how to specify a specific URL, or is it not possible? to see what I mean, go to you can then click on the Mite event, or Presidents day event etc.... once there clicking on the schedule button will show that events schedule. But I noticed that the URL when viewing the schedule is the same no matter which event I am viewing... is there a way to find out a specific URL? or some way to download to a file the schedule every x minutes to look for changes? thanks!! John
Parsing output from a 'system' call into objects?
3 direct replies — Read more / Contribute
by eval142
on Feb 21, 2017 at 14:04

    I have a 'system' call to an app

    my $test = system($test, "list");

    which returns,

    p $test CoID: Type: State: ID: ExampleCo A former 97546 ExampleCo B pending 48541 ExampleCo A ready 75521 ExampleCo B former 50123 ExampleCo A contact 60047 ExampleCo B contact 19425 0

    I want to select from that output-in-a-variable only the records where Type == "A", and populate the data into an array of objects indexed by ID.

    So in effect I want to end up with

    array ID = 97546 CoID = ExampleCo State = former ID = 27377 CoID = ExampleCo State = ready ID = 60047 CoID = ExampleCo State = contact

    I can print out the `$test` output, but I can't manage to break it apart into object keys & values.

    Do I have to save the return into a file, and then parse the file back in?

    That seems wasteful, and I think I should be able to work with the data in the returned variable.

    What's the right way to break that return data into these objects?

Manually Select a Windows Window
2 direct replies — Read more / Contribute
by rd48sec
on Feb 21, 2017 at 11:56

    I have an experimental Perl script that selects a Notepad window and writes some text to it. My code works.

    If I have more than one Notepad window open, I would like to be able to manually select which window to write to. Is there a way to do this?

    #!/PERL5 use strict; use Win32::GuiTest qw(FindWindowLike SetForegroundWindow SendKeys); $ARGV[0] =~ s/\[!20\]//g; #Remove [!20] WinkeySim delays $ARGV[0] =~ m!\((.*)\)!; #Grab everything between ( ) put i +nto $1 my $EEcorr = "{(}".$1."{)}"; #Parentheses (special characte +rs) are added #Write to unit EEPROM #my @window = FindWindowLike(0, "Terminal", ""); my @window = FindWindowLike(0, "Notepad", ""); SetForegroundWindow($window[0]); sleep 1; Win32::GuiTest::SendKeys($EEcorr,20);

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 calm...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (6)
    As of 2017-02-22 17:46 GMT
    Find Nodes?
      Voting Booth?
      Before electricity was invented, what was the Electric Eel called?

      Results (333 votes). Check out past polls.