Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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
If line matches, print column, else print file name
1 direct reply — Read more / Contribute
by Yakup
on Oct 25, 2016 at 14:05

    Hello everyone. I had used Perl a little bit long time ago and now I'm trying to write a little program, but I have got stuck. I would like to check bunch of kickstart files for a '--hostname=' pattern. If it matches I want to get the collumn right after it (actual hostname). Else it should take the name of the file (stripped of '.ks' suffix) and save all of them to the array. Commented lines should not be taken. This is how I have done it in bash + awk

    #!/bin/env bash declare -a hostlist=$(grep -P '(?<!#)--hostname' *.ks | awk -F'=' '{pr +int $2}') declare -a not_predefined_hosts=$(grep -L "\-\-hostname" *.ks | cut -d +\. -f1 ) declare -a commented=$(grep -l ".*#.*\-\-hostname" *.ks | cut -d\. -f1 + ) for host in "${not_predefined_hosts[@]}" do hostlist+="$host" done for host in "${commented[@]}" do hostlist+="$host" done

    This works, but when I want to do it with perl, I get multiple issues.

    #!/usr/bin/env perl use strict; use warnings; use File::Basename; my $lab_root = dirname $0; opendir( DH, $lab_root) or die "Cannot open $lab_root: $!\n"; my @kickstarts = grep ( /\.ks$/, readdir(DH)); my @bsname ; my $hostname; for my $kickstart (@kickstarts) { my $name = (split /\./)[0], $kickstart; open my $fh, $kickstart or die "Cannot open $kickstart: $!"; while (<$fh>) { chomp; my @fields = split /=/; if ( $fields[0] eq '--hostname') { $hostname = $fields[1]; print "$hostname\n"; push @bsname , $hostname; }else { $hostname = $name; print "$hostname\n"; push @bsname , $hostname; } close $fh; } }

    First, I get the warnings

    Provisioner]$ ./ Useless use of private variable in void context at ./ line + 13. Use of uninitialized value $_ in split at ./ line 13. Use of uninitialized value $hostname in concatenation (.) or string at + ./ line 24, <$fh> line 1. readline() on closed filehandle $fh at ./ line 27. Use of uninitialized value $_ in split at ./ line 13. readline() on closed filehandle $fh at ./ line 27. Use of uninitialized value $_ in split at ./ line 13. Use of uninitialized value $hostname in concatenation (.) or string at + ./ line 24, <$fh> line 1. readline() on closed filehandle $fh at ./ line 27. Use of uninitialized value $_ in split at ./ line 13. Use of uninitialized value $fields[0] in string eq at ./ l +ine 18, <$fh> line 1. Use of uninitialized value $hostname in concatenation (.) or string at + ./ line 24, <$fh> line 1. readline() on closed filehandle $fh at ./ line 27.

    I don't understand why I'm getting "Use of uninitialized value" warnings, when I initialize all variables with "my" beforehand. Also, why does the filehandle "$fh" close before the close statement? And what the "Useless use of private variable in void context" mean? All examples I was able to google on it were very different from my code and didn't help me to understand that.

    Second, when the code runs,( with added 'print @bsname;' ) it matches only once (but strangely prints twice). There are multiple kickstart files with "--hostname" directive in it, but it ignores the rest. Also the "else" part doesn't work (none of the file names are matched).

    [################### Provisioner]$ ./[################### Provisioner]$

    Can somebody please point out mistake in my code? I'm sure it will be something trivial, but I can't wrap my head around it. Thanks!

IPC::Run command is correctly constructed but does not have effect
No replies — Read more | Post response
by byrnejb
on Oct 25, 2016 at 12:55

    I am hacking at an old perl scrip that is used to generate and maintain a private CA. The script contains this code:

    . . . use IPC::Run qw( start pump finish timeout new_appender new_chunker); . . . sub cmd { my $self = shift; my $cmd = shift; my $cmdline = shift; my $args = shift; my $conf; my $cfgcmd; if ( (grep $_ eq $cmd,qw(req ca)) && !$args->{noconfig}) { $conf = $self->{csp}->writeConfig($cmd,$args); $self->{csp}->die("Unable to write configuration file") unless -f $c +onf; $cfgcmd = " -config $conf "; } elsif ($cmd eq 'x509' && !$args->{noconfig}) { $conf = $self->{csp}->writeConfig($cmd,$args); $self->{csp}->die("Unable to write configuration file") unless -f $c +onf; $cfgcmd = " -extfile $conf -extensions extensions "; } $cmd = '' if $cmd eq 'dummy'; ${$self->{_in}} = "$cmd $cfgcmd $cmdline"; if ($ENV{CSPDEBUG}) { $self->warn("Here I am"); $self->warn("# openssl $cmd $cfgcmd $cmdline\n"); } $self->{_handle}->pump while length ${$self->{_in}}; $self->{_handle}->finish; . . .
    When I run this command
    csp CA_HLL_ROOT_2016 init --verbose --type=root --keysize=4096 --days= +7317 --dige +st=sha512 "CN=CA_HLL_ROOT_2016,OU=Networked Data Services,O=Harte & L +yne Limited,L=Hamilton,ST=Ontario,C=CA,DC=harte-lyne,DC=ca"
    then I get this result:
    [CSP][ ] Here I am [CSP][ ] # openssl genrsa -des3 -passout pass:'test me' -out / +home/byrnejb/Projects/Software/rcsp/ca_test_a/csp/CA_HLL_ROOT_2016/pr +ivate/ca.key 4096 [CSP][ ] Here I am [CSP][ ] # openssl req -config /home/byrnejb/Projects/Software +/rcsp/ca_test_a/csp/CA_HLL_ROOT_2016/tmp/csp-32489.conf -x509 -sha51 +2 -days 7317 -key /home/byrnejb/Projects/Software/rcsp/ca_test_a/csp +/CA_HLL_ROOT_2016/private/ca.key -passin pass:'test me' -new -out /ho +me/byrnejb/Projects/Software/rcsp/ca_test_a/csp/CA_HLL_ROOT_2016/ca.c +rt

    There are no errors but the files specified in the command lines are not created. If I copy and paste the command lines that are output from the warn statements then the files are created without problem.

    I have zero experience with Perl. Well, not quite zero any more but not much. Is there something obvious that I am missing here? The entire project is available on github at if more context is desired.

Efficient way to print 2 columns from 2 Array of Arrays next to each other
3 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 25, 2016 at 12:34
    Dear Monks,
    I have 2 arrays of arrays (let's call them @AoA_first and @AoA_second that have same amount of lines and columns. What I want to do is read through each of them, and print in a file, each column of each array of arrays, separated by tabs.
    I managed to do so with the following code, but the thing is that it is very slow and there has to be a faster way that I am not aware of...
    Please advise!
    for $i ( 0 .. $#AoA_first ) { $row_first = $AoA_first[$i]; $row_second = $AoA_second[$i]; open TEMP, ">TMPFILES/$i.tmpfile"; for $j ( 0 .. $#{$row_first} ) { print TEMP "$row_first->[$j]\t$row_second->[$j]\n"; } close TEMP; } }

    I think it is slow for me because it reads through every element of every column of each of the two array of arrays, but I can't think of another way to "dump" the two columns side by side into a new file...
Packaging Libraries before deploying my Scripts.
1 direct reply — Read more / Contribute
by ArunMKumar
on Oct 25, 2016 at 07:02
    Namaste Monks.. I have written a set of scripts, on a local system (my laptop) The work fine here.
    Now My scripts are required to be deployed in remote system(s) and the thing I am worried about is the availability and the installation of the Libraries that I have used.
    The 2 Libraries that are currently in use are "XML::LibXML" and "Spreadsheet::ParseExcel".
    I have this idea where I will put them in a "lib" directory in the same project Directory, and the scripts will refer from them. My questions are as follows.
    I have installed them in a directory, using the commands as follows from the extracted tar files.

    perl Makefile.Pl
    make test
    make install PREFIX=<path_to_lib> LIB=<path_to_lib>

    I also see the .pm files being populated in various tree structures in that lib directory.
    My question now is , How do i force my scripts to reference this lib directory when It wants to look for the modules?
    I am new to perl, from what I searched online I think it has to do with modifying @INC variable, while some solutions say about explicitly including these versions of the library (which I have no clue, as those post included their own perl modules).
    so.. How do i force my scripts to use these libraries exclusively, and not throw an error when i deploy them to different systems (all Linux systems with perl 5 or above for sure).
heredoc and Carriage return
3 direct replies — Read more / Contribute
by gabrielsousa
on Oct 25, 2016 at 05:18

    heredoc read the LF ( \n ) , but dont parse / stores the Carriage return ( \r )

    how can i force heredoc to read and store carriers return ( \r ) ?

    =====================added before======================

    i have a pdf file / binary inside my perl script using heredoc
    has 40 Carriage returns, i can search on VI, i see the ^M
    but when i print to a file i lose all Carriage returns..
    i'm assuming is a problem of heredoc
perl and Docker
3 direct replies — Read more / Contribute
by IAperl
on Oct 24, 2016 at 22:44

    Hi Everyone

    I am working on a Docker project which implements the following script (

    This script is written in python and contains a section of code which converts a certain conf_file written in perl syntax.

    This conf_file is used by an external function 'Nlpfit' which reads perl syntax (

    while running this code in docker environment, i keep getting error:

    String found where operator expected at (eval 8) line 6, near "u'step + ' => 8, u'" and Error reading config from /scratch/volgenmodel-fast-example/workfl +ow/writeconf_01_/fit_stage_01.conf (fix it!)_

    I have checked the conf_file for syntax error, but there doesnt seem to be any string error. My default conf_file looks like this

    @conf = ( {'step' => 8, 'blur_fwhm' => 4, 'iterations' => 20}, {'step' => 6, 'blur_fwhm' => 3, 'iterations' => 20}, {'step' => 4, 'blur_fwhm' => 2, 'iterations' => 10}, {'step' => 2, 'blur_fwhm' => 1, 'iterations' => 10}, );

    Note that this external function (Nlpfit) file uses version 1.2.0 but the docker environment uses Perl5Lib

    Do you think this is due to compatibility issue between Perl version 1.2.0 and Perl version 5.0

    I am new to perl and need guidance on what is causing this error and how can I fix it

    Also note that this algorithm runs fine outside Docker environment


blockchain Someone ?
3 direct replies — Read more / Contribute
by dominix
on Oct 24, 2016 at 21:52
    Is there any API/Module for Perl to deal with blockchain ?

    I've searched on metacpan for blockchain but found nothing, I also searched Github an only find this project

    which is centered on Ethereum but I am looking for something more generic on blockchain.

    I guess Perl desserve an entry point for that technology, I'm confortable enought with Perl5 and I am willing to learn Perl6, so any of these Perl will fit, but I'm not experienced enought on that matter to build such a solution by myself. If you have heard of any project of interest in that matter thank for your inputs.
PDF::Tk Background Image
2 direct replies — Read more / Contribute
by qwertywolf
on Oct 24, 2016 at 18:00

    I am new to PERL and don't even know where to start. This code was written before me and they want me to use PDF::Tk now.

    What it does is takes the HTML that is in the CGI, it puts those into lines, turns those lines into PS lines and then turns those PS lines into PDF lines and prints out the PDF.

    So after the PDF lines are printed we need to stick an image behind all the lines of text. Can anyone help with that?

    sub printFilePdf { my $unique_id = shift; my ($file) = "$OUTFILES/$unique_id.html"; open(my $htmlFH, '<', $file) or die "Can't open file $file $!\n"; my $processId = open2(\*POUT, \*PIN, qq(html2ps -U -f /home/apache +/cgi-bin/test/html2psrc-tst)); my @lines = <$htmlFH>; print PIN @lines; close PIN; my @psLines; while (<POUT>) { chomp; push(@psLines,$_); } waitpid $processId, 0; $processId = open2(\*POUT, \*PIN, qq(ps2pdf -sPAPERSIZE=letter - - +)); print PIN "$_\n" foreach(@psLines); close PIN; my @pdfLines; while (<POUT>) { chomp; push(@pdfLines, $_); } waitpid $processId, 0; #I am thinking right about here print "Content-Type: application/pdf\n"; print "Content-Disposition: attachment; filename=record.pdf\n\n"; print "$_\n" foreach(@pdfLines); }
Detecting stale pid file under *nix
3 direct replies — Read more / Contribute
by Dallaylaen
on Oct 24, 2016 at 17:36

    This is more of a Unix question rather than a Perl one, but still...

    I'm looking for a way to stop my daemon, but I don't want to terminate an innocent bystander process. I came up with idea that PID file is created after the process has been spawned. Therefore, it's not older then the process, so the following code was written which seems to work correctly on my Ubuntu:

    open (my $fd, "<", $conf->{pidfile}) or die "Failed to read pidfile $conf->{pidfile}: $!"; my $pid = <$fd>; chomp $pid; die "Broken pid file $conf->{pidfile}" unless $pid =~ /^\d+$/; # detect stale pid if ([stat $fd]->[9] >= ([stat "/proc/$pid"]->[9] || 9**9**9)) { print "Killing pid $pid...\n"; kill INT => $pid; };

    Of course, it can still be tricked by touching the pid file, but then it's also possible to write rubbish into the pid file anyway.

    Now I would like to ask what is the proper way of avoiding sending signal to a wrong process? CPAN has a multitude of modules for PID file handling, I was unable to choose one.

    Thank you!

Using read/syswrite with IO::Socket::SSL
3 direct replies — Read more / Contribute
by Bloehdian
on Oct 24, 2016 at 17:26

    Hello Monks,

    the following code

    $sock = IO::Socket::SSL->new( LocalAddr => $ssl_addr, LocalPort => $ssl_port, Listen => 5, Reuse => 1, Proto => 'tcp', SSL_cert_file => 'server.crt', SSL_key_file => 'server.key', ) || die "Can't bind TCP SSL port"; $sock->blocking(0); ... some other code ... while ( 1 ) { ... some other code ... # SSL server listens to clients # $client_sock = $sock->accept(); $sock->recv( $ext_mesg, 4096 ); eval( $bgp->$ext_mesg ); ... some other code ... }

    leads to the following message

    Use of recv() not implemented in IO::Socket::SSL; use read/sysread instead at line 181.

    but I have no idea how to do this. read/sysread require a file handle. How to I get a file handle from an IO::Socket::SSL object?



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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (10)
    As of 2016-10-25 18:23 GMT
    Find Nodes?
      Voting Booth?
      How many different varieties (color, size, etc) of socks do you have in your sock drawer?

      Results (326 votes). Check out past polls.