Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

The Monastery Gates

( #131=superdoc: print w/replies, xml ) Need Help??

If you're new here please read PerlMonks FAQ
and Create a new user.

poll ideas quest 2022
Starts at: Jan 01, 2022 at 00:00
Ends at: Dec 31, 2022 at 23:59
Current Status: Active
4 replies by pollsters
    First, read How do I create a Poll?. Then suggest your poll here. Complete ideas are more likely to be used.

    Note that links may be used in choices but not in the title.

Perl News
Stackoverflow blog: Why Perl is still relevant in 2022
on Jul 07, 2022 at 11:28
2 replies by NetWallah
    Girish Venkatachalam has blogged "Why Perl is still relevant in 2022" on July 6, 2022.

    No new info there - it is interesting only because it purports to be positive for perl, is published on SO, and showed up on my Google news feed.

    The author seems to have somewhat dated knowledge of perl and no knowledge of raku.

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

Admins for RT
on Jul 05, 2022 at 15:02
1 reply by hippo

    TPF is calling for volunteers to assist with the administration of, specifically to help with keeping it free from spam. If you have the necessary time, skill and inclination please consider supporting this.


Strawberry Perl can't find a module installed at a non-standard location
1 direct reply — Read more / Contribute
by LittleJack
on Aug 07, 2022 at 20:04

    I need to test what happens when a module is installed at a non-standard location.

    So I installed HTML::Template using:

    cpanm -l "C:/Users/Me/SecretModules" HTML::Template

    And installation was successful.

    When I go to use it (from Eclipse), like this:

    Package::DummyModule; use lib 'C:/Users/Me/SecretModules/lib/perl5'; use Text::HTML;

    It says that it can't find the module:

    Can't locate Text/ in @INC (you may need to install the Text::H +TML module) (@INC contains: C:/Users/Me/SecretModules/lib/perl5/MSWin +32-x64-multi-thread [etc]

    So I'm confused. The module is installed at C:/Users/Me/SecretModules/lib/perl5 and there's a packlist file in C:/Users/Me/SecretModules/lib/perl5/MSWin32-x64-multi-thread/auto but why can't Strawberry Perl find the module? Why does it automatically append the multi-thread directory? Can I install in such a way that it's non-multi-thread compatible?


Why does eof have a prototype?
1 direct reply — Read more / Contribute
by LanX
on Aug 07, 2022 at 12:57

    I'm trying to parse the usage infos in the pod of perlfunc to create code snippets, and am getting surprised by idiosyncrasies.

    For instance is eof magic when called with empty brackets eof()

    but has a prototype

    $ perl print prototype 'CORE::eof',"\n" __END__ ;* $

    See also perlsub

    > Note however that some built-ins can't have their syntax expressed by a prototype (such as system or chomp). If you override them you won't be able to fully mimic their original syntax.

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

Immediately writing the results of search-and-replace
2 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 05, 2022 at 13:56
    Most gracious monks: I am a Perl novice working on a script that takes a CSV file as input, uses the first element of each row as a string to search on, and the second element of each row as the replacement string. In the event that one search term appears in multiple rows, these are merged together so that the second element becomes an array of replacement candidates, reduced down to unique items. In either case, the user is prompted to adjudicate the replacement (either yes/no for a single replacement candidate, or a numeric input for multiple). I have the core functionality working, but some problems crop up when doing replacements on search strings for which other search strings are substrings.

    For example, if my script finds that a file contains the search string 'IP whitelist' on a given line, even if I confirm that I want to replace this with 'IP access list', it will then prompt me to replace just 'whitelist' with one of its various candidates. What I would expect to happen in that case is that the replacement of 'IP whitelist' with 'IP access list' happens before the search for the 'whitelist' key is initiated, preventing it from finding a match there. Doing some digging, it seemed that setting up autoflushing would solve my problem, but either I've misunderstood and that is not a solution to the situation, or I have implemented it incorrectly. Here is the subroutine that performs the actual search-and-replace:
    sub search_and_replace { open my $target_file, "<", $_; $| = 1; my $filename = $_; while (my $target_string = <$target_file>) { for my $row (@table) { my $search = $row->[0]; my $replacement = $row->[1]; if ((lc $target_string) =~ (lc $search)) { print "Found $search in $filename in the following context +:\n"; print "$target_string\n"; if (ref($replacement) eq 'ARRAY' && length $replacement > +1) { print "Choose a replacement candidate by typing the appr +opriate number, or else type '0' to skip.\n"; my $count = 1; for my $value (@$replacement) { print "$count\) $value\n"; $count++; } my $choice = <STDIN>; if ($choice >= 1 && $choice <= (length $replacement)) { my $replace_choice = $replacement->[$choice]; edit_file { s/$search/$replace_choice/gi} $filename; } else { print "Skipping this occurrence without replacing.\n"; } } else { print "Confirm replacement with $replacement by typing y +, or skip by typing any other key.\n"; my $choice = <STDIN>; if ($choice eq 'y' || 'Y') { edit_file { s/$search/$replacement/gi } $filename; } else { print "Skipping this occurrence without replacing.\n"; } } } } } close $target_file; }
    1) I was under the impression that autoflushing on the currently open filehandle can be set using the $| variable, but if that's the case, why doesn't this have any affect?
    2) Is refactoring this subroutine so that it loops over the rows of the tables first, and then over each file in the inner loop a better solution? It seems like a lot more IO to be opening and closing every file over and over for each term, but I'm not a real programmer by any stretch, so I could be way off the mark here.
    3) I also tried putting the line $target_file->flush; as the last line of each of the replacement 'if' statements, and that did nothing either.

    Please let me know if there is any additional info I can provide that would help. Thank you very much.
