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
can u suggest a regex for this ?
4 direct replies — Read more / Contribute
by misterperl
on Feb 03, 2023 at 09:34
    I often have requests to comment out BLOCKS of text, such as "comment out everything between and including each cat/^cat block. Example:
    mouse cat 1 2 3 ^cat deer eel cat furbaby elk ^cat dog fish
    the result I want is
    mouse #cat #1 #2 #3 #^cat deer eel #cat #furbaby #elk #^cat dog fish
    This can be in an array (one element per line) or a scalar. Usually I just loop thru the list and set a flag when hit cat, and unset it after leaving ^cat, and add the # if flagged. Which is like how I would do this in C. Every time I solve a problem in my brain in C, I always think to myself surely Perl has a better approach!.. Fugly code:
    my $comment=0; for (@a) { $comment++ if /^cat\b/; $_ = '#'.$_ if $comment; $comment-- if /^\#\^cat\b/; }

    Can you suggest a brief solution with a single no-looping regex, OR a map for an array? Or maybe a clever mapped split?

    Much thanks and happy Day of the Earth-Rodent-Forecaster!

Something wrong with
3 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 02, 2023 at 20:38
    I've noticed that some modules, which do exist, are sometimes shown as "not found" on If you reload the page it will kinda alternate between found and not found. Here is one such module:


    Some of the modules in that distribution are always found, like Astro::Montenbruck::Ephemeris

    Others kinda alternate between found and not found, like Astro::Montenbruck::CoCo

    What's going on at metacpan?

Why does system("mv $x $y") not work when it works at the command line?
6 direct replies — Read more / Contribute
by Cody Fendant
on Feb 02, 2023 at 20:18

    This is a simple script which sorts TV show episodes into the right folders, it comes down to this basic example:

    ### I run this from the command line: /usr/bin/perl The.Rookie.S05E14.720p.HEVC.x265-MeGusta[eztv.r +e].mkv

    The script dies here:

    unless ( -e $ARGV[0] ) { die("$ARGV[0] doesn't exist"); }


    • The file does exist: it shows up if I do ls The.Rookie.S05E14.720p.HEVC.x265-MeGusta[].mkv
    • The script is operating in the right folder, I double checked

    If I skip the file-exists test and go right to moving the file to the correct folder, I can do this:

    say "move command: 'mv $ARGV[0] ./r/'"; system("mv $ARGV[0] ./r/");

    The mv command in the system() call fails with "No such file or directory", but the exact same command, if I copy and paste it from the perl script's output and hit enter, works just fine.

    Is it the brackets in the filename? I tried escaping them and it make no difference. The only wrinkle I can think of is that this is happening on MacOS, but the files in question are on a share from a Linux NAS, could that be causing issues?

Multiple file handles
3 direct replies — Read more / Contribute
by MoodyDreams999
on Feb 02, 2023 at 11:00
    Hey, Monks, I've never done this before, besides referencing from a database in sql to perl. I have a file that will have missing data that I am parsing, I want to populate the State based off the zip codes, so I created spreadsheet with like 80000 zip codes and they state abbr. Could you please give me some guidance on using one file to reference zip codes and based on those to update the state on another external file.
octal number mysteriously changes after pass to subroutine
2 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 01, 2023 at 13:56
    Dear Monks, I'm passing an octal file mode to a sub like sub(0666) but in the sub I'm seeing it as 438, and 0777 becomes 511. Quoting the mode causes it to lose the leading zero so that sub(q(0666)) becomes 666. I can put the leading 0 back and continue but that feels messy. Why is 0666 changing to 438? Thank you!
Parsing error
5 direct replies — Read more / Contribute
by MoodyDreams999
on Jan 30, 2023 at 13:01
    I keep running into an uninitialized error that doesnt make sense to me, I should be writing the state in column 6, but I keep getting Use of uninitialized value $t6 in string eq at line 48, <FH> line 2551. Use of uninitialized value $t6 in string eq at line 42, <FH> line 2552. Used perl c- it runs, says syntax is okay I tried declaring $t in line 35, "$worksheet->write($rowCount1+1, $_, my $t$_);" Perl C- said that was okay, but I get "syntax error at line 37, near "$t[""
    use Excel::Writer::XLSX; use strict; use warnings; my $rowCount = 0; my $filename = "Output2022.xlsx"; my $workbook = Excel::Writer::XLSX->new( $filename ); open(FH, "<", "SRC185.xlsx" ) or die; my $worksheet = $workbook->add_worksheet('List'); $worksheet->write(0, 0, "source_id" ); $worksheet->write(0, 1, "first_name" ); $worksheet->write(0, 2, "middle" ); $worksheet->write(0, 3, "last_name" ); $worksheet->write(0, 4, "address1" ); $worksheet->write(0, 5, "city"); $worksheet->write(0, 6, "state"); $worksheet->write(0, 7, "postal_code"); $worksheet->write(0, 8, "phone_number"); $worksheet->write(0, 9, "address3"); $worksheet->write(0, 10,"province"); $worksheet->write(0, 11, "email"); my $rowCount1 = 0; my $t=0; my @z = 90005; while (<FH>){ chomp; my @t=(',',$_); if(defined($t[8])){ my $Count=0; $worksheet->write($rowCount1+1, $_, $t[$_]); $Count++; } elsif($t[6] eq "CA" && $t[7] eq !defined) { $worksheet->write($rowCount1+1, 7, $z[7]); } $rowCount1++; } $workbook->close(); close(FH);
Seeking a way to access a network user's ID
2 direct replies — Read more / Contribute
by TorontoJim
on Jan 29, 2023 at 06:56
    Can a lady or gentleman please help me find the correct resource. I am looking for information that I can use in an app I'm building.

    It will be run on a LAN behind a firewall. It would be far more convenient if the user could be logged in automatically if they are already logged in to the network it is running on. So, I'm seeking information on accessing a network users's userid and login status. I'm not looking for specific code, just guidance to the proper keywords or terms that I need to research to achieve this end goal, if it is even possible.

    I don't mind doing the legwork, I just don't know what the specific legwork is that I must do right now.

Why don't some CPAN modules append perllocal?
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 28, 2023 at 21:15
    perl -MExtUtils::Installed -le '$_=ExtUtils::Installed->new->modules;p +rint'
    perl -le '@_=grep/Module/,split/\n/,`perldoc -uT perllocal`;print scal +ar@_'
    perl -MExtUtils::Installed -le '@_=ExtUtils::Installed->new->modules;p +rint for sort{lc$a cmp lc$b}@_' >mod.ext perl -e '@_=grep/Module/,`perldoc -uT perllocal`;for(@_){s/.*?\|([^>]+ +)>/$1/}print for sort{lc$a cmp lc$b} @_' >mod.loc diff -y mod.ext mod.loc

    diff -y mod.ext mod.loc|grep '<'|wc -l
    diff -y mod.ext mod.loc|grep '>'|wc -l
What to test in a new module
3 direct replies — Read more / Contribute
by Bod
on Jan 28, 2023 at 17:16

    I've created a helper function for my own purposes and thought it would be useful to others. So CPAN seems a sensible place to put it so others can use it if they want to...

    It's function is simple - to go to the homepage of a website and return an array of URI's within that site, being careful not to stray outside it, that use the http or https scheme. It ignores things that aren't plain text or that it cannot parse such as PDFs or CSS files but includes Javascript files as links (thing like or document.location.href) might be lurking there. It deliberately doesn't try to follow the action attribute of a form as that is probably meaningless without the form data.

    As the Monastery has taught be that all published modules should have tests, I want to do it probably and provide those tests...

    But, given that there is only one function and it makes HTTP requests, what should I test?

    The obvious (to me) test is that it returns the right number of URIs from a website. But that number will likely change over time, so I cannot hardcode the 'right' answer into the tests. So beyond the necessary dependencies and their versions, I'd like some ideas of what should be in the tests, please.

    In case you're interested, this came about from wanting to automate producing and installing sitemap files.

Any reference about %SIG
3 direct replies — Read more / Contribute
by exilepanda
on Jan 28, 2023 at 03:56
    I want to study more on SIGNAL. But after half hour digging, there's nothing useful. I am looking some full documentation that explains

    $SIG{__DIE__} will fire when die() happen
    $SIG{INT} will fire when Ctrl-C pressed

    Is that anything like this exists?

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