Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
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
5 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
do something if hash keys match regex
4 direct replies — Read more / Contribute
by anandk
on Jul 20, 2017 at 04:03

    O wise monks, greetings. I'm trying to replace the values of a hash based on the keys matching a regex. Is there a more compact, one liner way of doing it, without using foreach and the braces ? Most ideally I would want something like this:

    $hash{/abc_(.*)_ghi/} = boom_$1
    That should reassign all the 4 key-value pairs in the hash.

    My current code:
    use Data::Dumper; my %hash = ( xxx_text10_yyy => 1, xxx_text11_yyy => 1, mmm_text12_nnn => 1, mmm_text13_nnn => 1, abc_text6_ghi => 1, abc_text7_ghi => 1, abc_text8_ghi => 1, abc_text9_ghi => 1 ); print Dumper(\%hash); foreach my $key (keys %hash) { $hash{$key} = "boom_$1" if ($key =~ /abc_(.*)_ghi/); $hash{$key} = "doom_$1" if ($key =~ /mmm_(.*)_nnn/); } print Dumper(\%hash);
Value of a key in hash in hex
2 direct replies — Read more / Contribute
by PSP
on Jul 20, 2017 at 03:12

    print $sqlexecinfo; shows 1677721807/20/2017 00:03:38 while print $sidstat{$instsid}; shows HASH(0x2017b98) why is the difference?

    open(SQLPLUS,"$CMD|") or die "Could not start SQLPLUS"; while (<SQLPLUS>) { chomp; my ($instsid,$sqlexecinfo)=split '=>'; $sidstat{$instsid}{SQLEXECINFO}=$sqlexecinfo; print $sqlexecinfo; print $sidstat{$instsid}; } debug(scalar(keys %sidstat)." session state recorded.\n"); return \%sidstat;
How to replace empty values with value in previous array
4 direct replies — Read more / Contribute
by Fshah
on Jul 20, 2017 at 02:29
    hello monks,

    I have an array which is joined by ';' In the next loop I get empty values I want to find the empty values and replace them by the one's obtained in the previous loop

    @first_loop=(1668;1733;60;32;3173;0;2517;58221;55764;0;0;0;0;Td;30720; +1;9;25;0;2; Carry 0); @next_loop=(1671;1661;0;0;;;0;0;0;0;;;;;;1;9;25;0;2; Carry 1);

    I want the output in the present array to not have empty values instead have previous value

    thank you for the help in advance edit: I have an array of such data in each line I want to perform this operation on entire array
    1668;1733;60;32;3173;0;2517;58221;55764;0;0;0;0;Td;30720;1;9;25;0;2; C +arry 0 1671;1661;0;0;;;0;0;0;0;;;;;;1;9;25;0;2; Carry 1 1671;1661;;0;;;0;0;0;0;;;;;;1;9;;0;2; Carry 2
DNA Pattern Matching
1 direct reply — Read more / Contribute
by Speed_Freak
on Jul 19, 2017 at 17:50

    The basics: I'm new to this, and I've gotten myself so far down a rabbit hole that my mind is going blank. I have searched for the basics on pattern matching, phoned a friend, and tried venturing into the BioPerl world, but can't wrap my head around the solutions involving C, or using Hex. (Also, the BioPerl How To's seem to be broken?)

    The layout: I have a database with multiple tables that I am working with. One of the tables contains a list of pairs (pairs of consensus sequences and their mismatch.) These pairs correspond to ID's in another table. And those ID's correspond to sequences in a third table.

    So far, (with the extensive help of a colleague) I have a script that looks through the list of pairs and finds both sequences by their id and their corresponding mismatches in the ID table (four sequence id's in total.) The script then matches those ID's with their sequences in the sequence table. I have also gone as far as to identify the central base for each sequence. (Each sequence is 25 bases in length, and I have filtered the results to only find sequences that are exactly 25 bases long.)

    The problem: For each sequence, there is only one potential mismatch listed in the database. I need to find out if the other two sequences exist in the database. If my (shortened for simplicity) sequence was ACTGCCT, I would call this my pm_seq. It's identified mismatch (mm_seq) might be ACTACCT. But the sequence table itself may contain up to two more mismatches. (ACTCCCT and ACTTCCT.) I need to search each of my identified pm_id's seqences against the entire sequence list and identify the id's of all three possible mismatches. One attempt at this has been to set a pattern for the sequence like so: my $pattern = ($ppi_pm_seq =~ /(CAGT{12})(CAGT{1})(CAGT{12})/). But I am lost when it comes to searching against my sequence table and identifying sequence ID's that have a sequence that matches $1 AND $3.

    Ultimately I am looking to generate an output that contains each sequence, and up to three mismatches that differ at the central base for a large list of target sequences. The code below is later in the script, but it fetches the sequences for the identified pairs. (one pm and one mm per pair.) But I still need to search the entire sequence set for the other two possible mm's. (I think it will be easier to search for all 3 possible mm's based on the pm sequence rather than identify the known mm and search for the other two.)

    #perl 5.8.8 #code to get data from database not included my $ppiquery1 = "select sequence from seq_table where seq_id = $ppi_pm +_id AND LENGTH(seq) = 25"; my $ppiquery2 = "select sequence from seq_table where seq_id = $ppi_mm +_id AND LENGTH(seq) = 25"; my $mpiquery1 = "select sequence from seq_table where seq_id = $mpi_pm +_id AND LENGTH(seq) = 25"; my $mpiquery2 = "select sequence from seq_table where seq_id = $mpi_mm +_id AND LENGTH(seq) = 25"; my @row3 = $dbh->selectrow_array($ppiquery1); unless (@row3) { die "No seq for $ppi_pm_id?\n"; } my @row4 = $dbh->selectrow_array($ppiquery2); unless (@row4) { die "No seq for $ppi_mm_id?\n"; } my @row5 = $dbh->selectrow_array($mpiquery1); unless (@row5) { die "No seq for $mpi_pm_id?\n"; } my @row6 = $dbh->selectrow_array($mpiquery2); unless (@row6) { die "No seq for $mpi_mm_id?\n"; } my ($ppi_pm_seq) = @row3; my ($ppi_mm_seq) = @row4; my ($mpi_pm_seq) = @row5; my ($mpi_mm_seq) = @row6;

    Beyond here, I've attempted to set the pattern with ($ppi_pm_seq =~ /(CAGT{12})(CAGT{1})(CAGT{12})/) and then use a series of if/elsif for each of the four possible combinations of $2 (A, C, T, G) to store the seq_id's for each no avail. Any guidance would be greatly appreciated. I'd like to stay completely in perl if possible, mostly because I barely grasp this let alone something new!

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 chanting in the Monastery: (4)
    As of 2017-07-26 04:45 GMT
    Find Nodes?
      Voting Booth?
      I came, I saw, I ...

      Results (383 votes). Check out past polls.