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
Problem Declaring a hash
3 direct replies — Read more / Contribute
by The_Evil_One
on Nov 05, 2025 at 14:39

    I am learning Perl using a book downloaded from the internet.

    Opal is a system for charging fares on public transport in NSW Australia. I have had four Opal cards and they identified by 16 digit numbers. I have four target directories into which the files related to these cards go and I have tried to set up a hash to map between card numbers and the destination directories but it does not work and the error messages make no sense to me.

    This is the current form of the hash definition which is at line 26 in the script:-

    my %OCN_PFX_To_TDir=( 3085220111379778 => '3085220111379788_Rexx', 3085220334620644 => '3085220334620644_Spare', 3085220346432582 => '3085220346432582_Rexx2', 3085220352420034 => '3085220352420034_Algol' ); This is the input followed by the error messages I get:- carl@md-tower-004:~$ perl ~/bin/copy_opal1.plx Can't modify constant item in scalar assignment at /home/carl/bin/copy +_opal1.plx line 26, near ");" Execution of /home/carl/bin/copy_opal1.plx aborted due to compilation +errors (#1) (F) You aren't allowed to assign to the item indicated, or otherwi +se try to change it, such as with an auto-increment. Uncaught exception from user code: Can't modify constant item in scalar assignment at /home/carl/bin/ +copy_opal1.plx line 26, near ");" Execution of /home/carl/bin/copy_opal1.plx aborted due to compilat +ion errors.

    I am writing a small program to copy Opal related files from my downloads directory to the appropriate destination directories.

    Basically it tells me that I am trying to assign a scalar value to the % sign representing the modulus operator which cannot receive such a value but the % sign is to mark the variable as being a hash. I have tried various changes on the definition, originally I had both keys and values in double quotes now I have the values in single quotes, originally I used commas to separate key value pairs. No matter what I change the error messages remain the same.

    It may be that the book I downloaded is old and some syntax suggested in the book no longer works but no matter how many queries I have put into Google and how many AI overviews I have seen and hit list links that I have followed nothing has confirmed this.

Fetching 'http://search.cpan.org/uploads.rdf' (from cpan) LibXML error is triggered
3 direct replies — Read more / Contribute
by Intrepid
on Nov 05, 2025 at 13:09

    Hello Monks and Nuns. I was working with cpan (the installer) on CygPerl today and tried the command recent; it didn't work and this is what I saw:

    
    cpan2> recent
    CPAN: XML::LibXML loaded ok (v2.0210)
    Fetching 'http://search.cpan.org/uploads.rdf'
    CPAN: LWP loaded ok (v6.80)
    DONE
    
    Catching error: "XML::LibXML::Error=HASH(0xa01ebe148)" at /usr/local/share/perl5/site_perl/5.40/CPAN.pm line 397.
            CPAN::shell() called at -e line 1
    

    Could anyone else try this and see what happens? I'd appreciate it.

    My system setup is:

    This is perl 5, version 40, subversion 3 (v5.40.3) built for x86_64-cygwin-threads-multi
    /usr/lib/perl5/vendor_perl/5.40/x86_64-cygwin-threads/XML/LibXML.pm
    XML::LibXML version 2.0210
    CPAN        version 2.38
    @INC:
    /usr/local/lib/perl5/site_perl/5.40/x86_64-cygwin-threads
    /usr/local/share/perl5/site_perl/5.40
    /usr/lib/perl5/vendor_perl/5.40/x86_64-cygwin-threads
    /usr/share/perl5/vendor_perl/5.40
    /usr/lib/perl5/5.40/x86_64-cygwin-threads
    /usr/share/perl5/5.40
    
    Nov 05, 2025 at 18:08 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)

