Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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
Perl PDL slower than python numpy
2 direct replies — Read more / Contribute
by fanasy
on Sep 20, 2020 at 08:44
    Hi all, I test on same machine with lastest version of perl and python

    Perl version:v5.32.0
    Python version:3.8.5
    I have a little disappoint about the result:
    Perl slower a lot than python.
    weizhong@weizhong-VirtualBox:~/Git/compare-pdl_numpy$ ./compare.csh

    0.5u 1.0s 0:01.84 88.0% 0+0k 0+0io 0pf+0w

    1.2u 2.0s 0:05.83 55.5% 0+0k 19064+0io 907pf+0w

    0.7u 0.7s 0:02.54 60.2% 0+0k 52008+0io 141pf+0w

    1.3u 1.9s 0:05.28 61.3% 0+0k 30432+0io 1005pf+0w

    Perl Code(
    #!/home/weizhong/tools/perl-5.32.0/perl use PDL; $PDL::BIGPDL = 1; $X = zeroes(1000,500,500); $Y = ones(1000,500,500); $Z1 = $X + $Y; $Z2 = $X / $Y;
    Python Code(
    #!/home/weizhong/tools/Python-3.8.5/python import numpy as np X = np.zeros((1000,500,500)) Y = np.ones((1000,500,500)) Z1 = X+Y Z2 = X/Y

    #!/bin/csh -f echo "Python"; time ./ echo "Perl"; time ./ echo "Python"; time ./ echo "Perl"; time ./
Can Test::MockObject mock a file?
2 direct replies — Read more / Contribute
by Lady_Aleena
on Sep 19, 2020 at 16:44

    I am going to start writing tests for my Fancy::Open module that needs a file to open. Test::MockObject was suggested. In reading the beginning of the documentation, I saw it can mock arrays, scalars, code, and globs, but I do not see files.

    So, can Test::MockObject mock a file? If yes, how does it work? I am having a difficult time understanding the documentation.

    My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Compressing/obfuscating Javascript
5 direct replies — Read more / Contribute
by kepler
on Sep 18, 2020 at 14:06


    I found in Perl Monks a very good node about this subject, at

    It works very well, with the exception of the removing of comments, that accidently also cuts/removes urls of the form 'http://etc...' that are processed just to 'http'. The other problem I partially fixed, is the case of, for example, an array that is placed in the code in multiline, ending this way (the first lines) in a comma. The script assumes that the lines must terminate only in ';', '{' or '}'. Either way, can someone help me regarding the regex for the removal of comments without removing urls?

    I would apreciate it very much.

    Thanks, Kepler

Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's
13 direct replies — Read more / Contribute
by Lana
on Sep 17, 2020 at 23:17

    Hello Monks!

    I am stuck with some trivial task. Maybe somebody could give me some idea on how to solve it.

    I need to generate all possible combinations of "0" and "1" - at the specified lenght of "0"'s string and using specified count of "1"'s.

    For example, I have a string of ten zeros - "0000000000", and I need to get all unique combinations using three ones. Like:

    1110000000 1101000000 1100100000 1100010000 ...till... 0000100011 0000010011 0000001011 0000000111

    It seems to be easy to do on a 10-zeros string just by iterating 1024 binary numbers and filtering out those not having three ones and seven zeros. But when it comes to long string of zeros, say 40, I have to iterate an enormous one trillion binary numbers which semms to be a very bad solution with giant overhead.

    The count of "1" and length of "0" string may vary so I would prefer to have it as a subroutine, like:

    sub GenUniStrings { my ($OnesCount, $ZeroStrLn) = @_; # some Perl magick goes here in loop, printing generated unique stri +ngs } GenUniStrings(3,25);

    What is the best way to achieve that with minimal overhead and highest speed?

    Thanks :)

Dealing with package version numbers when monkey-punching
2 direct replies — Read more / Contribute
by MikeTaylor
on Sep 17, 2020 at 13:20
    I maintain two Perl modules, Net::Z3950::SimpleServer and Net::Z3950::FOLIO.

    The former defines classes for a bunch of node-types which together represent a parsed query — things like Net::Z3950::RPN::Term, Net::Z3950::RPN::And, and so on. The latter enhances the functionality of these classes by adding extra methods — a technique I have seen referred to as "monkey-punching", though if there is a more Perlish name for it I would like to be enlightened.

    My problem: SimpleServer is at version 1.21. When I try to publish the newer FOLIO module on CPAN, which is at version 1.0, it is rejected with error messages like:

         module : Net::Z3950::RPN::And
         version: 1.0
         in file: Net-Z3950-FOLIO-1.0/lib/Net/Z3950/
         status : Not indexed because
                 Net-Z3950-SimpleServer-1.21/ in
                 M/MI/MIRK/Net-Z3950-SimpleServer-1.21.tar.gz has a higher
                 version number (1.21)
    How can I specify in the FOLIO module that I am not trying to define these classes, only add new methods? And so tell CPAN that I am not trying to provide alternative definitions.

    Thank you!

Losing Bits with Pack/Unpack
7 direct replies — Read more / Contribute
by o0lit3
on Sep 16, 2020 at 15:50

    I am trying to pack bytes into as few unicode characters as possible. Consider the Twitter 140 character limit, which allows byte-heavy unicode characters. I believe I can squeeze 3 8-bit ascii characters into a single 20-bit unicode character, which I am currently trying via the following:

    $\="\n"; $_='Hello World!'; print; $_=pack'(U)*',map{hex}unpack'(H5)*'; print; $_=pack'(H5)*',map{sprintf"%05X",$_}unpack'(U)*'; print;
    Hello World!
    He`lo Wopld

    Note that when I attempt to pack'(H5)*' the same data I map/unpacked, (almost*) every third character is garbled, a symptom of dealing with an odd 5 hex characters, I'm sure. What is the appropriate way to do this without losing bits?

    Packing/Unpacking with the normal 16-bits works as expected:

    $\="\n"; $_='Hello World!'; print; $_=pack'(U)*',map{hex}unpack'(H4)*'; print; $_=pack'(H4)*',map{sprintf"%04X",$_}unpack'(U)*'; print;
    Hello World!
    Hello World!

    ...and even allows 'n*' packing, since n is a 16-bit unsigned short:

    $\="\n"; $_='Hello World!'; print; $_=pack'(U)*',map{hex}unpack'(H4)*'; print; $_=pack'n*',unpack'(U)*'; print
    Hello World!
    Hello World!
    Thanks for the help, and Cheers!
printing to stdout from a soft ln -s doesn't show up
5 direct replies — Read more / Contribute
by mrkrinkle
on Sep 16, 2020 at 14:11

    Ok so- I have a .pl file that prints to cli:

    print "wpeofkwpoef";

    If I run the .pl directly, it prints just fine. ./

    I have an ln -s link to it in /usr/local/bin/damn.

    If I run it from cli there /usr/local/bin/damn, it runs (the code later on even changes files; it works), but it never prints.

    This same code usta work in perl like 15 years ago lol.

    important to note to test, I made .sh script that just does:

    echo 'wefwe';

    And then did an ln -s to it and it prints the wefew. So .sh can do that from the link but perl demands it be done from the actual file not the link.

    Ubuntu 18.04 $ perl -v This is perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux-gnu-thread-multi (with 67 registered patches, see perl -V for more detail)

    Thanks monks, sorry for the lack of < code> tags but the code is so minimal. m
net::OpenSSH several commands, stdout are mixed up
2 direct replies — Read more / Contribute
by Bolemo
on Sep 16, 2020 at 13:12

    I am quite new to Perl, and I am using smokeping.

    I created a RemoteTraceroutePing Probe based on the RemoteTraceroute one. Basically, the idea is to create a SSH connection to a remote host, and fire regularly traceroute commands and read the output. So far, I was able to have something that works,, and it works with one or two probes, but with more than that, all standard outputs are mixed up.

    I do create the connection this way:
    # $ssh_sr contains the remote host like user@host open my $def_in, '<', '/dev/null'; $self->{ssh} = Net::OpenSSH->new($ssh_sr, default_stdin_fh => $def_in) +;
    Then, the different targets (Traceroute commands) are called like this (in parallel):
    # @cmd contains the traceping command to launch remotely via the exist +ing SSH connexion. my $killed; my (undef, $f_stdout, $f_stderr, $pid) = $self->{ssh}->open3(@cmd); while (<$f_stdout>){ # OUTPUT ANALYZED AND PROCESSED HERE $killed = kill(15, $pid); last; } waitpid $pid, 0; close $f_stdout; close $f_stderr;
    The problem is, despite that I can launch several traceping simultaneously with individual pids, the f_stdout of one pid is receiving the stdout from others. How can I fix that ? Thank you
[OT] A New Everything ?
6 direct replies — Read more / Contribute
by Aldebaran
on Sep 15, 2020 at 18:35

    The Perl Conference changed my life in many ways. As it was coincident with great upheavels due to covid, it becomes hard to separate the two. A lot of real world shtuff has been going on, that I've had to deal directly with, as a civic-minded resident of Portland, OR. Also, the American West is on fire, and I'm grateful that I'm in one of the few places without smoke to breathe.

    That's the paragraph where I've been stuck for quite a while now. That bit about being one of the few places without smoke has certainly changed, and I am stuck inside. So, lots of Big Changes for Aldebaran, and my computing capability needs to be commensurate. Some of the notions I will write about were discussed in this conference, and I have needed *this long* to research them somewhat and come up to speed.

    The first thing I need is a new web host. After watching the conference with this is mind, I realize that there's no 'one size fits all' option. The following should be the specs:

    • useful to someone who lives in US west
    • is not free: if free, then I'm not getting what I need
    • allows me to have a *nix system with sudo privileges
    • will finally let me install Mojolicious
    • doesn't assume that I'm some huge business: I'm not
    • Do ISP's have reputations? If so, I'd prefer one with a good reputation.

    I need to figure out the whole thing again from scratch; my previous website is gone. I ask for help to build back better.

Change to package name handling in Perl 5.18?
1 direct reply — Read more / Contribute
by tobyink
on Sep 15, 2020 at 15:23

    Try this on an old version of Perl (before 5.17) and a newer one (5.18 or above):

    perl -MHTTP::Tiny -e'my $class = q(::HTTP::Tiny); $class->new'

    Was this change documented anywhere?

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 taking refuge in the Monastery: (5)
    As of 2020-09-25 11:35 GMT
    Find Nodes?
      Voting Booth?
      If at first I don’t succeed, I …

      Results (137 votes). Check out past polls.