Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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
Passing hashes to sub
1 direct reply — Read more / Contribute
by PSP
on Jul 22, 2017 at 04:28
    my $loopCnt=0; my %prevsidstat=%{getSessStat()}; while (1) { debug("Sleeping for $ENV{SESSNAP_INTRVL}...\n"); sleep $ENV{SESSNAP_INTRVL}; my %cursidstat=%{getSessStat()}; if ((keys %cursidstat) != 0 ) { my $ComResult={sidStatComp(%prevsidstat,%cursidstat)}; my %prevsidstat=%cursidstat; } last; }

    Here I am getting hash reference when %{getSessStat()} is executed. Now I want to pass two hash references to sub to process them further, however just printing them in sub getting following error. Appreciate in advance for help. Odd number of elements in anonymous hash at line 65, <SQLPLUS> line 20.

    Sub code:
    #***************************************************** sub sidStatComp { #***************************************************** my (%prevsidstat,%cursidstat)=@_; print "Previous\n"; for my $instsid (keys %prevsidstat) { print "$instsid => $prevsidstat{$instsid}{SQLEXECINFO}\n"; } print "Current\n"; for my $instsid1 (keys %cursidstat) { print "$instsid1 => $cursidstat{$instsid1}{SQLEXECINFO}\n"; } return 1; }
open and read text file
4 direct replies — Read more / Contribute
by hchana
on Jul 21, 2017 at 16:01

    I am trying to open and print the contents of a text file I've named 'Text1.txt' which is in my perl folder. I'm running the code below using Komodo on my Mac Book Pro but it is not working, I keep getting the message "/bin/sh: command not found". I'm new to Perl as they come, so could my error be explained in a simple manner. Thanks in advance.?

    #!/user/bin/perl open(FILE, "Text1.txt"); while(<FILE>){ print "I saw $_ in Text1.txt\n"; } close FILE;
Get TAP output when compiling Perl and running "make test"
1 direct reply — Read more / Contribute
by Rjevski
on Jul 21, 2017 at 15:53


    I am currently working on automating our Perl builds and I'd like to know how do I get the TAP output when I run make test so that the continuous integration system can pick up failures and display them nicely (manually going through a 5k line build log is not fun).

    I've already tried setting PERL_TEST_HARNESS_DUMP_TAP as well as HARNESS_OPTIONS but those have no effect when running through make for some reason.

    Steps to reproduce:

    curl +2 | tar -jxC /root cd /root/perl* ./Configure -des -Dprefix=/opt/perl-5.20.3 make -j $(nproc) make test

    I am running Perl 5.20.3 on Debian 9.0 amd64.


Tk::TableMatrix won't build on Strawberry 5.26.0
3 direct replies — Read more / Contribute
by aplonis
on Jul 21, 2017 at 09:17

    Running Strawberry version 5.26.0 on Win 10 Pro. And, alas, Tk::TableMatrix will not build. I get this error...

    Checking dependencies from MYMETA.json ... Checking if you have ExtUtils::MakeMaker 0 ... Yes (7.24) Checking if you have Tk 800.022 ... Yes (804.033) Building and testing Tk-TableMatrix-1.23 cp blib\lib\Tk\ AutoSplitting blib\lib\Tk\ (blib\lib\auto\Tk\TableMatrix +) cp TableMatrix/ blib\lib\Tk\TableMatrix\ cp TableMatrix/ blib\lib\Tk\TableMatrix\Spreadsh cd pTk && gmake gmake[1]: Entering directory 'C:/Users/gan/.cpanm/work/1500635987.1034 +8/Tk-TableMatrix-1.23/pTk' gmake[1]: *** No rule to make target '..\blib\arch\Tk\pTk\.exists', ne +eded by 'config'. Stop. gmake[1]: Leaving directory 'C:/Users/gan/.cpanm/work/1500635987.10348 +/Tk-TableMatrix-1.23/pTk' gmake: *** [Makefile:1171: pTk/libpTk.a] Error 2 -> FAIL Installing Tk::TableMatrix failed. See C:\Users\gan\.cpanm\wor +k\1500635987.10348\build.log for details. Retry with --force to force + install it.

    And --force also failed. Anyone know how to work through this?