Making DBI with SQLite3 truly read-only
1 direct reply — Read more / Contribute
by mldvx4
on Nov 04, 2025 at 09:25

    Greetings, wise monks,

    I am trying to crank down restrictions on an FCGI script. I have DBI opening an SQLite3 database as read-only. This is how I am currently connecting to the database:

    my $dbh = DBI->connect("dbi:SQLite:dbname=$database", undef, undef, { AutoCommit => 0, RaiseError => 1, on_connect_do => "PRAGMA foreign_keys = ON", sqlite_open_flags => "DBD::SQLite::OPEN_READONLY", }) or die("Could not open database '$database': $!\n");

    However, AppArmor still gives the following error about needing write access, even though the error and restrictions causing it do not impair the script from appearing to work as expected:

    2025-11-04T14:02:51.130580+00:00 ID28399 kernel: [5084204.347595] audi +t: type=1400 audit(1762264971.124:63): apparmor="DENIED" operation="o +pen" profile="/var/www/fcgi/foo.fcgi" name="/var/www/db/foo.sqlite3" +pid=377569 comm="search.fcgi" requested_mask="wc" denied_mask="wc" fs +uid=1002 ouid=1002

    and in the AppArmor profile:

    /var/www/db/foo.sqlite3 kr,

    If I add w to that, the errors go away. However, I don't want to do that because the script's access is supposed to remain read-only. Yet it appears that DBI is somehow still requesting some kind of write access, even if it does not use it. What can I change in the DBI connection to ensure that it is purely read-only as far as the database goes?

Latest Strawberries: addresses reported by "p", Devel::Peek, etc.
3 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 30, 2025 at 18:59
    use strict; use Devel::Peek; $Devel::Peek::pv_limit = 3; use warnings; printf "$^V\n"; my $s = 's' x 32; Dump $s; my $addr = pack 'p', $s; printf "Q: hex: %x, dec: %1\$d\n", unpack 'Q', $addr; Dump $s; __END__ v5.32.1 SV = PV(0x1fbf48) at 0x24f0e08 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x2565cb8 "sss"...\0 CUR = 32 LEN = 34 Q: hex: 2565cb8, dec: 39214264 SV = PV(0x1fbf48) at 0x24f0e08 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x2565cb8 "sss"...\0 CUR = 32 LEN = 34 v5.42.0 SV = PV(0x1330f20f4c0) at 0x1330f2836a0 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x1330f3129f0 "sss"...\0 CUR = 32 LEN = 40 COW_REFCNT = 1 Q: hex: 1330f312400, dec: 1318809838592 SV = PV(0x1330f20f4c0) at 0x1330f2836a0 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1330f312400 "sss"...\0 CUR = 32 LEN = 34

    Previously (5.32), there was consistency as expected: PV address was "real" RAM address; and simple fact of "packing to pointer" didn't change scalar' guts. Now, I see some weird huge ("virtual"?) addresses -- even though everything works (including, not shown above, accessing (or acquiring, same values) these pointers from C). Where such numbers come from, is this the new normal? Is difference in "Dump" output related to that, or totally another issue?

