Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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. Post a new question!

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.

User Questions
GetOptions getting a string
3 direct replies — Read more / Contribute
by ovedpo15
on Mar 24, 2018 at 14:24
    Hello wise monks!

    I would like to use Getopt::Long module in to declare a flag which can get one string, many strings or none.
    I know how to make each one separate, but I don't know how to do all of them.
    For example:
    GetOptions("flag=s{,}" => "$flag");

    If I run "./ --flag" it will say that I didn't gave it a string.
    How can I handle this case? I would like that the following code will work:

    "./ --flag"
    "./ --flag a"
    "./ --flag a,b,c,d,e,f"
Can't locate object method "json_content"
1 direct reply — Read more / Contribute
by Krillianaire
on Mar 24, 2018 at 07:11
    Hi all,

    I have a script to retrive some company data from Companies House. I have a list of company numbers, the script goes through each one in turn and collects some data. There are several hundred numbers to go through and at random points I get the following error and the script dies:

    Can't locate object method "json_content" via package "HTTP::Headers" at /usr/local/share/perl5/HTTP/ line 652.

    This does not occur at any given company number, can happen on the 10th one or 255th one, no pattern there. The part of the code where the error occurs:

    my $ua = LWP::UserAgent::JSON->new; my $request = HTTP::Request::JSON->new( GET => "https://api.companiesh$cn"); $request->authorization_basic('password_here'); # actual password remo +ved my $response = $ua->request($request); my $json = $response->json_content;

    I thought that maybe the response from companies house was broken at certain times, or it was giving an error and that was the reason. But when I dumped the return for $response, there appears to be no issue just before the script breaks. Not really sure what else to try, so any help would be greatly appreaciated.

