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
Why doesn't 'system' return '-1' when a program fails to start on Windows?
2 direct replies — Read more / Contribute
by jkahrman
on Feb 23, 2017 at 19:49
    My reading of 'perldoc -f system', 'perldoc -f exec', 'perldoc perlport' (system, exec, and wait sections), and 'perldoc perlvar' ($? section) lead me to expect a '-1' error code from system when the program to launch is not found (fails to launch).

    The following returns -1 on Unix,

    DB<5> x (system 'jxdDoesNotExist'), $?, $! Can't exec "jxdDoesNotExist": No such file or directory at 0 '-1' 1 '-1' 2 'No such file or directory'
    As does this:
    DB<7> x (system {'jxdDoesNotExist'} 'jxdDoesNotExist'), $?, $! Can't exec "jxdDoesNotExist": No such file or directory at 0 '-1' 1 '-1' 2 'No such file or directory'
    But on Windows...
    DB<3> x (system {'jxdDoesNotExist'} 'jxdDoesNotExist'), $?, $!, $^E 0 65280 1 65280 2 'No such file or directory' 3 'The system cannot find the file specified' DB<4> x (system 'jxdDoesNotExist'), $?, $!, $^E 'jxdDoesNotExist' is not recognized as an internal or external command +, operable program or batch file. 0 256 1 256 2 'No such file or directory' 3 'Access is denied'
    The second Windows 'system' call makes some sense (the error code is from cmd.exe), based on the following snippet from 'perldoc -f system':
                ... On Windows, only the "system PROGRAM
                LIST" syntax will reliably avoid using the shell; "system LIST",
                even with more than one element, will fall back to the shell if
                the first spawn fails.
    
    and from 'perlport'
        exec    "exec LIST" without the use of indirect object syntax ("exec
                PROGRAM LIST") may fall back to trying the shell if the first
                spawn() fails. (Win32)
    
    combined with this snippet from 'perldoc -f system':
                Like "exec", "system" allows you to lie to a program about its
                name if you use the "system PROGRAM LIST" syntax. Again, see
                "exec".
    
    But the 65280 code from the Windows "system PROGRAM LIST" syntax perplexes me. If perl knows the command failed to launch, shouldn't system return '-1' (as documented!!!) ?

    See also rt://46333 and rt://95308, which led me to this question

WWW-Mechanize-Firefox install issue
2 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 23, 2017 at 19:13

    Hi,
    I am trying to install the WWW-Mechanize-Firefox
    module and it is failing. Not sure why. Any help is
    greatly appreciated. This is being installed on
    windows. Here is the install text


How do you structure and run module test code?
7 direct replies — Read more / Contribute
by nysus
on Feb 23, 2017 at 08:39

    I'd love to get some general pointers from pros on how to efficiently create tests for modules.

    Using the Module::Starter::PBP module, I notice that the t directory has the following test files by default: 00.load.t,  perlcritic.t,  pod-coverage.t,  pod.t.

    Some of the questions I'm trying to answer are: When should I create a new .t file? How should I group my tests into the different .t files? What's best practice for naming the .t files?

    Also, I'd like to be able to run the tests as efficiently as possible from vim, my tool of choice. Right now, I'm using Damian Conway's vim configuration which has a short cut for running make on a module. It runs all the tests it finds. I imagine this could slow things down quite a bit, however, if I'm just interested in running a few of the test files. How are the pros running individual test files quickly and efficiently with vim?

    Thanks!

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

What basic things should I know about versioning modules?
6 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.

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 :

    foo_bar_foo10.1.1.1.TEST.txt foo_test_foo10.1.1.1.foo10.1.1.1.TEST_test.txt

    "foo"

    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
4 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 sfh2a_tkx.pl

    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

    Hello,

    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
5 direct replies — Read more / Contribute
by bash
on Feb 22, 2017 at 14:17
    Hello,
    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 =~ /08\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)?

    update 2017-02-23
    I emulated connection lost via fork:
    unless (fork()) { $dbh->{InactiveDestroy} = 0; exit; } wait();
    My observation shows that:
    $dbh->{Active} == 1 even if dbh return "DBD::Pg::db selectrow_array failed: no connection to the server".
    $dbh->{Active} == '' only if you explicitly call $dbh->disconnect;
    But $dbh->state works like expected.

    Still interesting in any production experience with robust handling for connection lost.

    PS. To be sure that ping is really slow down I performed simple test
    sub query { $dbh->selectrow_array(q{ SELECT now() }); } sub ping_and_query { $dbh->selectrow_array(q{ SELECT now() }) if $dbh->ping; } cmpthese(-2, { 'query' => \&query, 'ping+query' => \&ping_and_query, }); --- Rate ping+query query ping+query 6349/s -- -39% query 10327/s 63% --
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 +/correctaddress_debug.pl:11: $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 +/correctaddress_debug.pl:884: 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_ +debug.pl 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.

    UPDATE

    I got a dump after the output thread is joined, and the refcount is still 5. There are no references to $ret until it crashes, so it seems the memory is freed even though the refcount is 5?

    >> /polaris_stg_root/dev/data/QAS_TEST/correctaddress_debug.pl:11: $De +vel::Trace::TRACE = 0; Main thread before output thread finishes: SV = PVMG(0x19555c0) at 0x1822318 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0x190b330 MG_VIRTUAL = 0x7fe80f9de320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0x17a7b68 "" Output thread before returning: SV = PVMG(0x19f2730) at 0x1a3d280 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0x1a3e2e0 MG_VIRTUAL = 0x7fe80f9de320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0x17a7b68 "" Main thread after output thread finishes: SV = PVMG(0x19555c0) at 0x1822318 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0x190b330 MG_VIRTUAL = 0x7fe80f9de320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0x17a7b68 "" >> /polaris_stg_root/dev/data/QAS_TEST/correctaddress_debug.pl:884: ex +it($ret) if($ret == -1); # already failed, don't compare counts or pr +int stats panic: attempt to copy freed scalar 17a7b68 to 1822318 at /polaris_stg +_root/dev/data/QAS_TEST/correctaddress_debug.pl line 884. Attempt to free unreferenced scalar: SV 0x17a7b68, Perl interpreter: 0 +x1775870.

Add your question
Title:
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.