Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Seekers of Perl Wisdom

( #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
Why does eof have a prototype?
1 direct reply — Read more / Contribute
by LanX
on Aug 07, 2022 at 12:57
    Hi

    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/listret.pl -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:

    >VFG000033(gb|WP_002208793) MPPAARLSLLQRSSTMSDFLPFALPDIGEAEIQAVTESMRSGWLTTGPNAREFEREFAAY IGADVEAVAVNSATAGLHLALEAIGVGPGDEVITTTHTFTASAEVARYLGAEPVLVDIDP ATLCISPAAIERAITPRTRAIVPVHYGGLSCDMDSILEIARKHGLKVIEDAAHALPASWQ GRRIGSLESDLTVYSFYATKTLATGEGGMVVTRDPALAKRCRVMRLHGIDRDAFDRFTSK KPAWYYEIVAPGFKYNMTDTAAAMGRVQLQRVQQMRDRRAQIAAAYDQAFADLPLTLPPG PGRTPGVERVAHRDDDEHSWHLYAIRIHPQAPLKCDDFIVRMTENGIGCSVHYVPLHLQP YWRDRYGLTPDMYPHSQAAFEGMASLPIYSRMTDADVQRVIASVRQLLRP >VFG000036(gb|NP_490509) MQFIDLKTQYQALRDTINPRIQAVLDHGQFIMGPEVKELEAALCAYTGAKHCITVASGTE ALLISLMALGVKAGDEVITTSFTFVATAEVIALLGAKPVFVDVEPDTCNIKVSEIEAKIT PRTKAIIPVSLYGQCGDMDEV

    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.

Workflow / Setup Questions
3 direct replies — Read more / Contribute
by monsignor
on Aug 03, 2022 at 14:37

    Please pardon this humble neophyte, but I am having trouble getting my head around how to set myself up efficiently. The general language documentation is excellent! I love the elegance of the language and I have managed to write several moderately complex scripts just using VIM.

    I have been debugging by running perl at the command line, adding print statements for debugging, rerunning, removing the print statements, etc. This seems unnecessarily slow and tedious, so I'm hoping there might be a better way. (For example Python has Idle--when a program dies, it leaves the environment intact so that you can execute code interactively to get more information without having to edit the original code and reproduce the error.

    1. Is there an IDE with good code completion for Perl that runs on a modern linux? PADRE looks like the type of thing that would be ideal, but it seems to be abandonware (Ubuntu 10!? Circa 2012). Strawberry Perl also looks good... but I don't do Windows, and it appears to be for Windows only. The "perl debugger" looks very hard to use for a casual user.

      Since I don't code on a daily basis syntax completion would be a big help, but I don't have a budget for an expensive commercial product.
    2. How to install packages so that I don't make a mess of my system. (I develop on Linux Mint.)
    3. Is there a way to deploy scripts so that they are stand alone (say a single source file, or a single directory? I want to deploy a script or two to my pfSense box without disrupting the system or installing a lot of bloat or something that might impact the integrity of the system.

    My searches have lead me to many broken links or very old pages. Any guidance would be much appreciated.

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/File.pm 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?

Python equivalent of an Ovid, brian d foy, or chromatic
3 direct replies — Read more / Contribute
by davebaker
on Aug 02, 2022 at 11:13

    I'll be doing some work with Python's Django web framework, and wonder if any Perl people have needed to do so also, but in particular I'm asking whether you've located authors of books and articles on the Python language (or Django in particular) who are in the league of Curtis "Ovid" Poe, brian d foy, or chromatic.

    I've taken the liberty of posting here because you'll know exactly what I mean by the question, and other Perl developers might have needed to go down this road.

    There are so damned many Python and Django resources that it's nearly impossible for this longtime Perl enthusiast to find the diamonds among the coals, even using criteria like reader reviews.

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/Entity.pm 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 ?

    Cheers,
    Rob

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


  • 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 or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2022-08-07 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?