XS Error: Segfault with B::HooksAtRuntime
2 direct replies — Read more / Contribute
by Ovid
on Aug 05, 2022 at 06:01

    My module, MooseX::Extended is quickly becoming popular and is now being used in production at some companies. However, one person is reporting intermittent segfaults. This appears to be related to my using B::Hooks::AtRuntime to avoid the need to add __PACKAGE__->meta->>make_immutable; to the end of every Moose module. There's not much XS code involved, but my XS knowledge is even worse than my C.

    Paul "LeoNerd" Evans commented on IRC:

    14:18 LeoNerd: #0 Perl_SvREFCNT_dec_NN (sv=0xa65636e6174736e, my_perl=0x55921df002a0) at inline.h:242 <== that looks very much like a bad sv address
    14:20 LeoNerd: Not terribly clear where that comes from.. the next context frame is popeval, which suggests stack unwind. Possibly at this point some accessing of bad memory
    14:20 LeoNerd: valgrind might help.

    Can anyone with XS knowledge help me? As far as I can tell, the code is still solid for prod. I'm wondering if this has something to do with the effectively random order of global destruction because this is just being triggered by a compilation test. (That's just speculation and could be a red herring).

    Note: If anyone else experiences this, the workaround is to simply exclude the automatic immutable behavior and add it manually to your M ooseX::Extended classes.

