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
Driving the Engine right off the road
3 direct replies — Read more / Contribute
by Intrepid
on Aug 22, 2025 at 09:51

    The Perl Regexp Engine of course. I have been working hard on working out how to formulate a regular expression with a negative lookbehind and I cannot get it right. What I have right now is this: /^(?<!XDG_) [_A-Z0-9]+ _PATH$/x

    I'll explain in english what I need. There will be desired matches with strings like PKG_CONFIG_PATH but I must not match XDG_SEAT_PATH or XDG_SESSION_PATH. Naturally, it is not as simple as avoiding any match with a string beginning with XDG_ because I want to match XDG_DATA_DIRS, for example. (Yes, these are well-recognized environment variables).

    This works for getting only PATH: /^(?<!XDG_)PATH$/, so I can see that the Engine does understand the lookbehind. Any suggestions will be appreciated!

    Aug 22, 2025 at 13:49 UTC

    A just machine to make big decisions
    Programmed by fellows (and gals) with compassion and vision
    We'll be clean when their work is done
    We'll be eternally free yes, and eternally young
    Donald Fagen —> I.G.Y.
    (Slightly modified for inclusiveness)

Valkey and Redis support
3 direct replies — Read more / Contribute
by hippo
on Aug 22, 2025 at 08:49

    Given the recent licensing hokey-cokey by Redis I'm inclined to use Valkey in preference. However, there isn't any explicit mention of Valkey on CPAN that I could find. This isn't a huge surprise given how relatively new it is but nor does it give me confidence about support for it now or in the future.

    So, does anyone have experience with using any of the various Redis modules with Valkey as the server? Is it as much of a drop-in replacement as I would hope?


    🦛

XML::Smart how to prevent encoding <body> tag
2 direct replies — Read more / Contribute
by zatlas1
on Aug 20, 2025 at 11:52

    I have XML that I manipulate and save.

    Among other tags, I may have a <body> tag with simple text in it. XML::Smart insists on encoding that in this way:

    <body dt:dt="binary.base64">

    which causes an error since dt is not defined and I am not interested in defining it.

    Is there a way to tell XML::Smart to NOT encode that tag.

    XML::Smart proves to be to smart in this case

    Thank you

    ZA

confusing result from Win32::GetACP()
4 direct replies — Read more / Contribute
by hexcoder
on Aug 20, 2025 at 08:29
    Hello dear fellow nuns and monks!

    While researching for a encoding problem, I wanted to retrieve the value of the current windows ANSI code page.
    This can be done e.g. with Powershell like this:

    Get-WinSystemLocale | Select-Object -ExpandProperty TextInfo | Select-Object -Property ANSICodePage
    which gives the following output:

    ANSICodePage ------------ 1252

    Instead of calling PowerShell and parsing its output, I tried to use Perl's own facilities.

    perl -MWin32 -we "print Win32::GetACP()"
    which however gave the following output:

    65001
    indicating an UTF8 ANSI code page instead of Western-Europe one (cp1252) from above.

    I am using Strawberry Perl 5.38.0 64-bit with its own W32 module running on Windows 11.

    Is this behavior understood and known? Did I miss something or could it be a bug?
    Thanks very much for your attention!

How to set the UTF8 flag?
4 direct replies — Read more / Contribute
by dissident
on Aug 17, 2025 at 22:31
    Somewhere in a script that has turned all UTF8 flags on, Perl seems to get confused.
    No matter what I try, it destroys the encoding when writing the data to a file, by writing two characters for every Unicode byte.
    Thus most Unicode characters become a 4-byte, 2-character near-random looking string.
    Could the UTF8 flag be not set where it should be set?

    As it is apparently impossible to reliably check the UTF8/Latin state of a string variable, the best solution might be just to set the UTF8 flag when the data is known good UTF8.
    So, is there a function or some reliable technique to set the UTF8 flag?

    Or could it maybe be a better idea in those cases to write the file as raw binary data instead, to circumvent unwanted conversions?

    Any other idea what to do?
