Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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
option control in script
No replies — Read more | Post response
by dideod.yang
on Jul 22, 2018 at 00:52
    Hi monks. I wonder that Can script control option by itself?? For example, when I want to use @F(automatic split line by line) , I operate option -ane "perl -ane". also there are many options such as -n -e ... Recently I write some script to open text file and really useful on @F. but I always forgot option -ane :(.. I know I am not man.. So I need your help. When I operate script "perl" then script is operated on "-ane" to use @F. Thank you :)
Counting characters without a space
2 direct replies — Read more / Contribute
by htmanning
on Jul 21, 2018 at 21:52
    I have a form that users are filling in by listing numbers separated by commas, but not adding a space. This results in a very long string which makes the resulting page too wide. My thought it to count the number of characters in each word and target any word more than 15 characters without a space. I would then replace the commas with a comma and space. Anyone have a better way to do it?

    How do I find words that are longer than 15 characters?

Perl/Tk Windows fonts for Linux?
No replies — Read more | Post response
by tlhackque
on Jul 21, 2018 at 16:08

    I think I have all the pieces, but not how to put them together:

    I have a Perl/Tk application that runs on Windows & Linux (Fedora, if it matters). Originally written on Windows.

    The Linux default fonts are causing some issues - they make a number of widgets larger, and aren't as readable for some. In any case, the goal is a common look and screen size.

    The application users (X display servers) are mostly Windows (VxXsrv), some Linux.

    So, I'd like to have the application request (and the clients use) the Windows fonts for the Tk widgets.

    What I (think I) know:

    • Perl Tk has "Tk Default, Text, Fixed, Monospace, Heading, Caption, Small Caption, Icon and Tooltip fonts.
    • On Windows, the "System" font is Arial, which is used for the widgets without custom fonts
    • The MS TT fonts (including Arial) can be obtained for use using the process at
    • There ought to be some mapping in .Xdefaults to connect the dots

    That's where I get lost.

    • A - having followed the process (built and installed the RPM), xlsfonts doesn't report Arial on Linux clients. It does report a bunch of other -microsoft fonts. xlsfonts | grep ^-microsoft | sort | head starts with -microsoft-comic
    • B - I assume I need to do something to get VcXsrv to recognize the fonts too. I have no clue what.
    • C - Even if it did - what do I put in .Xdefaults (or elsewhere) to have TK adopt the MS Arial (and Courier) fonts?

    I don't want to declare a custom font for every widget - I want the defaults for this application to switch to these as the "system" fonts"

    I don't know why I find all this stuff so confusing - OS internals are simple :-)

    Thanks in advance

unintstall and Install Perl linux
4 direct replies — Read more / Contribute
by yaklichk0719
on Jul 21, 2018 at 02:33
    I would like to uninstall Perl, which has been already per-installed in centOS 6.6 and after that to install Perl 5.10 in configuration I like. Let's say /perl5.10/bin. Can I use command yum remove Perl or there are other commands in Linux?
exec & system
2 direct replies — Read more / Contribute
by dideod.yang
on Jul 20, 2018 at 23:04
    Hi monks. I need your idea to achieve my script.. I understand difference between exec and system. LOCAL is my local command in my linux. My goal is when LOCAL command fail, then script try again command. How can I do that? below script, each case has limit point.. Please help me.
    #LOCAL: local command #case 1 # when LOCAL failed script die exec "LOCAL"; print "success"; #case 2 #when LOCAL failed but print suceess system "LOCAL"; print "success";
Copying a list of files from a directory to a new directory
3 direct replies — 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()
2 direct replies — Read more / Contribute
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
4 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
5 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"; }


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 contemplating the Monastery: (1)
    As of 2018-07-22 05:17 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 (451 votes). Check out past polls.