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
Parsing XML file and keeping the formatting tags
3 direct replies — Read more / Contribute
by corfuitl
on Mar 22, 2018 at 10:50

    Hi PerlMonks

    I have the following bilingual file and would like to extract the source and target nodes preserving its xml elements they might have as well as the line breaks. Could you please help me on that? I have no experience in xml parsing with Perl.

    Here is a sample of my file:

    <trans-unit id="1" maxbytes="14"> <source xml:lang="en-US">Hello <x id=1/> world! How are you?</source> <target xml:lang="ja-JP">Ciao<x id=1/> mondo! Come stai?</target> </trans-unit>

    The expected result should be:

    Hello <x id=1/> world! <lb/> How are you? || Ciao<x id=1/> mondo! <lb/> Come stai?

    Thank you for your time!

File::Temp::tempfile : name versus handle
3 direct replies — Read more / Contribute
by mldvx4
on Mar 22, 2018 at 08:24

    I'm using File::Temp::tempfile to get a name to use for a temporary file to temporary output from a piped command. The book Programming Perl, 4th ed. mentions that it is prefereable to use the file handle that the tempfile() function creates instead of the file name. Is there any way to do that in the context of the code snippet below?

    my ($handle, $output) = tempfile("tidyXXXXXX", DIR => "/run/validator", SUFFIX => ".dat" ); my $tidy = q(/usr/bin/tidy); my @args = ('-quiet', '-xml', '-file', $output); # process web page with tidy open(TIDY, "-|", $tidy, @args, $tempfile) or die("Could not open '$tidy' : $!\n"); close(TIDY);

    What about other improvements to the above?

Passing a structure from C to Perl
1 direct reply — Read more / Contribute
by drpraveena
on Mar 22, 2018 at 07:47

    I have a C program (let's call it A) which calls a perl5 script (B) and it passes some parameters to it. The scope of the script is now changing and I need to pass more parameters to it. And in the future, even more. Instead of passing individual parameters to the script, I would like to pass a structure with all these parameters and keep extending the structure as and when I need to pass more parameters. This will keep the interface between the C code and Perl script constant (with just one parameter, the structure) and also keeps the code clean. How can I do this...? I have been searching the documentation, but unable to find the solution.

    Thanks, Praveen
compute paths in Pascal's triangle (aka Tartaglia's one)
7 direct replies — Read more / Contribute
by Discipulus
on Mar 22, 2018 at 05:03
    Hello wise monks and nuns,

    I want to add a 17th fun experiment to my project: infact there is a properties I have still not shown: the number in a specific tile is also the number of different shortest path from the top tile (no backwards move nor lateral ones).

    I want to show (colorizing them) all distinct paths in sequence and to do it I need a serie of coordinates: given the following structure

    0-0 1-0 1-1 2-0 2-1 2-2 3-0 3-1 3-2 3-3 4-0 4-1 4-2 4-3 4-4 5-0 5-1 5-2 5-3 5-4 5-5

    if the user click the node 3-1 i need to have back:

    0-0 1-0 2-0 3-1 0-0 1-0 2-1 3-1 0-0 1-1 2-1 3-1

    I have asked in the chatterbox some days ago and oiskuu, Eily and Lanx were so kind to suggest various approach, but franckly i was not able to implement a simple way: infact not all combinations are valid: 0-0 1-0 1-1 2-1 3-1 contains the illigal lateral move 1-0 1-1

    Well I can produce all combinations and then throw away solutions with too much moves.. but for sure exists a simpler perlish way.

    Squeezing my brain I only ended with naive method to highlight the area of such valid tiles:

    sub enum_area{ my ($x,$y) = split '-', $_[0]; my $minus = $x - $y; print "$_ " for grep { my ($cx,$cy) = split '-',$_; $cy > $cx ? 0 : ( $cx-$cy < $minus + 1 ? 1 : 0) } glob '{'.(join ',',0..$x).'}-'. '{'.(join ',',0..$y).'}'; }

    thanks in advance


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
poor quality perl code
5 direct replies — Read more / Contribute
by pwagyi
on Mar 22, 2018 at 02:01

    In my team, there are a few programmers who do not follow any coding style guide. I am talking about using ALL CAPS variable/subroutine names(including local variables), overuse of global variables, manually re-inventing stuff (like parsing command line argument,), no design, no concern for DRY (a lot of repetition of code), stringly typed ( way of doing things.

    I've been observing this for some time. I am afraid that it will only get worse. It also got me thinking such kind of code are what gives perl bad reputation as read only language. What should I do? should I just tell them quietly?

Unable to populate the graph
2 direct replies — Read more / Contribute
by Sarat1729
on Mar 22, 2018 at 00:40
    Hi Monks, There has been a requirement I am working on, which is not working. The requirement is to retrieve the data from DB and plot it as a graph and send as a content of mail. However I 1) Successfully retrieved the data 2) Successfully plotted the graph using GD::Graph module and saved it as .png file in a server. 3) Successfully sent the mail by referring the image (using html img tag) and received it. But, couldn't get the image in the mail. In the doubt that mail retrieving the pic from the local machine that I am using to access the mail, I created a dummy pic with the same name as I created in the server and the mail triggered next time is picking that. So, how I can achieve the functionality of getting the pic into mail from the server where I am executing the script instead of the machine where I am accessing the mail. Note: Couldn't paste the code due to the restrictions that I have. Apologies for that.