Is it possible load optional modules from PERL5OPT?
3 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 17, 2025 at 00:02
    I have an internal module that I include in all my scripts. I have been treating it as an optional dependency by loading it like so BEGIN { eval { require Internal::Module } }. This allows me to share my scripts and not have to worry about the internal module not existing on the users' system. But now I'm thinking it would be easier to manage by removing the extra boilerplate and including the module via the env var like so: export PERL5OPT="-MInternal::Module". This only works if the module exists, but I also need it to work if it does not exist. I've been playing around with the if module but haven't been able to get it to work for this case. Is it possible to do what I want?
WWW::Mechanize::Chrome : random complaining that Log4perl is not initialised and test fails
1 direct reply — Read more / Contribute
by bliako
on Aug 11, 2025 at 16:54

    I see random complains like below when I instantiate a WWW::Mechanize::Chrome object:

    Log4perl: Seems like no initialization happened. Forgot to call init() +? 23:28:21 Sent 'Target.closeTarget' message $VAR1 = '{"id":11,"params": +{"targetId":"F75959D2137237798CF1FCB89E87FFAA"},"method":"Target.clos +eTarget"}'; 23:28:21 Can't call method "irand" on an undefined value at (eval 140) + line 17 during global destruction.

    As an example, consider the following test-script. For me, the below script sometimes passes and sometimes it fails (see below for when exactly), although all subtests succeed all the time.

    #!/usr/bin/env perl use Test::More; use WWW::Mechanize::Chrome; use Log::Log4perl qw(:easy); # This is for the mech obj, Set priority of root logger to ERROR Log::Log4perl->easy_init($ERROR); my %default_mech_params = ( headless => 1, # log => $mylogger, launch_arg => [ '--window-size=600x800', '--password-store=basic', # do not ask me for stupid chrome ac +count password # '--remote-debugging-port=9223', # '--enable-logging', # see also log above '--disable-gpu', '--no-sandbox', '--ignore-certificate-errors', '--disable-background-networking', '--disable-client-side-phishing-detection', '--disable-component-update', '--disable-hang-monitor', '--disable-save-password-bubble', '--disable-default-apps', '--disable-infobars', '--disable-popup-blocking', ], ); my $mech_obj = WWW::Mechanize::Chrome->new(%default_mech_params); ok(defined($mech_obj), "WWW::Mechanize::Chrome->new() : called.") or B +AIL_OUT("failed to create WWW::Mechanize::Chrome object"); done_testing();

    Here is what it says when I run make all && make test. Note that this fails all the time.

    Manifying 1 pod document PERL_DL_NONLAZY=1 "/usr/bin/perl" "-MExtUtils::Command::MM" "-MTest::H +arness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/l +ib', 'blib/arch')" t/*.t t/xx.t .. 1/? Log4perl: Seems like no initialization happened. Forgot +to call init()? 2025/08/11 23:40:43 Sent 'Target.closeTarget' message $VAR1 = '{"metho +d":"Target.closeTarget","id":11,"params":{"targetId":"FF3672A330F5788 +556F1B4B18CCC5610"}}'; 2025/08/11 23:40:43 Can't call method "irand" on an undefined value at + (eval 140) line 17 during global destruction. t/xx.t .. Dubious, test returned 255 (wstat 65280, 0xff00) All 1 subtests passed Test Summary Report ------------------- t/xx.t (Wstat: 65280 (exited 255) Tests: 1 Failed: 0) Non-zero exit status: 255 Files=1, Tests=1, 11 wallclock secs ( 0.03 usr 0.00 sys + 0.41 cusr + 0.12 csys = 0.56 CPU) Result: FAIL Failed 1/1 test programs. 0/1 subtests failed. make: *** [Makefile:873: test_dynamic] Error 255

    However, when I run make all && perl -Iblib/lib t/xx.t, sometimes it fails with above complaining and sometimes it succeeds.

    I use the latest W:M:C v0.73. It fails with perl v5.40.2. On another computer with perl v5.38.2 I see no failures (after 10 consecutive runs). Both running latest linux.

    Thanks, bw, bliako

    ps. my question is not urgent, don't stop swimming to answer it...

    ps2. OT but Corion does the dreadful Log4* abomination Log::Log4perl need to be used by WWW::Mechanize::Chrome?

Using system sort order
3 direct replies — Read more / Contribute
by sciurius
on Aug 11, 2025 at 05:59
    Dear monks,
    I want my perl program to sort just like the system it is running on.
    This is trivial on Linux, but somehow seems complicated on Windows and macOS. I use the following test program
    #! perl use strict; use warnings; use utf8; binmode STDOUT => ':utf8'; my @words = ( "Maria Lenore", "Mária Helena", "María Dolores" ); my @sorted = sort @words; if ( "@sorted" eq "María Dolores Mária Helena Maria Lenore" ) { print("Sorted OK\n"); } elsif ( "@sorted" eq "Maria Lenore María Dolores Mária Helena" ) { print("Sorted C (ASCII)\n"); } else { print("Sort failed: @sorted\n"); }
    On Windows, I get
    PS C:\Users\Johan> perl locale.pl Sorted C (ASCII) PS C:\Users\Johan> perl -Mlocale locale.pl Sorted C (ASCII) PS C:\Users\Johan>
    Same on macOS.

    When I create files with names "Maria Lenore", "Mária Helena" and "María Dolores" they sort correctly in the Explorer (Windows) and Finder (macOS).
    How can I have perl use the same sort order as the system?

    Windows 10 with Strawberry Perl 5.30
    MacOS 10.15 with Perl 5.34.
    These versions are a bit old, but I can not imagine I'm the first to try such a sort in 25+ years.
How to pass custom options to pod2man from Extutils::MakeMaker?
1 direct reply — Read more / Contribute
by nicholashubbard
on Aug 10, 2025 at 14:00
    With ExtUtils::MakeMaker, I would like to set the title of my manpage using pod2man's "--center" option. Unfortunately I cannot figure out how to do this. I have read through Makemakers doumentation but cannot find any mention of passing custom flags to pod2man. I noticed that in MakeMaker's outputted Makefile, there is a rule "manifypods" that handles creating the manpages with pod2man. However, I cannot figure out how to customize this rule. I noticed that pod2man is executed via a function "ExtUtils::Command::mm::pod2man, that does allow passing options, but I'm unsure of how I can gain control of this functions invocation. Does anybody know how I can do this? Does anybody know of any other projects that pass custom options to pod2man from ExtUtils::MakeMaker?
unpack Binary FAIL
2 direct replies — Read more / Contribute
by marinersk
on Aug 07, 2025 at 20:09

    I'm sure this is going to be really basic, but I've gone several decades using Perl and never needed the pack or unpack functions before.

    #!/usr/bin/perl use strict; use warnings; # my $pReqsca = "ABCD"; my @bytary = unpack 'C*', $pReqsca; + my $binstr = ''; + foreach my $scabyt (@bytary) { + my $intbyt = int($scabyt); + my $hexbyt = sprintf("%02X", $intbyt); + my $binbyt = unpack 'b8', $scabyt; + $binstr .= $binbyt; + print " Byte = [$scabyt] Integer = [$intbyt] Hex = [$hexbyt] B +inary = [$binbyt] Binary String = [$binstr]\n"; print "Aborting Loop\n"; last; }

    Results:

    W:\Steve\PerlMonks>perl sopw-584100.pl Byte = [65] Integer = [65] Hex = [41] Binary = [01101100] Binary + String = [01101100] Aborting Loop

    Same results using 'B8',
    Last time I checked, Hex 41 should show binary 01000001.

    What am I missing here?


Add your question
Title:
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, details, 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, summary, 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.