How to retrieve the intergenic sequences and the introns from GenBank page of NCBI?
2 direct replies — Read more / Contribute
by supriyoch_2008
on Mar 24, 2018 at 00:46

    I am interested to retrieve the intergenic sequences (sequences between genes) and the intron sequences (noncoding region of genes) separately of an organism using the accession number from GenBank page (Nucleotide database) of NCBI. After searching for a perl script in Google I could not find one to perform the task. Although I came across a few perl modules which can retrieve the complete nucleotide sequence of the accession number e.g. NC_027152.1 Lens culinaris cultivar Northfield chloroplast, complete genome. It has a sequence of 122967 bases with detailed annotations and underlined links for genes indicating positions e.g. for gene psbA, complement(313..1374) and for gene trnK-UUU, complement(join(1691..1719,4200..4236)). The bases from 1..312 and from 1375..1690 in the complementary sequence are intergenic sequences. The word "Complement" stands for complementary sequence. But the bases from 1720..4199 is the intron sequence (intervening sequence) for the trnK-UUU gene.

    Extracting the specific region using "Change region shown" on the right panel of GenBank page is a very tedious and time-consuming process. If a perl script is written to extract the intergenic sequence and the intron sequence(s) of genes, it will certainly save time for data collection. I welcome suggestions and guidance from Perl experts to retrieve the intergenic sequences and the intron sequences separately.

    I have written a script that can retrieve the complete sequence of 122967 nucleotides. I have given the code below:

    The GenBank page partly looks like (it is not the complete GenBank information for Acc No. NC_027152.1):

    Lens culinaris cultivar Northfield chloroplast, complete genome NCBI Reference Sequence: NC_027152.1 FASTA Graphics LOCUS NC_027152 122967 bp DNA circular PLN 0 +3-JUN-2015 DEFINITION Lens culinaris cultivar Northfield chloroplast, complete g +enome. ACCESSION NC_027152 VERSION NC_027152.1 DBLINK BioProject: PRJNA285561 KEYWORDS RefSeq. SOURCE chloroplast Lens culinaris (lentil) ORGANISM Lens culinaris Eukaryota; Viridiplantae; Streptophyta; Embryophyta; Trach +eophyta; Spermatophyta; Magnoliophyta; eudicotyledons; Gunneridae; Pentapetalae; rosids; fabids; Fabales; Fabaceae; Papiliono +ideae; Fabeae; Lens. REFERENCE 1 (bases 1 to 122967) AUTHORS Sveinsson,S. and Cronk,Q. TITLE Delimitation of conserved gene clusters in the scrambled p +lastomes of the IRLC legumes (Fabaceae: Trifolieae, Fabeae) JOURNAL Unpublished REFERENCE 2 (bases 1 to 122967) CONSRTM NCBI Genome Project TITLE Direct Submission JOURNAL Submitted (02-JUN-2015) National Center for Biotechnology Information, NIH, Bethesda, MD 20894, USA REFERENCE 3 (bases 1 to 122967) AUTHORS Sveinsson,S. and Cronk,Q. TITLE Direct Submission JOURNAL Submitted (16-MAY-2014) Botany, University of British Colu +mbia, 3529-6270 University Blvd, Vancouver, British Columbia V6T +1Z4, Canada COMMENT PROVISIONAL REFSEQ: This record has not yet been subject t +o final NCBI review. The reference sequence is identical to KJ8502 +39. COMPLETENESS: full length. FEATURES Location/Qualifiers source 1..122967 /organism="Lens culinaris" /organelle="plastid:chloroplast" /mol_type="genomic DNA" /cultivar="Northfield" /db_xref="taxon:3864" gene complement(313..1374) /gene="psbA" /locus_tag="ABY07_gp001" /db_xref="GeneID:24418176" CDS complement(313..1374) /gene="psbA" /locus_tag="ABY07_gp001" /codon_start=1 /transl_table=11 /product="photosystem II protein D1" /protein_id="YP_009141518.1" /db_xref="GeneID:24418176" /translation="MTAILERRDSENLWGRFCNWITSTENRLYIGWFGV +LMIPTLLTA TSVFIIAFIAAPPVDIDGIREPVSGSLLYGNNIISGAIIPTSAAIGLHF +YPIWEAASV DEWLYNGGPYELIVLHFLLGVACYMGREWELSFRLGMRPWIAVAYSAPV +AAATAVFLI YPIGQGSFSDGMPLGISGTFNFMIVFQAEHNILMHPFHMLGVAGVFGGS +LFSAMHGSL VTSSLIRETTENESANEGYRFGQEEETYNIVAAHGYFGRLIFQYASFNN +SRSLHFFLA AWPVVGIWFTALGISTMAFNLNGFNFNQSVVDSQGRVINTWADIINRAN +LGMEVMHER NAHNFPLDLAAVEAPSING" gene complement(1691..4236) /gene="trnK-UUU" /locus_tag="ABY07_gt001" /db_xref="GeneID:24418184" tRNA complement(join(1691..1719,4200..4236)) /gene="trnK-UUU" /locus_tag="ABY07_gt001" /product="tRNA-Lys" /note="anticodon:UUU" /db_xref="GeneID:24418184" gene complement(1967..3490) /gene="matK" /locus_tag="ABY07_gp074" /db_xref="GeneID:24418113" CDS complement(1967..3490) /gene="matK" /locus_tag="ABY07_gp074" /codon_start=1 /transl_table=11 /product="maturase K" /protein_id="YP_009141519.1" /db_xref="GeneID:24418113" /translation="MKESQVYLERARSRQQHFLYSLIFREYIYGLAYSH +NLNRSLFVE NVGYDNKYSLLIVKRLITRMYQQNHLIISANDSNKNSFWGYNNNYYSQI +ISEGFSIVV EIPFFLQLSSSLEEAEIIKYYKNFRSIHSIFPFLEDKFTYLNYVSDIRI +PYPIHLEIL VQILRYWVKDAPFFHLLRLFLCNWNSFITTKNKKSISTFSKINPRFFLF +LYNFYVCEY ESIFVFLRNQSSHLPLKSFRVFFERIFFYAKREHLVKLFAKDFLYTLTL +TFFKDPNIH YVRYQGKCILASKNAPFLMDKWKHYFIHLWQCFFDVWSQPRTININPLS +EHSFKLLGY FSNVRLNRSVVRSQMLQNTFLIEIVIKKIDIIVPILPLIRSLAKAKFCN +VLGQPISKP VWADSSDFDIIDRFLRISRNLSHYYKGSSKKKSLYRIKYILRLSCIKTL +ACKHKSTVR AFLKRSGSEEFLQEFFTEEEEILSLIFPRDSSTLERLSRNRIWYLDILF +SNDLVHDE" gene complement(4722..6149) /gene="rbcL" /locus_tag="ABY07_gp073" /db_xref="GeneID:24418112" CDS complement(4722..6149) /gene="rbcL" /locus_tag="ABY07_gp073" /codon_start=1 /transl_table=11 /product="ribulose 1,5-bisphosphate carboxylase/o +xygenase large subunit" /protein_id="YP_009141520.1" /db_xref="GeneID:24418112" /translation="MSPQTETKAKVGFQAGVKDYKLTYYTPEYQTKDTD +ILAAFRVTP QPGVPPEEAGAAVAAESSTGTWTTVWTDGLTSLDRYKGRCYEIEPVPGE +DNQFIAYVA YPLDLFEEGSVTNMFTSIVGNVFGFKALRALRLEDLRIPNAYVKTFQGP +PHGIQVERD KLNKYGRPLLGCTIKPKLGLSAKNYGRAVYECLRGGLDFTKDDENVNSQ +PFMRWRDRF LFCAEAIYKSQAETGEIKGHYLNATAGTCEEMLKRAIFARELGVPIVMH +DYLTGGFTA NTTLSHYCRDNGLLLHIHRAMHAVIDRQKNHGMHFRVLAKALRLSGGDH +IHAGTVVGK LEGEREITLGFVDLLRDDYIEKDRSRGIYFTQDWVSLPGVIPVASGGIH +VWHMPALTE IFGDDSVLQFGGGTLGHPWGNAPGAVANRVALEACVQARNEGRDLAREG +NAIIREAGK WSPELAAACEVWKEIKFEFPAMDTL" gene 6916..8385 /gene="atpB" /locus_tag="ABY07_gp072" /db_xref="GeneID:24418114" CDS 6916..8385 /gene="atpB" /locus_tag="ABY07_gp072" ..................................... (Many lines omitted here) 122821 aaaagcttcg ggtaaatcac gaaagctacc gtaacagctg caacaggagt ctattata +aa 122881 ttattttctc ttttttgttt taatagattc atgggcgaac gacgggaatt gaacc +cgcgc 122941 atggtggatt cacaatccac tgccttg //
    My script goes like:
    #!/usr/bin/perl use warnings; use strict; use Bio::DB::GenBank; use Bio::SeqIO; use Text::Wrap; my $acc="NC_027152.1"; my $gb= new Bio::DB::GenBank; my $seq1 = $gb->get_Seq_by_acc($acc); my $sequence = $seq1->seq; print "\n Complete sequence: $sequence\n"; # code for intergenic sequence needed # code for intron sequence needed exit; #################