Making this script process 56,000 lines 5 times faster
4 direct replies — Read more / Contribute
by kris004
on Mar 21, 2018 at 21:31

    So, I decided to learn some Perl earlier today. I had a small script that downloaded and converted a hosts file for use with unbound:

    #!/bin/sh curl +|grep '^0\.0\.0\.0' | awk '{print "local-zone: \""$2"\" redirect\nloc +al-data: \""$2" A\""}' > ads.conf

    Which discards all lines except lines like this:

    And changes it into two lines like this:

    local-zone: "" redirect local-data: " A"

    I poked around this site and read some docs and came up with this:

    #!/usr/bin/perl use strict; use warnings; use LWP::Simple; my $url = " +hosts"; open(my $file, '>', 'ads.conf') or die "wtfile?"; for (split /^/, get ($url)) { if ($_ =~ /0\.0\.0\.0/ and $_ !~ /#/){ $_ =~ s/0\.0\.0\.0 //; $_ =~ s/\n//; print $file "local-zone: \"" . $_ . "\" redirect\nloca +l-data: \"" . $_ . " A\"\n"; } } close $file;

    Turns out the Perl script takes 5 times as long as the shell script, and uses nearly twice the cpu. What mistakes did I make, and is there a way to do this in Perl faster then the shell script can?

    Updated script based on comments:

    #!/usr/bin/perl use strict; use warnings; use LWP::Simple; my $url = " +hosts"; open(my $file, '>', 'ads.conf') or die "wtfile?"; for (split /^/, get ($url)) { if ($_ =~ /^\Q0.0.0.0\E (.*)$/){ print $file "local-zone: \"" . $1 . "\" redirect\nloca +l-data: \"" . $1 . " A\"\n"; }
Print only the matched line when process line by line from a file
5 direct replies — Read more / Contribute
by sreek3502
on Mar 21, 2018 at 15:04

    have the below code, and its input file "fruits.txt" has following values.






    I'm getting the output as below;

    Grapes not in list

    Strawberry not in list

    Grapes not in list

    Strawberry not in list


    Strawberry not in list

    Grapes not in list

    Strawberry not in list

    Grapes not in list

    Strawberry not in list

    Grapes not in list

    Strawberry not in list

    However, i'm actually looking for an output like this, please help!

    Expected result as follows.


    Strawberry not in list

    use strict; use warnings; open (FILE,"fruits.txt"); while (<FILE>) { if (/Grapes/) { print $_; } else { print "Grapes not in list\n";} if (/strawberry/i) { print $_; } else { print "Strawberry not in list\n"; } } close FILE;
What are best practices for Fast CGI nowadays?
4 direct replies — Read more / Contribute
by mldvx4
on Mar 21, 2018 at 11:43

    Where can I look for the current best practices regarding implementing a perl script using Fast CGI? Which module(s) should I look at and which should I avoid? I'm starting to process data from HTML forms using Fast-CGI, but have done quite a lot with slow CGI in the distant past. The perl script will be communicating with the web server via UNIX sockets.

    Also, should the script fork ever? If so, when and what should be done with the UNIX socket to handle the forking?

UTF-8 and PSGI/Starman vs. CGI
3 direct replies — Read more / Contribute
by dsheroh
on Mar 21, 2018 at 11:34
    I have two originally-identical webapp servers, both connecting to the same back end database. When they were first set up, both of them ran the webapp through apache and plain old CGI. Since CGI was being extremely slow (who would've thought?), I changed one of the servers over to nginx/PSGI/starman.

    Now I have triple- or quadruple-encoding (I'm not sure which) issues with UTF-8 characters on the starman box, but UTF-8 works perfectly on the apache box.

    I get the exact same results regardless of whether I talk to nginx or go to the starman server directly, so I'm confident that nginx is not the issue. It's also plainly not the database because both of them are connecting to the same database. The webapp source directory is on the same git branch and commit with no untracked files on either machine, so they should be running identical code and their config files have the same md5sum. As far as I can tell, apache/CGI vs. starman/PSGI is the only difference between them.

    As an example, doing a search in the webapp for "ångbåten" returns 10 hits for "ångbåten" on the apache server, and 0 hits for "Ã¥ngbÃ¥ten" via starman. Since it's triple/quadruple-encoded and returns no hits, I figure it must be getting double-encoded on the way in (when it reads the search terms), then double-encoded again on the way out (when displaying the results).

    Any ideas for what I might have messed up in either the starman startup or the PSGI file to cause this issue?

    Bonus Question: Is there any way to get starman to put timestamps in its error log? It would be very useful to be able to tell whether an error just happened or if it's three days old.

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.