Catching error in DateTime::Format
4 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 27, 2025 at 13:29

    I'm an inexperienced Perl programmer (my background is mainly in Python), charged with keeping a legacy app running, and I'm having trouble figuring out how to validate a date. (At least, that's what I think the problem is.)

    We have a function that takes a date value and does some transformations on it. This is thoroughly tested and works fine for its use case, but we don't actually have a test for what happens if someone sends garbage into it—it assumes the input really is a valid date. Normally, this is fine; the input comes from internal sources and should always be OK. We now need to extend it for a case when we don't have control over the incoming data. So, I tried to wrap it in an eval to catch any errors. Currently, it looks like this:

    # in MyApp::Util sub convert_datetime { my ( $self, $incoming_datetime ) = @_; my $new_dt; eval { $new_dt = DateTime::Format::ISO8601->parse_datetime( $incom +ing_datetime ); }; return undef if $@; # No error, now we can convert the date (not shown) }
    The test script now has this:
    my $bogus_datetime = MyApp::Util->convert_datetime("FOO"); is ($bogus_datetime, undef, "convert_datetime doesn't blow up if sent +bogus date");
    When I run this, however, I get (I've changed the file paths):
    ok 141 - convert_datetime doesn't blow up if sent bogus date Invalid date format: at /path/to/MyApp/Util.pm line 818. eval {...} called at /path/to/MyApp/Util.pm line 818 MyApp::Util::convert_datetime("MyApp::Util", "FOO") called at t/Ut +il.t line 297 ...propagated at t/Util.t line 298.

    So the test "works" in that it returns an "ok" value, but it also prints the whole "Invalid date format" thing. How do I get it to _not_ do this, or otherwise test to see if the input is valid before I pass it to DateTime::Format::ISO8601->parse_datetime? The docs for this function say "The parse_datetime method will attempt to match these ambiguous strings" etc., but doesn't say what happens if it can't, or what you're supposed to do to test for valid input.

    I tried to write a test script, but even though it looks identical (to me), this does _not_ print the "Invalid date format":

    #!/usr/bin/env perl use strict; use warnings; use DateTime::Format::ISO8601; eval {my $foo = DateTime::Format::ISO8601->parse_datetime("FOO");}; print "ERROR\n" if $@;

    Running this simply prints "ERROR" and that's it, no other output. What's going on in the real code, or what's the right way to handle this?

Oneliner replace in-place adds extra spaces at the end of the file
1 direct reply — Read more / Contribute
by bliako
on Oct 26, 2025 at 16:04

    Wise Monks,

    I am doing an in-place replacement in a text file which succeeds but one (?) extra newline is added at the end of the file:

    The input file (a):

    1 2 3

    The command:

    perl -i -lp0e 's/2/4/gs' a

    Output

    1 4 3 # there is an extra newline at the end, above

    Looping the above command (the replacement is done only the first time), will yield as many newlines at the end:

    for i in $(seq 1 10); do perl -i.x -lp0e 's/2/4/gs' a; done
    @@@@

    bw, bliako

Why dot does not match newline in oneliner but it does in script, with /s modifier?
1 direct reply — Read more / Contribute
by bliako
on Oct 24, 2025 at 10:10

    Dear Monks,

    I can not understand why this works:

    my $x = <<'EOX'; A { B 123 C xyz } EOX $x =~ s/A\s*\{.+?\}//s; print "x='$x'\n"; # empty

    but this does not (linux terminal):

    perl -pe 's/A\s*\{.+?\}//s' <<'EOX' A { B 123 C xyz } EOX # prints exactly what the input is

    I have checked if there are mistaken shell interpolations to the oneliner script, but there are not, I think:

    x='s/A\s*\{.+?\}//s' echo $x # s/A\s*\{.+?\}//s

    Edit: the same problem if the oneliner operates on a file:

    cat > infile<<'EOX' A { B 123 C xyz } EOX perl -pe 's/A\s*\{.+?\}//s' infile # or perl -i -pe 's/A\s*\{.+?\}//s' infile
Net::DNS::Paranoid and IPv6
1 direct reply — Read more / Contribute
by Corion
on Oct 23, 2025 at 03:57

    Is there any IPv6-capable "paranoid" DNS resolver like Net::DNS::Paranoid, except with support for IPv6?

    I'm currently writing a "link preview" fetcher, using Mojo::UserAgent. As this preview fetcher mildly implies fetching user-generated and/or user-controlled links (even though the application is primarily for myself), I'm looking for something like LWPx::ParanoidAgent / LWP::UserAgent::Paranoid / HTTP::Tiny::Paranoid, but for Mojolicious.

    I have already resolved (hah!) myself to writing Mojo::UserAgent::Paranoid, which delegates (as they all do) the DNS resolution to Net::DNS::Paranoid, a DNS resolver class that blocks hosts and IP addresses that are unlikely URLs for external services, especially localhost and other internal networks (192.168.*, 10.* and the multicast addresses).

    This is all fine and good, if it weren't for IPv6. Net::DNS::Paranoid happily lets ::1 through, as the code mostly looks at IPv4 addresses and its DNS resolution also only speaks A records, not AAAA.

    Simply adding ::1 (localhost in IPv6-speak) to the list of blocked hosts is a good start, but I would also want to block the multicast and link-local IPv6 addresses and some others. So, I'm mildly looking towards extending Net::DNS::Paranoid to also support IPv6, but maybe somebody has already done the work, or has links to an existing implementation in another language where I can crib the pitfalls and footguns from.

On which portable Strawberry Perl will Tk install?
1 direct reply — Read more / Contribute
by aplonis
on Oct 17, 2025 at 22:29
    Might anyone know which archived version of portable Strawberry Perl, installable on Windows 11, will accept installation of Tk without jumping through patching hoops?
X11::GUITest and Umlauts
1 direct reply — Read more / Contribute
by LanX
on Oct 16, 2025 at 20:04
    Hi

    OS=Linux

    I'm struggling to send special characters like umlauts ("äöüÄÖÜ") via X11::GUITest and SendKeys() to another application.

    Is there a trick I am missing? Anything after the first special character is discarded.

    If I try the linux tool xte from the package 'xautomation', it works fine from the console:

    gnome-text-editor & sleep 1; xte "str äöüÄÖÜß"

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery


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.