shebang problem on openSUSE
4 direct replies — Read more / Contribute
by kcott
on Mar 23, 2018 at 20:22

    I encountered a problem with the shebang line using openSUSE.

    For testing purposes, I narrowed this down to one of the simplest of scripts (

    #!/usr/bin/env perl -l print 'Hello, world!';

    When I attempt to run this, I get:

    ken@ibm-laptop:~/tmp> /usr/bin/env: perl -l: No such file or directory

    It's not a $PATH or permission issue:

    ken@ibm-laptop:~/tmp> echo $PATH <list of unrelated paths>:. ken@ibm-laptop:~/tmp> which /home/ken/tmp/ ken@ibm-laptop:~/tmp> ls -l -rwxr-xr-x 1 ken users 48 Mar 24 10:29

    I've checked for any problematic characters that might have been present, but found none:

    ken@ibm-laptop:~/tmp> cat -vet #!/usr/bin/env perl -l$ $ print 'Hello, world!';$ $

    When changing the shebang line to any of the following, the scripts run with expected output (i.e. prints "Hello, world!" with or without "\n"):

    #!/usr/bin/env perl #!/usr/bin/perl -l #!/usr/bin/perl

    The '-l' switch appears to work correctly from the command line:

    ken@ibm-laptop:~/tmp> perl -e 'print "Hello, world!"' Hello, world!ken@ibm-laptop:~/tmp>
    ken@ibm-laptop:~/tmp> perl -le 'print "Hello, world!"' Hello, world!

    The platform is "openSUSE Leap 42.1" and the Software Updates notification is telling me "Your system is up to date" (after running "Check For Updates"). Other relevant info:

    ken@ibm-laptop:~/tmp> perl -v | head -2 | tail -1 This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-li +nux-thread-multi ken@ibm-laptop:~/tmp> uname -a Linux ibm-laptop 4.1.34-33-default #1 SMP PREEMPT Thu Oct 20 08:03:29 +UTC 2016 (fe18aba) x86_64 x86_64 x86_64 GNU/Linux

    I'm using a borrowed laptop (the owner is not currently contactable) and I'm not overfamiliar with openSUSE: perhaps I just need to tweak a setting. Neither Super Search nor Internet searches have proved fruitful.

    Any help would be appreciated.

    — Ken

strawberry perl Windows and apply patch
2 direct replies — Read more / Contribute
by limner
on Mar 23, 2018 at 15:28
    hi i need to apply this patch on a perl module: Object-Import-1-004-RT106769.patch I'm using perl by Straberry Perl. Which is the method to apply the patch? Using cpan? Which is the way? Thanks Limner
POD: getting usage snippets from perldoc
3 direct replies — Read more / Contribute
by LanX
on Mar 23, 2018 at 13:08

    I stumbled over Pod::Functions which provides a list of all Perl builtins listed in perlfunc plus short-description.

    use strict; use warnings; use Data::Dump qw/pp dd/; use Pod::Functions; pp '\%Flavor: ', \%Flavor; pp '\%Type_Description: ', \%Type_Description; pp '\@Type_Order: ', \@Type_Order; pp '\%Kinds: ', \%Kinds; pp '\%Type: ', \%Type;
    ( "\\%Flavor: ", { "-X" => "a file test (-r, -x, etc)", "__FILE__" => "the name of the current source file", "__LINE__" => "the current source line number", "__PACKAGE__" => "the current package", "__SUB__" => "the current subroutine, or C<undef> if not +in a subroutine", "abs" => "absolute value function", "accept" => "accept an incoming socket connect", "alarm" => "schedule a SIGALRM", "atan2" => "arctangent of Y/X in the range -PI to PI", ...

    But it doesn't have an option to return the usage examples, like


    any standard recommendation how to get this, without hacking it with Pod::Simple et al?

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

[emacs] perldoc on windows
2 direct replies — Read more / Contribute
by LanX
on Mar 23, 2018 at 10:52

    before I reinvent the wheel:

    ...cperl-mode comes with a lot of options to show perldocs, but requires external unix tools like man and nroff to work, which is a problem on Win without cygwin.

    I'm hacking something with pod2man and woman.el (emacs built-in interpretation of nroff output), but probably someone allready knows a drop-in replacement which works generally?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery untie attempted while 2 inner references still exist
3 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 23, 2018 at 02:09


    My web error log shows an error " untie attempted while 2 inner references still exist at "cgi:error untie attempted while 2 inner references still exist at perl5/IO/Socket/"

    I do not specifically use this module. So what could have caused cgi error to occur?

    Thanks in advance.

Reading from text file
5 direct replies — Read more / Contribute
by sarath92
on Mar 23, 2018 at 01:02

    I have a text file as following:









    I want to get output as follows and need to write the output to another file:




    How can I get this done?

Parsing XML file and keeping the formatting tags
3 direct replies — Read more / Contribute
by corfuitl
on Mar 22, 2018 at 10:50

    Hi PerlMonks

    I have the following bilingual file and would like to extract the source and target nodes preserving its xml elements they might have as well as the line breaks. Could you please help me on that? I have no experience in xml parsing with Perl.

    Here is a sample of my file:

    <trans-unit id="1" maxbytes="14"> <source xml:lang="en-US">Hello <x id=1/> world! How are you?</source> <target xml:lang="ja-JP">Ciao<x id=1/> mondo! Come stai?</target> </trans-unit>

    The expected result should be:

    Hello <x id=1/> world! <lb/> How are you? || Ciao<x id=1/> mondo! <lb/> Come stai?

    Thank you for your time!

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

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2018-03-25 00:38 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (299 votes). Check out past polls.