Extract multiple lists od Identifiers from a FASTA file
5 direct replies — Read more / Contribute
by joluito
on Aug 04, 2022 at 07:20

    Hello everybody,

    I'm trying to extract a set of diferent ID lists from a FASTA file in a batch.

    I have a code that allows me to do it but it only extracts the first list of the loop (even though it reads the other lists and tell how many od each list's ID are found in the FASTA file).

    There must be an issue with the OUTFILE opening/closing in the for loop, but I seem unable to find what the problem is.

    Any help would be appreciated.

    Here is the code (and I can provide dummy test files if needed):

    #!usr/bin/perl -w use strict; use diagnostics; use Getopt::Long; #usage example: perl /path_to_script/ -p /path_to_lists_dir +-f /path_to_fasta/FastaFile.faa my ($path,$fasta); GetOptions( 'path=s' => \$path, 'fasta=s' => \$fasta, ); #print "$path\n"; chdir $path or die "ERROR: Unable to enter $path: $!\n"; opendir (TEMP , "."); my @files = readdir (TEMP); closedir TEMP; my $name; my $found=0; my $totalist=0; for my $file (@files) { if($file=~/(\w+)\.fasta/){ $name = "$1"; open (INFILE2, "$path/$name.txt") || die ("cannot open input file"); chomp(my @lista = <INFILE2>); $/ = "\>"; open (INFILE, "$path/$name.fasta") || die ("cannot open input file"); chomp(my @data = <INFILE>); open OUT,'>'."$path/$name.out" or die "ERROR: Unable to open $file $! +\n"; foreach my $li (@lista){ chomp $li; print"$li\n"; $totalist++; for(@data){ if(/$li/){ $found++; print ">"."$_\n"; print OUT ">"."$_\n"; } } } print "For $name we found $found of a total $totalist\n"; } }

    Example of FASTA format:


    Example of ID list:

    WP_002208793 WP_002208792 WP_002211763 WP_002211762 NP_490508 NP_490509 NP_459538 NP_459540

    I expect to get a new FASTA file (for each of the ID lists) containing only the sequences matching to the ID on the specific list. The code expects to, basically, be a kind of Sequences retriever in batch. I mean, I expect the script to:

    -open the original FASTA file

    -open each of the ID lists

    -read and chomp it

    -match & retrieve the correspondent sequences from the original FASTA

    -write them to individual files (named like the lists but in FASTA format)

    The output should ne in FASTA format (the same format in the above example).

    Leaving aside the format specifications, I think the issue here is why it only works in the first "for" loop iteration and not in the following ones. Can you spot any error I can't, regarding the outfile opening or how I try to write my results in it inside the loop?

    Thank you very much in advance.

Incompatibility after perl 5.10 packing and UTF-8 strings
3 direct replies — Read more / Contribute
by powerperl
on Aug 03, 2022 at 14:18

    You can refer incompatibility in perldelta doc of v5.10. My concern is I am upgrading old perl before v5.10 to v5.20. Should I worry about it. How to identify if this incompatibility is serious concern for me. I my codebase I found lot of usage of pack and unpack. It is mentioned that 'use bytes' will give old behaviour back.

    Can we do it everywhere ? How to handle this incompatibility in general if we are jumping from version before 5.10 to version after 5.10 that is 5.20. I know that it is complex to take such high jump I am trying to take care of everything. Good explanation will help me here.

Inconsistent results with keys on thread shared hashes
2 direct replies — Read more / Contribute
by izomiac
on Aug 03, 2022 at 00:22
    I've been stumped with a bug in my code for days, until I traced it down to a fairly simple subroutine that gave inconsistent results when two or more threads were running. Here's an isolated version of the problem:
    #!/usr/bin/perl use strict; use warnings; $|=1; use threads; use threads::shared; my %testOne :shared = (A => 1, B => 2, C => 3, D => 4, E => 5); my %testTwo = (A => 1, B => 2, C => 3, D => 4, E => 5); my $inc :shared = 1; do { threads->create(sub { while (1){ my $i = join('', sort keys %testOne); my $j = join('', sort keys %testTwo); print "$inc-1\t$i\n" if ($i ne 'ABCDE'); print "$inc-2\t\t$j\n" if ($j ne 'ABCDE'); } }); } while (sleep 5 && $inc++ && $inc < 100);
    I'm running this with Strawberry Perl on Windows, and my application uses threads so the Tk GUI doesn't freeze. I want to be able to use a lot of worker threads for a CPU-bound genetic sorting algorithm. Is this a bug in perl, or have I misunderstood something?
Diagnosing "Input/output error"
4 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 02, 2022 at 13:18

    I'm seeing a very weird problem in a large Catalyst-based app I have to maintain.

    The logging system is set up on the application level, using Log::Dispatch::File (FileRotate, specifically); every Controller simply sets a different logfile as necessary (or disables logging entirely). The setup is exactly the same for every Controller: they inherit the same begin.

    Exactly one Controller has very frequent errors: 'Caught exception in My::Controller:ThisParticularController->begin "Cannot write to '/path/to/logs/ThisParticularController.log': Input/output error at /path/to/Log/Dispach/ line 101."' However, this controller also very frequently does successfully write to this logfile. No other Controller ever reports this error. This Controller is probably the busiest one, but it's not a vast difference. There are no differences in permissions for this file, and it's in the same filesystem as all the other logs.

    I simply can't see any difference with how logging is handled for this Controller vs. any of the others, and I don't know what "Input/output error" actually means. What can I do to try to figure this out?

Is it ever legitimate to override $^O ?
6 direct replies — Read more / Contribute
by syphilis
on Aug 01, 2022 at 21:53
    Ken Williams' Path-Class module has a test file (t/01-basic.t) that begins with:
    BEGIN { $^O = 'Unix'; # Test in Unix mode }
    Without that override, many tests fail on Windows because paths that are expected to contain '/' are detected as instead containing '\\'.
    And there are also cases of an expected empty string being detected as '0'.
    Try building Path::Class on Windows and watch it pass - then comment out the BEGIN{} block in t/01-basic.t and examine the failures then elicited by that script.
    (BTW, I don't know how that override hack even works.)

    The trouble is that, beginning with perl-5.34.0, this hack fails to deliver because running 'perl -Mblib t/01-basic.t' fails in the way already outlined above, irrespective of whether that override is included or commented out.
    In addition (with perl-5.34.0 onwards and the BEGIN{} block included), t/01-basic.t fails in an entirely different way when run inside the Test::Harness (as part of 'make test'):
    t/01-basic.t .......... 1/78 Can't call method "relative" on an undefi +ned value at C:\sisyphusion\Path-Class-0.37\blib\lib/Path/Class/ line 7 +6. t/01-basic.t .......... Dubious, test returned 22 (wstat 5632, 0x1600)
    What bug(s) should be investigated ?
    Is overriding $^O a legitimate thing to do ?
    Is it a bug that, on perl-5.34 and later, the "Can't call method "relative"..." error arises ? If so, is it a Path::Class bug ? or a Test::Harness bug ? or a perl bug ?.
    Is it a bug that, on perl-5.34 and later, the override loses its effectiveness when run outside Test::Harness ? If so, is it a Path::Class bug ? or a perl bug ?

perltidy and experimental for_list
1 direct reply — Read more / Contribute
by ibm1620
on Aug 01, 2022 at 13:39
    Dear Monks,

    Perltidy (ver 20220613) currently doesn't handle the v5.36 for_list syntax.

    Running perltidy -npro:


    #!/usr/bin/env perl use v5.36; no warnings q/experimental::for_list/; for my ( $k, $v ) ( %ENV ) { say "$k=$v"; } say "Done";
    #!/usr/bin/env perl use v5.36; no warnings q/experimental::for_list/; for my ( $k, $v ) (%ENV){ say "$k=$v"; } say "Done";
    Does its experimental status mean that it won't be supported by perltidy? Are there .perltidyrc settings to keep it from removing the line break following the closing bracket?


How has your coding style changed over the years?
9 direct replies — Read more / Contribute
by stevieb
on Aug 06, 2022 at 21:42

    Since I started coding C and C++ in 2000, and Perl very shortly afterwards, my style hasn't fundamentally changed. If anything, I've simply become more pedantic about certain things. I'm bored so I thought I'd put a list together off the top of my head. What's your list look like?

    - Four space tabs!

    - I like no space between function parens:

    function($param1, \%param2);

    - I'm very much a K&R style person who likes 'else' type statements on their own line:

    sub function { if ($_[0] == 1) { do_something(); } else { do_something_else(); } }

    - When dereferencing a list, I always use whitespace:

    my @list = keys %{ $href->{thing} };

    - I always use the deref indicator where it is needed:

    my $thing = $hash{one}->{two}; my $other = $href->{one};

    - In my personal Perl code (60+ CPAN distributions), I always put the POD at the bottom of the file after all code (at $work, it's inline):

    package Blah; ... 1; =head1 NAME Blah - The thing with the guy and the place ...

    - I *ALWAYS* write unit tests before or at least during writing each function/method. I use the test scripts as test runs to prototype the code I'm writing instead of having a separate script. The test scripts are inherently part of the test suite. I also *ALWAYS* review my unit test suite for each piece of functionality and update it if necessary if patching/updating subs days, weeks or years later.

    - I (almost) *ALWAYS* write POD documentation as I'm writing the code (rarely before, but sometimes I do that too).

    - I frequently peruse the documentation of a random publication of my own software (regardless of language), and make fixes or produce relevant updates I may have missed.

    - I use the same editor all the time (vi/Vim) when I don't have my IDE handy, intelliJ IDEA (with vim support, of course). (VSCode for some of my work projects).

    - I rarely have to use the perl debugger, because I almost always find base issue cause through Data::Dumper statements. If I do use a debugger, it's more often for C code than it is for Perl code.

    - One of my favourite topics for new development is writing code to help other developers (including me). Introspection and unit test software is a big thing for me.

    - I love PPI, and am fluent enough with it that I rarely need to refer to the documentation when I want to use some of its functionality.

    - For my Perl code, I strive with great effort to achieve 95%+ unit test coverage, and that coverage generally covers that 95% 16 ways from Sunday. I often write additional code just so that tests can test code it just cant cover. This includes special constants, env vars etc. Some of my software has complete routines as add-ons just to get a single statement covered that otherwise couldn't have been.

    - I use Continuous Integration testing (CI) for almost everything. Mostly Github Actions (formerly Travis CI until they pissed me off tremendously), but some of my code can't run there, so I use my own Test::BrewBuild for such software.

    - I used to, but not so much anymore, review CPAN latest releases to try to find bugs to fix, even if its just documentation.

    - I am very specific about honouring other artist's copyright information. To further, I regard and honour the license of projects I've taken over from other authors. I'm a published author, poet, lyricist and music producer so the copyright thing in ingrained and imprinted. Appreciating other's art ownership isn't a desire to me, it's a core instinct.

    - I am diligent in acknowledging contributors to my software. My Changes files and commits generally have the persons name and/or CVS username embedded.

    - I take criticism very well; that said, I *ALWAYS* give credit where it is due, and *NEVER* claim credit for things I did not myself do

    - I take bug/issue/feature requests very seriously, and do my utmost to respond in as timely a manner as I humanly can (sometimes I don't, but that's very rare).

    - I use a bug tracker for almost everything I find myself; new features, real life bugs, security issues or even POD typos. If I'm perusing a random distribution of my own and I see a typo in the SYNOPSIS, I create a ticket for it.

    - I never use shift in an OOP module, I always use my ($self, ...) = @_;

    - I *ALWAYS* do parameter validation in each and every function/method.

    - I use pure perl OOP; very, very rarely do I ever use any of the helpers. The only time that happens is if I'm requiring a distribution that has that required already.

    - My POD format for subs is typically:

    =head2 method($param) Contemplates the reason for life, using common sense as a modifier. my $explanation = My::Continuity->new; my $thing = 'Reason for living'; my $reasoning = $explanation->method($thing); I<Parameters>: $param I<Mandatory, String>: The explanation of the formation of humanity in +a single string. I<Return>: Hash reference, where each key is a continent, and its valu +e is a colour. C<croak>s on failure.

    - I very sparsely use comments. Almost always my comments within code refer to *why* something is happening. It very rarely (if ever anymore) refer to 'what' is happening. The code says what is happening. Said comments when I make them are generally one to two lines only, and I use them only when I, myself will need to be reminded why in the fsck I did something so bizarre.

    I'm sure I can add a hundred other particulars I've formed over the years, but that's a start. How about you?

    Edit: Oh dear, I completely forgot. If it isn't blaringly obvious, Perl is my language of choice. Always has been, and I'm sure always will be. I'm decently fluent in C, C++, C#, wrote code in Python for four years as part of a job, can dabble my way through Javascript/JS, but I always lean back to Perl. Need an API for something (eg. Raspberry Pi)?, I'm making it available in Perl! New unofficial API for a new toy (eg. Tesla)? I'm ensuring it can be accessed with Perl! My priorities in life: My health, contentedness and happiness, my sobriety, my wife and children, Perl, everything else :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2022-08-08 16:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found