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
Copying a list of files from a directory to a new directory
1 direct reply — Read more / Contribute
by ramjamman
on Jul 20, 2018 at 21:06

    Hi Monks I would welcome any suggestions as to the best way to resolve this problem

    I have on a centos server about 7000 files mostly pdf in one directory. I need to download about 4000 of them for which I have a list in a csv or txt file of their names.

    I am thinking the best way to approach this is to run a script from the command line to copy the files required into a new directory using a script something like that below and then I can download them .

    However I am always open to any suggestions of any other methods that would achieve a solution to this problem Thank You

    #!/usr/bin/perl -w open FILE , '<' , "mylist.txt" || die "$!"; while (<FILE>){ chomp; system("cp $_ newdirectory/"); } close FILE;
XML::Twig - '#CDATA' attribute in hashref disappears after first call to XML::Twig::Elt->new()
No replies — Read more | Post response
by ateague
on Jul 20, 2018 at 17:56

    Good afternoon!

    I am experiencing a rather unexpected issue creating new elements with XML::Twig's XML::Twig::Elt->new() method where the '#CDATA' key in my attribute hashref gets undefined after passing it to new().


    my $attributes = $sibling_element->atts(); $attributes->{'#CDATA'} = 1; # '#CDATA' attribute is defined here my $elt1 = XML::Twig::Elt->new('present', $attributes, 'This is in CDA +TA'); # '#CDATA' attribute is undefined here my $elt2 = XML::Twig::Elt->new('missing', $attributes, 'This is not in + CDATA');

    Is this supposed to happen? I did not see anything mentioning this in the CPAN docs...

    Thank you for your time.

    Perl info:

    perl -v This is perl 5, version 26, subversion 0 (v5.26.0) built for MSWin32-x +64-multi-thread
    XML::Twig info: 3.52

