Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Seekers of Perl Wisdom

( [id://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.

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.

Post a new question!

User Questions
Perl 5.36 warns for magic comparison
3 direct replies — Read more / Contribute
by hurricup
on Oct 15, 2023 at 06:34
    According to the perldebguts:
    Each array @{"_<$filename"} holds the lines of $filename for a file compiled by Perl. The same is also true for evaled strings that contain subroutines, or which are currently being executed. The $filename for evaled strings looks like (eval 34).
    Values in this array are magical in numeric context: they compare equal to zero only if the line is not breakable.
    But perl 5.36 warns in my debugger as: Argument "use v5.36;\n" isn't numeric in numeric eq (==) at
    There is a simple comparison: if $lines[$lineno] == 0
    Do I do something wrong?
bignum usage?
3 direct replies — Read more / Contribute
by rkd257
on Oct 14, 2023 at 23:44

    I must be using bignum incorrectly, as I would have thought I would end up with 5 and 7 on the bottom 2 lines.


    use Math::BigRat; use bignum; my $cf = 1 - 1/6 - 1/4; ## $cf = 7/12 + \ + my $n = $cf->numerator(); my $d = $cf->denominator(); print "$cf\n"; print " $n\n"; print " $d\n";
Can two separate responses be sent to the client's browser from Perl, such as via fork{}?
3 direct replies — Read more / Contribute
by Polyglot
on Oct 14, 2023 at 23:01
    I want to send a file to be downloaded via a "Save as..." type dialog in the client's browser. This requires a special set of HTTP headers. However, I would also like to update the requesting webpage at the same time, AJAX-style. Unfortunately, the two sets of headers must be different, and are not mutually compatible. My attempts so far have simply broken the file download, which is otherwise working.

    Can I use a fork { ... } type of command to send back the file while still updating the webpage via AJAX? If so, how is this done? Which response should go back to the browser first, or does fork { ... } end up creating a race condition?

    I'm not sure where to start looking for the direction to go with this, so any coding suggestions are welcome. Please note that I am not looking for a package--I have the downloading working already, and have learned to program the AJAX myself. I'm just needing to know how, if this is possible, to do both at the same time.



how Mojo::ua work with Net::Stomp
1 direct reply — Read more / Contribute
by Anonymous Monk
on Oct 14, 2023 at 04:41
    I've an Mojo Useragent to connect a system to do regular tasks, and it works fine by far. Recently our R&D team introduce Stomp to do some long-live stuff. Since they use Java & JS, I have to implement it in perl by myself. Below is some example code in JS:
    var url = "ws://localhost:61614/stomp"; var client = Stomp.client(url); function afterConnect(roomid) { btn.addEventListener('click', function () { var msg = input.value; client.send(roomid, {}, msg); }, false); } function createConnect(roomid, uid) { client.connect(headers, function (error) { if (error.command == "ERROR") { console.error(error.headers.message); } else { afterConnect(roomid); client.subscribe(uid, function (msg) { var body = msg.body; if (msg.headers['content-type'] == 'application/json') { body = JSON.parse(msg.body) } }); } }); } ...... ......
    The logic is simple, a local stomp client connect to server over websocket, then get data, modify data save data etc. What I confuse is, I've searched Stomp on cpan, all modules about Stomp does not support WS, The Mojo::ua support WS (and async which why I prefer it), but doesn't support Stomp Protocol directly. Before I dig into Stomp protocol, is there a decent way to combine Mojo::ua and Stomp(Like Net::Stomp)? Thanks.
Is it possible at all to flush buffer and commit file write, if UTF-8 encoding is being used??
4 direct replies — Read more / Contribute
by dissident
on Oct 12, 2023 at 20:15

    I was trying unsuccessfully half the day to find out how to make Perl flush and commit write of a logfile, for showing the progress on a web page.

    This is my code:

    sub printtolog { my $txt = shift; my $logf; $txt = getdirdatetag() . ': ' . $txt . "\n"; # see open($logf, ">>:utf8", $logging_logfile) || die "$0: can't open $logging_logfile for appending: $!"; # see # this does not work # my $oldfh = select $logf; # $| = 1; # select($oldfh); # this also does not work # $logf->autoflush(1); print $logf $txt; # this does not work # $logf->flush; # $logf->sync; # this also does not work # $select()->flush(); # $select()->sync(); close $logf; }

    Finally, I found a comment on StackOverflow, saying that the UTF-8 encoding layer adds another buffering layer.
    (See the discussion between Ikegami and Ωmega on

    Is this the reason why the logfile only gets created and written after either writing some kilobytes of many messages, or when the program much later terminates?

    Does there exist a way to circumvent this buffering and to force creation and flushing of the file at all?

GitHub perl-ci.yml
3 direct replies — Read more / Contribute
by Bod
on Oct 12, 2023 at 19:36

    Whilst attempting to test a previous module, pryrt very helpfully introduced me to GitHub Continuous Integration.

    I tried using this when I released WWW::Crawl and got lots of test configurations to pass, but others failed. When the tests ran again, different configurations passed and different configurations failed!

    From this test run it seems much of the problem is from the $VERSION string. But this inconsistency is not reflected in the CPAN Testers results.

    Is this an anomaly of GitHub Continuous Integration or have I got something wrong in the per-ci.yml file?

Increase verbosity of "make test"
3 direct replies — Read more / Contribute
by bliako
on Oct 11, 2023 at 03:04

    Dear Monks,

    I am trying to investigate failed tests which occur on some CPAN testers' systems but not in mine.

    Without excluding other suggestions, I would like to make the command make test more verbose by not suppressing diag and STDERR output for each test. Right now, running make test I get:

    ... t/11-scripts-pod.t .............................. ok t/12-from-file.t ................................ ... Test Summary Report ------------------- t/12-from-file.t (Wstat: 9 Tests: 11 Fail +ed: 0) Non-zero wait status: 9 Parse errors: No plan found in TAP output

    So, I would like to know what's going in t/12-from-file.t and where exactly it gets KILL'ed. Alas the output is opaque.

    I have read here that running make test TEST_VERBOSE=1 will do exactly what I want.

    The problem is I don't know how to tell CPAN testers to run make test TEST_VERBOSE=1 (and not the default make test).

    The furthest I got was to add ANOTHER make test target in the produced Makefile, with increased verbosity flags, to be run after the usual make test via the MY::postamble technique (using ExtUtils::MakeMaker, e.g. see Re: Benchmarks target in Makefile). The caveat is that I am not sure it will go ahead if previous Makefile target has failed. I think it does not.

    Is there a solution to this?

    bw, bliako

PAR-Packer and IPC-Run
3 direct replies — Read more / Contribute
by izomiac
on Oct 10, 2023 at 07:38
    I'm trying to produce an exe version of a script I've written and have run into trouble with the IPC::Run module. Both PAR::Packer and Perl2Exe produce errors with the code below:
    #!/usr/bin/perl #perl2exe_include ""; use strict; use warnings; $|=1; use Symbol qw( gensym ); use IPC::Run qw( start ); use Tie::Handle; use IPC::Run::Win32IO; my %sp; $sp{'STDIN'} = gensym(); $sp{'STDOUT'} = gensym(); $sp{'STDERR'} = gensym(); my $h = start ['./cnc.exe'], '<pipe', $sp{'STDIN'}, '>pipe', $sp{'STDOUT'}, '2>pipe', $sp{'STDERR'} or die "returned $?"; sleep 20;
    PAR::Packer generates the following when I attempt to run the resulting .exe:
    Inappropriate I/O control operation: Win32::Process::Create() at C:\Us +ers\Izomiac\AppData\Local\Temp\par-4a6f7368\cache-6146a0a42031e00189f +f49a355f90de7875866db\inc\lib/IPC/ line 2143. Inappropriate I/O control operation: Win32::Process::Create() at C:\Us +ers\Izomiac\AppData\Local\Temp\par-4a6f7368\cache-6146a0a42031e00189f +f49a355f90de7875866db\inc\lib/IPC/ line 2244. Inappropriate I/O control operation: Win32::Process::Create() at C:\Us +ers\Izomiac\AppData\Local\Temp\par-4a6f7368\cache-6146a0a42031e00189f +f49a355f90de7875866db\inc\lib/IPC/ line 2244.

    And Perl2Exe runs the .exe version dozens of times simultaneously.

    For context, this script launches a half-dozen other scripts and coordinates communication between them by piping things through STDIN/STDOUT. I adopted this approach since one subprocess is a Tk GUI, another loads gigabytes of data into memory, and another is a 32 thread genetic sorting algorithm, and combining them into one process caused all sorts of instability. This version is faster and much more stable, but it's a big ask to have end-users install perl.

    Any suggestions or alternatives?

Extracting /regex/mg in a while loop
3 direct replies — Read more / Contribute
by NetWallah
on Oct 09, 2023 at 17:53
    Esteemed Monks:

    I'm looking for some recommendations/best practice/elegant solutions for the following (This code already produces the right results).
    I was surprised by the fact that Method#2 (My original code) is SLOWER than Method#1.

    use strict; use warnings; my $x= <<"__X__" x 4; # Increase multiplier for benchmarking A data for A B data for b C data for c __X__ # Method #1 - works but I don't like using $1,$2 - would rather use na +mes while($x=~/(^A|^B)(.+)$/mg){ print "$1 method1 $2\n" } # Method #2 - open my $f,"<",\$x or die $!; while(<$f>){ my ($name,$data) = m/(^A|^B)(.+)$/ or next; print "$name method2 $data\n" } close $f; # Method #3 (infinite loop) #while(my ($name,$data)=$x=~/(^A|^B)(.+)$/mg){ # print "$name method3 $data\n" #}

                    "These opinions are my own, though for a small fee they be yours too."

How to disable taint checking by Perl?
2 direct replies — Read more / Contribute
by dissident
on Oct 09, 2023 at 06:31

    The perlsec section about Taint mode ( begins with a statement
    "By default, Perl automatically enables a set of special security checks, called taint mode".

    So the question arises:
    How to completely turn off these default-enabled "special security checks" which are enabled even without the -T option?
    Or: How to completely disable the said "taint mode"?

    There are several legit reasons to do so:
    - To find out quickly whether these "security checks" are what causes your script to not work.
    - When not needed at all in the given scenario.
    - To make Perl more efficient, runtime-wise and energy-wise.
       These checks adds a runtime overhead of ~10% (see
       Thus, in many scenarios this checking is not only utterly unnecessary, but actually detrimental.

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":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.