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
Escaping double quotes in complete document
1 direct reply — Read more / Contribute
by MeinName
on Jun 26, 2017 at 08:03

    Namaste, dear Perl Monks and Nuns!

    I have a little site to administrate that depends on Perl scripts printing HTML and JavaScript. The site stores data in and loads data from a database using SOAP::Lite and up until last week everything ran very smoothly. Unfortunately our servers got an update that raised Perl from version 5.10.1 to 5.16.3 and there the problems started. Firstly all special characters, like , , , and got mangled by the encoding. This error was resolved by using

    use open qw(:std :utf8); use Encode; use utf8;

    to load the data, by writing

    print header(-type=>'text/html',-charset=>'utf-8'); print start_html(-title => "FooBar");

    to start the web page and by using

    my $text=decode("utf8", $query->param("textbox"));

    to get the data back from the page and store it in the DB.

    Today I had to realize that it's also no longer possible to load strings from the database that contain double quotes, because the HTML textboxes see the double quote as an end marker for the textbox value. (EDIT: Saving content with double quotes is possible. I verified this directly on database level.)

    There are probably even a few more characters that cause issues that I haven't stumbled upon yet.

    I found that I can resolve the issue by using HTML::Entities and calling encode_entities() on the variable I want to see in my textbox, but I am loading hundreds of variables and several arrays of strings in one page visit and there is no way I can revisit every single script in my application to see which variables I maybe need to encode.

    Is there any way I can encode the whole HTML part of my script or a way to circumvent this mess?

    Thanks alot in advance