blank pdf generated using PDF::API2
1 direct reply — Read more / Contribute
by lennelei
on Jul 21, 2017 at 02:46

    Hi all,

    I've a very small Perl script that splits PDF files bigger than 100 pages :

    use strict; use warnings; use PDF::API2; #...some code here... #for testing purpose: my $file='path_to_some.pdf'; # my $oldpdf = PDF::API2->open($file); if ($oldpdf->pages > 100) { my $newpdf = PDF::API2->new; printf " (%d pages)\n", $oldpdf->pages; for my $page_nb (1..10) { $newpdf->importpage($oldpdf, $page_nb); } $newpdf->saveas("_$file"); }

    I'm running this on Windows (Windows 7 for my desktop, Windows 2008/2012 for the servers) with a Strawberry Perl 5.14 and PDF::API2 module installed using cpan.bat

    It's working and used for weeks now without trouble until this week. With a pdf received a few days ago, the script output is a 100 blank pages document.

    I tried using the alternative with importPageIntoForm by snoopy from with the same result.

    I also tried another tool (sejda) and the pages are correctly extracted so it's probably an issue with PDF::API2 or a misconfiguration but I don't know what to add/change in the script.

    FYI, the sejda command line:

    sejda-console.bat extractpages -f SOURCE.PDF -o TARGET.PDF -s 1-100

    Any idea/alternative I could try? I'd like to keep the Perl as this is only a small part of a bigger script, but if I have no other option, I'll use sejda for the split.

    Unfortunately, I cannot provide the PDF :(

    Thank you

    Edit : I just found and tried with CAM::PDF using the following code and it's working!

    For what I've seen, the difference between both code is that PDF::API2->import_page function tries to copy the content of the pages where CAM::PDF->extractPages function removes the pages outside the given range. Maybe there is a similar method in PDF::API2 but I couldn't find it yet?

    use strict; use warnings; use CAM::PDF; #...some code here... #for testing purpose: my $file='path_to_some.pdf'; # my $oldpdf = CAM::PDF->new($file) or die "$CAM::PDF::errstr\n"; if ($oldpdf->numPages() > 100) { printf " (%d pages)\n", $oldpdf->numPages(); $oldpdf->extractPages(1..100); $oldpdf->cleanoutput("_$file"); }
extract last 1 min log and find a string
4 direct replies — Read more / Contribute
by ytjPerl
on Jul 20, 2017 at 12:05

    Hi folks, I am writing a script to extract last 1 min log and search a string. I know I could use module like File::ReadBackwards, unfortunately, I do not have internet access on my VM and copying module manually with failure. So I am seeking for a way to avoid using external modules. Thanks!

where the module file *.pm located
5 direct replies — Read more / Contribute
by ytjPerl
on Jul 20, 2017 at 11:57
    Hi folks, I am facing an issue that I have no internet access on my VM, so when I running my perl script containing external modules, I have to manually copy *.pm from local machine to VM. For instance, I need to use File::Readbackwards. I have that locally at C:\Perl64\site\lib\File\, but in my VM, I only have directory perl\site\lib(no sub-directory File), so I manually copied there, \Perl\lib\ as well. When I execute my script, it is still saying cannot locate File::Readbackwards. Please help! Thanks
Creating a perl delete statement with MongoDB
3 direct replies — Read more / Contribute
by e0s
on Jul 20, 2017 at 08:44

    I am having difficulty creating a perl MongoDB delete statement that will select from EITHER one table or another, depending on the TYPE of input from the user. Let's say the databases are $FRUITS and each fruit is categorized into tables either by their 'fruit_names' or unique 'fruit_ids'

    Let's say the user wants to delete an entry from the fruit_names table, how do I configure my perl code so that it can differentiate between one input versus another?

    I've searched and found some MongoDB documentation on delete_one and delete_many but I haven't been able to figure out how I would create a delete_one statement that would pull from either one table or the other, depending on the user's input.

    I am a novice with perl still and I've never used MongoDB before, i've been really stuck on this for a while so any help would be greatly, greatly appreciated.

sysread() is deprecated on :utf8 handles
4 direct replies — Read more / Contribute
by mje
on Jul 20, 2017 at 07:02

    I have just tested some code on Perl 5.24.2 which was running happily on 5.16.0 and got a warning of "sysread() is deprecated on :utf8 handles". Now I'm wondering how to fix it and I'm a little confused by the discussion at RFC remove strange behaviour of sysread()/syswrite() on UTF-8 streams.

    The file handle in question is a unidirectional pipe created before a fork and the parent is writing XML down the pipe (which has :encoding(UTF-8)) layer and the child is using sysread on the read end of the pipe which also has encoding(UTF-8). The child then sends the data down a socket which also has encoding(UTF-8) layer on it.

    I'm slightly confused by the :utf8 and encoding("UTF-8"). I'm doing the latter but the RFC seems to indicate there is a difference between :utf8 and encoding("UTF-8") and that sysread works with :utf8 but not encoding(something else)? So why do I get the deprecated warning?

    Is the solution simply to leave the encoding on the write end of the pipe which uses print to send messages down the pipe, but leave it off the read end of the pipe and decode the read message as UTF8 before sending it down the onwards socket?

    CORRECTION: the connection between the parent and child is a socket although I don't think it makes any difference

    UPDATE1: It appears I can't just use :raw on the socket read end and decode to UTF-8 as I might have read part of a UTF-8 sequence. Still searching for an answer.

    UPDATE2: The code was originally using readline but that was switched to sysread because the code already uses IO::Select and you cannot mix buffered reading with IO::Select

    UPDATE3: This link explains why you cannot use buffered IO and IO::Select

error: Use of uninitialized value in print at line 31, <$readhandle> line 4.
2 direct replies — Read more / Contribute
by mmazlan67
on Jul 20, 2017 at 04:55

    Hi, i am working on a simple perl script to grab 3 variables based on a textfile. I am using regex. I tried to catch the word after the match string for the first variable, it works fine. But when i tried on the second variables, it appears error: Use of uninitialized value in print at line 31, <$readhandle> line 4. I tried to put no warnings 'uninitialized';, but still the second output did not appear as expected. From the output file, only the name which is blocka appear whereas the path did not. Below are my code and the text file used.

    #! /tools/perl/5.8.8/linux/bin/perl use strict; use warnings; print "Enter the name of the file to read: "; my $filetoread = <STDIN>; chomp($filetoread); print "Enter the name of the file to write: "; my $filetowrite = <STDIN>; chomp($filetowrite); open my $readhandle, "<", $filetoread or die "Unable to read '$filetor +ead'"; open my $writehandle, ">", $filetowrite or die "Unable to write '$file +towrite'"; my $name; my $path; while (<$readhandle>) { if ($_=~ m/^Name=(\S+)/) { $name = $1; print $writehandle $name; } if ($_=~ m/^Path=(\S+)/) { $path = $2; print $writehandle $path; } } close $readhandle; close $writehandle;

    This is the textfile used

    Name= blocka Path=/main/sub/a/bg/adee/swarfrw/ewqsx/fewaxr/akeil/awedr/date/file

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 taking refuge in the Monastery: (4)
    As of 2017-07-22 14:19 GMT
    Find Nodes?
      Voting Booth?
      I came, I saw, I ...

      Results (340 votes). Check out past polls.