PL_strtab/SHAREKEYS and copy-on-write leak
2 direct replies — Read more / Contribute
by sezal
on Jul 20, 2018 at 17:11

    Perl internally uses dedicated hash PL_strtab as shared storage for hash's keys, but in fork environment like apache/mod_perl this creates a big issue. Best practice says to preload modules in parent process, but nobody says it's eventually allocates memory for PL_strtab and these pages of memory tend to be implicitly modified in child processes. There are seems to be 2 major reasons of modification:

    Reason 1: reallocation (hsplit()) may happen when PL_strtab growths in child process.

    Reason 2: REFCNT every time new reference created.

    Example below shows 16MB copy-on-write leak in attempt to use hash. Attempts to recompile perl with -DNODEFAULT_SHAREKEYS fails ( I was able to get access to PL_strtab via XS module and ideally I'm looking for a way to downgrade all hashes created in parent to keep hash keys within a hash (HE object) rather than PL_strtab, i.e. turn off SHAREKEYS flag. This should allow to shrink PL_strtab to minimum possible size. Ideally it should have 0 keys in parent. Please let me know you think it's theoretically possible via XS.

    #!/usr/bin/env perl use strict; use warnings; use Linux::Smaps; $SIG{CHLD} = sub { waitpid(-1, 1) }; # comment this block { my %h; # pre-growth PL_strtab hash, kind of: keys %$PL_strtab = 2_000_000 +; foreach my $x (1 .. 2_000_000) { $h{$x} = undef; } } my $pid = fork // die "Cannot fork: $!"; unless ($pid) { # child my $s = Linux::Smaps->new($$)->all; my $before = $s->shared_clean + $s->shared_dirty; { my %h; foreach my $x (1 .. 2_000_000) { $h{$x} = undef; } } my $s2 = Linux::Smaps->new($$)->all; my $after = $s2->shared_clean + $s2->shared_dirty; warn 'COPY-ON-WRITE: ' . ($before - $after) . ' KB'; exit 0; } sleep 1000; print "DONE\n";
Efficient enumeration of pandigital fractions
3 direct replies — Read more / Contribute
by kikuchiyo
on Jul 20, 2018 at 16:51

    The fraction 6952 / 1738 has a curious property: each non-zero decimal digit appears exactly once in the expression, and the result of the divison happens to be the missing digit, 4.

    Are there, by any chance, other fractions that share this property? It is fairly simple to devise a semi-brute force solution to answer this question:

    restate the problem as abcd = efgh * i, generate all 5-element variations (k-permutations) of the set of digits 1..9, perform the multiplication and check that the result consists only of digits not in the sequence.

    Here is a somewhat optimized implementation:

    For base 10 this runs quickly enough to find that there is one additional solution. But for the obvious and straightforward generalization to higher bases this brute force solution is not going to cut it.

    Tinkering with the innards of the loop or using a different permutation engine might give us a speedup factor of two, while rewriting the whole thing in C might give us two orders of magnitudes. But we'd be still generating all permutations, and the number of those grows relentlessly as the base increases ((b - 1)! / (b/2 - 1)!):

    6 60
    8 840
    10 15120
    12 332640
    14 8648640
    16 259459200
    18 8821612800
    20 335221286400

    On my machine the program above needed 6 seconds to find all base-14 solutions, more than 3 minutes for base-16, and I dared not run it with higher bases.

    However, the number of actual solutions is much smaller:

    6 	1
    8 	2
    10 	2
    12 	18
    14 	136
    16 	188

    which suggests that there may be better, more efficient approaches that don't have to trudge through a lot of useless permutations to find the solutions. However, so far I haven't been able to find one.

    Any thoughts?

Can you limit the scope of require?
3 direct replies — Read more / Contribute
by taylorK
on Jul 20, 2018 at 14:58

    Hi Monks,

    First post so I apologize if I miss something or if there are small errors in the example I provided, I am just using it to explain my issue. I have run into this situation a few times now and have not been able to track down an answer. Let me be clear that this is for a work application so I cannot simply change the current setup of files etc...

    Background: We use multiple .conf files to house "parsers" for data files. Each of these .conf files has a sub that contains a header map in the form of an array. These .conf files are not packages.

    Question: I would like to write a program that will get that header map from one of these .conf files then get it from a different one as well but only for the purposes of a very simple operation. My current method is to use a require (as seen below) and grab the sub. The problem is when I use require a second time to get the second .conf file it completely overwrites the first require. Can I call this second one and have the scope of it limited to a particular subroutine or variable?


    if (-e "myfilepath/test1.conf") { require "myfilepath/test1.conf" or die("myfilepath/test1.conf - $! +\n"); } else { die("Missing provider config file: myfilepath/test1.conf\n"); } $header_map = &get_config; # grab a file to parse while (defined(my $line = <FILE>)) { my $second_file = &compare(); # split $line into the header if ($first_file{test} =~ /$seconf_file{test}/i) { # etc.... } } sub compare { if (-e "myfilepath/test2.conf") { require "myfilepath/test2.conf" or die("myfilepath/test2.conf +- $!\n"); } else { die("Missing provider config file: myfilepath/test2.conf\n"); } $header_map_2 = &get_config; # grab a file to parse while (defined(my $line = <FILE>)) { # split $line into the header } return "$parsed_line"; }


Catalyst app hangs httpd
1 direct reply — Read more / Contribute
by kenowen
on Jul 20, 2018 at 11:14
    This application is a web site ( which has been posted on three servers before without problem. Now I'm trying to post on AWS. when I start httpd (apachectl) it looks fine until the end, and it hangs. The gbd backtrace at the hang is:
    #0 __strcasecmp_l_avx () at ../sysdeps/x86_64/multiarch/strcmp-sse42. +S:203 #1 0x00005572988d7234 in ap_find_command (name=name@entry=0x5572c91ea +f70 "\260}1\232rU", cmds=0x7f678ba3c890) at /usr/src/debug/httpd-2.2. +34/server/config.c:908 #2 0x00005572988d7289 in ap_find_command_in_modules (cmd_name=cmd_nam +e@entry=0x5572c91eaf70 "\260}1\232rU", mod=mod@entry=0x7ffce845ff50) at /usr/src/debug/httpd-2.2.34/server/config.c:924 #3 0x00005572988d73cd in ap_build_config_sub (p=p@entry=0x55729a24413 +8, temp_pool=temp_pool@entry=0x55729a250198, l=l@entry=0x55729fbc0708 + "\260}1\232rU", parms=parms@entry=0x7ffce8460040, current=current@entry=0x7ffce845 +ffb0, curr_parent=curr_parent@entry=0x7ffce845ffb8, conftree=conftree +@entry=0x7ffce8460018) at /usr/src/debug/httpd-2.2.34/server/config.c:1007 #4 0x00005572988d7ae2 in ap_build_config (parms=0x7ffce8460040, p=0x5 +5729a244138, temp_pool=0x55729a250198, conftree=0x7ffce8460018) at /usr/src/debug/httpd-2.2.34/server/config.c:1224 #5 0x00007f678b81af97 in modperl_config_insert () from /etc/httpd/mod +ules/ #6 0x00007f678b81b047 in modperl_config_insert_parms () from /etc/htt +pd/modules/ #7 0x00007f677da1283a in ?? () from /usr/lib64/perl5/vendor_perl/auto +/Apache2/CmdParms/ #8 0x00007f678b536ee7 in Perl_pp_entersub () from /usr/lib64/perl5/CO +RE/ #9 0x00007f678b52f776 in Perl_runops_standard () from /usr/lib64/perl +5/CORE/ #10 0x00007f678b4c7ca0 in Perl_call_sv () from /usr/lib64/perl5/CORE/l #11 0x00007f678b81d429 in modperl_callback () from /etc/httpd/modules/ #12 0x00007f678b81c3d0 in modperl_cmd_perldo () from /etc/httpd/module +s/ #13 0x00005572988d5816 in invoke_cmd (cmd=0x7f678ba3c480, parms=parms@ +entry=0x7ffce84609f0, mconfig=0x55729a2c5158, args=0x55729a2a40a0 "#\tuse lib '/home/ec2-user/.cpan/build/Text-S +impleTable-2.05-LibLQW/lib/';\nuse lib '/home/ec2-user/perl5/lib/perl +5';\nuse lib '/var/www/html/ +b';\nuse PACE;\n#print"...) at /usr/src/debug/httpd-2.2.34/server/con +fig.c:757 #14 0x00005572988d79c7 in ap_walk_config_sub (section_vector=0x55729a2 +c1b88, parms=0x7ffce84609f0, current=0x55729a2a4178) at /usr/src/debug/httpd-2.2.34/server/config.c:1163 #15 ap_walk_config (current=0x55729a2a4178, parms=parms@entry=0x7ffce8 +4609f0, section_vector=0x55729a2c1b88) at /usr/src/debug/httpd-2.2.34 +/server/config.c:1196 #16 0x00005572988cf88a in virtualhost_section (cmd=0x7ffce84609f0, dum +my=<optimized out>, arg=<optimized out>) at /usr/src/debug/httpd-2.2. +34/server/core.c:2259 #17 0x00005572988d5816 in invoke_cmd (cmd=0x557298aff6f0 <core_cmds+80 +>, parms=parms@entry=0x7ffce84609f0, mconfig=0x55729a24fb90, args=0x5 +5729a2a39c0 "*:80>") at /usr/src/debug/httpd-2.2.34/server/config.c:757 #18 0x00005572988d79c7 in ap_walk_config_sub (section_vector=0x55729a2 +4f770, parms=0x7ffce84609f0, current=0x55729a2a3980) at /usr/src/debug/httpd-2.2.34/server/config.c:1163 #19 ap_walk_config (current=0x55729a2a3980, parms=parms@entry=0x7ffce8 +4609f0, section_vector=0x55729a24f770) at /usr/src/debug/httpd-2.2.34 +/server/config.c:1196 #20 0x00005572988d8812 in ap_process_config_tree (s=s@entry=0x55729a24 +e440, conftree=<optimized out>, p=0x55729a244138, ptemp=<optimized ou +t>) at /usr/src/debug/httpd-2.2.34/server/config.c:1765 #21 0x00005572988c05e5 in main (argc=3, argv=0x7ffce8460c58) at /usr/s +rc/debug/httpd-2.2.34/server/main.c:665
    The virtual host configuration is:
    <VirtualHost *:80> ServerAdmin ServerName ServerAlias DocumentRoot /var/www/html/ +/root ErrorLog logs/ CustomLog logs/ common #modperl config <Perl> use lib '/home/ec2-user/perl5/lib/perl5'; use lib '/var/www/html/ +lib'; use PACE; </Perl> <Location /> SetHandler ModPerl PerlResponseHandler PACE </Location> #modperl end </VirtualHost>
/perl5.10/bin and linux
3 direct replies — Read more / Contribute
by yaklichk0719
on Jul 20, 2018 at 10:57
    I saw this in the bash_profile some setting: /perl5.10/bin for CentOS 6.3, where is my centOS 6.6 I find this: /usr/bin/perl. I am assuming this is where Perl executive are kept by default? I am trying to understand why in the first case they use it this way and does it make a difference when use Perl libraries in a packages? Thank you, Sergey
Constants lists in array slices
4 direct replies — Read more / Contribute
by LanX
on Jul 20, 2018 at 07:40
    The following code is surprising me in two ways:

    use strict; use warnings; use feature 'say'; use constant FL => 17,3,16; use constant FL2 => 22,4,13; my @row= "a".."z"; my @a = (" ") x 26; @a[17,3,16] = @row[22,4,13]; say "@a"; @a = (" ") x 26; @a[FL()] = @row[FL2]; say "@a";

    It seems like constant folding isn't happening at all!!!

    • I get a warning for FL2
      Scalar value @row[FL2] better written as $row[FL2] (ignoring that its a constant _list_)
    • Deparsing the code shows that FL and FL2 are called as functions
      @a[FL()] = @row[FL2()];
    Am I missing something?

    (This is perl 5, version 16,)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Writing IBM Mainframe floating point
2 direct replies — Read more / Contribute
by caden_varn
on Jul 20, 2018 at 06:19
    I need to create a file including single precision floating point fields in the IBM mainframe format (specifically to be read by the SAS s370frb4. informat). I am on an HP-UX 11 box which evidently uses a different floating point - when I use pack's 'f' option the value changes when read in by the SAS program on the other end. Is there an option or module that can do this easily? Any help greatefully received...
get text from node - XML::LibXML
3 direct replies — Read more / Contribute
by corfuitl
on Jul 20, 2018 at 05:36

    Hi all

    I have the XML file

    <seg><foo mid="0" mtype="seg"><g id="1">Need to export this text</g></foo></seg>

    and I would like to get the text with its XML tags (i.e. <g id="1">Need to export this text</g>

    for my $foo ($seg->findnodes('foo')) { my $mid = ($foo->findvalue('@mid')); my $mrktext = ($foo->findnodes('text()')); print "$mid $mrktext\n"; }

    I use this but it doesn't export any text.

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 lurking in the Monastery: (4)
    As of 2018-07-21 02:35 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (443 votes). Check out past polls.