trying to read files in @ARGV but getting GLOB error! :(
1 direct reply — Read more / Contribute
by fasoli
on Jun 25, 2017 at 14:50

    Dear Wise Monks, I'm trying to open a bunch of files in ARGV and read them line by line and then split in the way I'm specifying in the code - and then for testing I am trying to print their contents. However I'm getting a "Can't open GLOB(0x605d48): No such file or directory" error :( Can anyone please give me a hint what's happening? I'd be extremely grateful.

    I'm trying to use two different filehandles to open the files and then push those two filehandles into @ARGV. Then I'm trying to use the <> operator. There is something that messes things up though and I'm getting an error. Any hints/feedback would be immensely useful.
    #!/bin/perl/ use strict; use warnings; my $molec1 = "molec1"; my $molec2 = "molec2"; my $input1; my $input2; @ARGV = (); my $i; my $j; my $path = "/store/group/comparisons"; my $line; my @columns; my $nextUnless = 2; # nr of lines to skip from beginning my $CountLines = 0; # total nr of lines in all files, including commen +ts for ($i=1; $i<=3; $i++) { open $input1, '<', "$path\/File-${molec1}-cluster${i}.out" or die $! +; for ($j=1; $j<=4; $j++) { open $input2, '<', "$path\/File-${molec2}-cluster${j}.out" or die +$!; push @ARGV, $input1; push @ARGV, $input2; my @list; my $list; my $a; my $b; while ($line = <>) { $CountLines += 1; next unless $. > $nextUnless; chomp $line; $line =~ s/^\s+|\s+$//g; push @list, [split/\s+/, $line]; @columns = split /\s+/, $line; } close $input1; close $input2; for ($a=0; $a<=$#columns; $a++) { for ($b=0; $b<=$#columns; $b++) { print "$list[$a][$b] "; # to check matrices } print " \n"; } } # for j } # for i
Ignore a range of numbers ina List
8 direct replies — Read more / Contribute
by pr33
on Jun 24, 2017 at 23:05

    Hello Monks,

    Within a list , I want to ignore a section of numbers that start with 6 and extending to the 7 My code below does that only for the first set of numbers . I am trying to splice the array on the copy and not original array But looks like the loop ends before it can read the next 6 in the list . Not looking for any grep or regex solution for this task .

    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; ######## my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7]; my $flag = 0; my $i = 1; my $sixindex = 0; my @copy = @$aref; if (scalar(@$aref) >= 2) { foreach my $x (0..$#{$aref}) { if (defined($aref->[$x])) { if ($aref->[$x] == 6) { $flag = 1; $sixindex = $x; next; } if ($flag) { unless ($aref->[$x] == 7) { $i++; next; } elsif ($aref->[$x] == 7 ) { splice(@copy, $sixindex, $i + 1); $flag = 0; $i = 1; $sixindex = 0; next; } } } } } print Dumper \@copy;
    ./ignore_6_to_7.pl $VAR1 = [ 1, 1, 6, 99, 99, 7 ];
    Result Expected [1, 1] by deleting all the numbers in the list starting with 6 and unt +il it finds the first available 7 .
Problem install Tk on CentOs using Perl 5.26
2 direct replies — Read more / Contribute
by dabella12
on Jun 24, 2017 at 09:34

    Hello monks i'm having a problem install Tk , my system is CentOs 6.4 32bit and I am running perl 5.26 this ist the message i'm getting

    Running make test

    ny help will be appreciated

Match all Non-0 and Letters
6 direct replies — Read more / Contribute
by arblargan
on Jun 24, 2017 at 03:05

    I'm relatively new to Perl and am having a terribly difficult time figuring this one out. I'm expecting a string in the following format:


    Essentially, a normal word will be 7 0's followed by a number between 0-9 (8-digits total). However, occasionally there is corruption in the file being processed, causing the format to have something like the following:

     FFFFFFFF or  6C163512

    I want to skip these lines of corruption and loop until the corruption has been passed. This is where the tricky part comes in (at least for me). I have tried every combination of matching I can think of, but can't seem to get this one squared away. Below are the lines of code I have tried:

    $Disc = get_word(); $D1 = substr($Disc,0,7); $D2 = substr($Disc,7,1); if ($D1 !~ /0+/ and $D2 !~ /([0-9]+)/) ##Catches FFFFFFFF just fine, b +ut not 6C163512 #### $D1 = 6C16351 and $D2 = 2 ###Get words until corruption is cleared. Works great with FFFFFFF +F, but will not catch 6C163512 if ($D1 !~ /0000000/ and $D2 !~ /\D/) ## Same as above ###Get words until corruption is cleared. Works great with FFFFFFF +F, but will not catch 6C163512 if ($Disc =~ /[1-9a-ZA-Z]{7}\D/ ## Same as above ###Get words until corruption is cleared. Works great with FFFFFFF +F, but will not catch 6C163512

    I've been working on this forever and can't seem to figure out how to dynamically catch this corruption in the event that all F's have migrated from the string word. I created the $D1 and $D2 variables to try and see why the regex patterns weren't matching, but I still can't figure it out.

    Lastly, it should be noted that occasionally, the line of corruption will show as 01020102. The corruption value will be dynamic. This is why I simply can't use /\D+/ for the majority of the string as the first 7 digits must be 0 for a valid word.

Splice an array into another array
4 direct replies — Read more / Contribute
by Smeb
on Jun 23, 2017 at 18:30

    Hi guys,

    As the title says, what I wanted to do is take one DNA file and put it into an array, do the same to a second DNA file and then insert one of the files somewhere in the other.

    I have 2 files, DNA3 and DNA4

    The content within the DNA3 file is: ATCGC

    The content within the DNA4 file is: AAATTGC

    What I want to do is to insert DNA4 into position #1 in DNA3, so the result would look like this: AAAATTGCTCGC

    Currently my code looks like this:

    #!/usr/bin/perl -w $DNA3file = 'testDNA3'; open(DNA3handle, $DNA3file); @DNA3 = <DNA3handle>; $DNA4file = 'testDNA4'; open(DNA4handle, $DNA4file); @DNA4 = <DNA4handle>; splice (@DNA3, 1, 0, @DNA4); print @DNA3, "\n"; exit;

    However, what is displayed really puzzled me:



    *Note, in my complier there isn't the space in between the output*

    For the life of me I can't figure out what is wrong, I can run the above operation fine if I manually insert the DNA sequences into an array, but for large DNA set that is simply not possible. Really appreciate any help here, thanks for your time!

Generate a report for every month for a year
3 direct replies — Read more / Contribute
by younggrasshopper13
on Jun 23, 2017 at 16:10
    Hey guys! I have a perl script that I use generate billing reports that gets ran and emailed every month via cronjob to report on some metrics. The perl script runs fine (I originally got this working because of some help from the wonderful monks!) For security reasons I cannot post the script itself but I can post how the current date filter works:
    y $START_DATE = `date '+%Y-%m-%d' -d "-1 month"`; my $END_DATE = `date '+%Y-%m-%d'`; chomp $START_DATE; chomp $END_DATE; my $URL = "http://url/url/url/dump?end_date=$END_DATE&start_date=$STAR +T_DATE&type=csv";
    What I need to do is setup this script to generate a monthly report every month for past 2 years. Is there any easy way to do this with the $START_DATE and $END_DATE? Can anyone help me do this?
Wide character in subroutine entry using encode_qp
1 direct reply — Read more / Contribute
by rumburak
on Jun 23, 2017 at 13:21
    use strict; use warnings; use utf8; use MIME::QuotedPrint; binmode STDOUT, ":encoding(UTF-8)"; my $str = "a\x{201E}z"; #my $str = "az"; #my $str = "a\x{201C}z"; #my $str = "a\x{03C9}z"; #my $str = "az"; #works print $str, "\n"; my $str_q = encode_qp($str); print $str_q, "\n";

    gives me this error message / faulty output:

    az Wide character in subroutine entry at ./x.pl line 18.

    Only the fifth version of $str is working.... Can anyone tell me what is going here? (It is perl 5.24.1.)

Issue with regex
2 direct replies — Read more / Contribute
by shounak mukherjee
on Jun 23, 2017 at 11:47
    Hello Experts, I am new to perl programming and facing a few issues related to perl regular expressions. I need to apply a condition logic only if my $filename is something like: $filename =._foo.xml #Any file that starts with ._" For such files,I need to overlook the content of the file. while( ($fileName = readdir(DIR))) { next if ($fileName eq "^\._.*"); However I think this regular expresion is not able to filter the filenames based on the criteria. I shall be highly obliged if anyone can please help.
Architecture design for full stack development.
4 direct replies — Read more / Contribute
by SimonClinch
on Jun 23, 2017 at 10:42
    Hi Monks, I am developing a website from scratch which will hopefully have plenty of activity some time in the future after it goes live. Although I am still developing it on Windows, I will migrate it to Linux/Apache, with Perl and Postgres completing the rest of the picture before and after migration. I am inclined to avoid PHP for performance reasons.

    While loading the database with some initial reference data (about 1GB), using one-off code that got fixed as I went along, I somehow managed to "lose" some PK values, because sequences do not roll back if accessed with nextval.

    But it occurred to me that when the system goes live, it will need to manage deleted values. Although I was careful to use bigint for PK values where necessary, I don't want to have the situation where the PK values stay fragmented and increase fragmentation over time.

    It was at this point that I realised I needed DBIx::Class, otherwise I would have layers of raw SQL hiding in any homegrown DBMS/Perl framework and that had to be avoided at all costs.

    So I am at the point where I have installed DBIx class and feel obliged to determine the rest of the architecture (MVC perhaps) and at the same level of detail, before writing any methods to dynamically defrag my keys (probably through use of an idpool table/class where inserters will look before calling nextval if nothing is in the pool for the table).

    It becomes clear that the defrag code belongs in the "Controller" part of the framework. But having never built a Controller before, I am hoping for advice on what the classes should look like in there, e.g. should I have a dbController class that uses methods in DBIx. Should it inherit from the Schema and/or should I have some kind of parent-of-all utility class? I am keen to make a good choice now rather than have to make fundamental architecture mods later. Many thanks in advance,


    One world, one people

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.