Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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
Reliable glob?
1 direct reply — Read more / Contribute
by hepcat72
on Oct 21, 2014 at 14:34

    I became disenchanted with perl's glob function quite some time ago and started using bsd_glob instead. It seems to be able to handle the strings it's given a lot better and more comprehensively, but I encountered some unexpected results yesterday. I implemented a work-around, but I'd like the implementation to be more simple.

    I debugged an issue where, when the script was called and given a long glob pattern, it was truncating it to just the preceding directory. So I just wrote a preprocessing routine to pre-expand any patterns containing '{...}' to hopefully shorten the string sent in on the command line before calling bsd_glob on it.

    As far as I understand this issue - and I could be slightly off - the posix flag GLOB_LIMIT (or *ARG_MAX?) is set too low to be able to handle a string that was successfully submitted to the script.

    By implementing my preprocessing of the string that comes from the command line, I was able to break anything up that had a '{...}' pattern in it into shorter pieces that I then spoon-feed to bsd_glob a bite at a time.

    I feel like there's got to be a better solution. If the script is receiving the whole string from the command line, shouldn't the limits of bsd_glob be the same as the surround shell - why would it not be able to handle as long of a string as the surrounding shell script can give it?

    Here's a toy example which shows it not work in the first call, but remove 1 bit and it does work in the second call:

    >perl -e '$x="cff_updated/1_lib/{A3DWE.1.Solexa-142587.splice.fastq,A3 +DWE.1.Solexa-142588.splice.fastq,A3DWE.1.Solexa-142589.splice.fastq,A +3DWE.1.Solexa-142590.splice.fastq,A3DWE.1.Solexa-142594.splice.fastq, +A3DWE.1.Solexa-142595.splice.fastq,A3DWE.1.Solexa-142596.splice.fastq +,A3DWE.1.Solexa-142597.splice.fastq, +q,A3DWE.1.Solexa-142599.splice.fastq,A3DWE.1.Solexa-142600.splice.fas +tq,A3DWE.1.Solexa-142602.splice.fastq,A3DWE.1.Solexa-142603.splice.fa +stq,A3DWE.1.Solexa-142605.splice.fastq,A3DWE.1.Solexa-142606.splice.f +astq,A3DWE.1.Solexa-142607.splice.fastq,A3DWE.1.Solexa-142608.splice. +fastq,A3DWE.1.Solexa-142609.splice.fastq,A3DWE.1.Solexa-142610.splice +.fastq,A3DWE.1.Solexa-142611.splice.fastq,A3DWE.1.Solexa-142612.splic +e.fastq,A3DWE.1.Solexa-142613.splice.fastq,A3DWE.1.Solexa-142614.spli +ce.fastq,A3DWE.1.Solexa-142615.splice.fastq,A3DWE.1.Solexa-142616.spl +ice.fastq,A3DWE.1.Solexa-142617.splice.fastq,A3DWE.1.Solexa-142618.sp +lice.fastq,A3DWE.1.Solexa-142619.splice.fastq,A3DWE.1.Solexa-142621.s +plice.fastq}.drp.fna.lib";use File::Glob ":glob";@y=bsd_glob($x);prin +t(join("\n",@y),"\n");' cff_updated/1_lib/ >perl -e '$x="cff_updated/1_lib/{A3DWE.1.Solexa-142587.splice.fastq,A3 +DWE.1.Solexa-142588.splice.fastq,A3DWE.1.Solexa-142589.splice.fastq,A +3DWE.1.Solexa-142590.splice.fastq,A3DWE.1.Solexa-142594.splice.fastq, +A3DWE.1.Solexa-142595.splice.fastq,A3DWE.1.Solexa-142596.splice.fastq +,A3DWE.1.Solexa-142597.splice.fastq, +q,A3DWE.1.Solexa-142599.splice.fastq,A3DWE.1.Solexa-142600.splice.fas +tq,A3DWE.1.Solexa-142602.splice.fastq,A3DWE.1.Solexa-142603.splice.fa +stq,A3DWE.1.Solexa-142605.splice.fastq,A3DWE.1.Solexa-142606.splice.f +astq,A3DWE.1.Solexa-142607.splice.fastq,A3DWE.1.Solexa-142608.splice. +fastq,A3DWE.1.Solexa-142609.splice.fastq,A3DWE.1.Solexa-142610.splice +.fastq,A3DWE.1.Solexa-142611.splice.fastq,A3DWE.1.Solexa-142612.splic +e.fastq,A3DWE.1.Solexa-142613.splice.fastq,A3DWE.1.Solexa-142614.spli +ce.fastq,A3DWE.1.Solexa-142615.splice.fastq,A3DWE.1.Solexa-142616.spl +ice.fastq,A3DWE.1.Solexa-142617.splice.fastq,A3DWE.1.Solexa-142618.sp +lice.fastq,A3DWE.1.Solexa-142619.splice.fastq}.drp.fna.lib";use File: +:Glob ":glob";@y=bsd_glob($x);print(join("\n",@y),"\n");' cff_updated/1_lib/A3DWE.1.Solexa-142587.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142588.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142589.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142590.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142594.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142595.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142596.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142597.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142598.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142599.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142600.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142602.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142603.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142605.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142606.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142607.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142608.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142609.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142610.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142611.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142612.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142613.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142614.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142615.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142616.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142617.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142618.splice.fastq.drp.fna.lib cff_updated/1_lib/A3DWE.1.Solexa-142619.splice.fastq.drp.fna.lib

    Perhaps bsd_glob is behaving as it should and the design of my surrounding script is what needs to be fixed...

    The surrounding shell script is part of a distributed package - a pipeline of analysis commands, each of which is a perl script. I had decided to handle the input files explicitly instead of supplying a glob pattern involving an '*' so that users could analyze a subset of their data files. Each step in the pipeline adds an extension to each of the files, which is why I chose to use the '{...}' glob pattern, so that I could do this: -d "{$STUBS}.extension". I wrapped it in double quotes so that the shell wouldn't expand them into a space-separated list and thus everything would be identified as an argument to the preceding flag. Like this: -i "dir1/{$STUBS}.drp.fna.lib.n0s.cands" -d "dir2/{$STUBS}.drp.fna.lib"

    Assuming that I might eventually encounter a command that exceeds an arbitrary command-line length limit, what would be a better way to submit a series of files, each with an added extension from the output of the previous step?

    I await the wisdom of the perl monks.

jQuery to select mutiple elements from dropdown by checking checkboxes
1 direct reply — Read more / Contribute
by ashraot
on Oct 21, 2014 at 13:13
    I have a multiselect dropdown which has 8 colors in a hash %all_colors:
    my %all_colors = ( 1 => 'Red', 2 => 'Yellow', 3 => 'Orange', 4 => 'Blue', 5 => 'Black', 6 => 'Brown', 7 => 'Green', 8 => 'White', );
    I have put it in a dropdown like this:
    <div> <% $color_selector %> </div> my $color_selector = '<select name="all_colors">'; foreach my $color (sort {$all_colors{$a} cmp $all_colors {$b}} key +s %all_colors ) { $color_selector .= qq~<option value="$color">$all_colors{$colo +r}</option>~; } $color_selector .= '</select>';
    Now I want to add 2 new checkboxes (checkbox1 and checkbox2). If I select checkbox1, it should automatically select first 4 from the dropdown, and if I select checkbox2, it should automatically select last 4 from the dropdown. So I added one more constant to select checkbox1 and put that constant in function. One set of four elements need not be disabled when others are selected. Both can be selected simutlaneously so that all are selected by checking both checkboxes. Users are allowed to select and deselect members of the group of four. But if any 1 color is randomly selected from the dropdown, then I want to disable the checkbox which is not related to that group. Checkbox should be reenabled for: 1) If one of the colors in a group is selected by itself 2) If more than one is selected but less than 4 3) if all four are selected 4) if a checkbox is checked then one (or more) of the colors from that group is removed from the selection
    use constant MAIN_COLORS => { 1 => 'Red', 2 => 'Yellow', 3 => 'Orange', 4 => 'Blue', }; my $main_colors = MAIN_COLORS;
    I have done the code for checkbox. When checked on the checkbox, the respective colors should be selected in dropdown as I mentioned above.
    <div> <input type="checkbox" name="maincolors" value="1" class="inputChe +ckbox" /> Main Colors <input type="checkbox" name="resetofcolors" value="1" class="input +Checkbox" /> Rest of the Colors </div>
    Also, how to select first 4 and last 4 from dropdown on selecting 2 checkboxes through jQuery? The below code is sample. I want to do it directly through perl code instead of manually creating dropdowns.
    $('input[name="colorcheckbox"]').click(function () { var colorsToSelect = $(this).val(); if($(this).prop('checked') == true) { if(colorsToSelect == 'maincolors') { $('#multipeColorSelect option').slice(0,4).prop('selected', tr +ue); } else if (colorsToSelect == 'restofcolors') { $('#multipeColorSelect option').slice(4,8).prop('selected', tr +ue); } } else { if(colorsToSelect == 'maincolors') { $('#multipeColorSelect option').slice(0,4).prop('selected', fa +lse); } else if (colorsToSelect == 'restofcolors') { $('#multipeColorSelect option').slice(4,8).prop('selected', fa +lse); } } });
    Also, I want to do it directly through perl code instead of manually creating dropdowns. Please help.
Forking Perl5 with Microperl++?
1 direct reply — Read more / Contribute
by rje
on Oct 21, 2014 at 10:27

    "If I were to implement a language now, I'd write a very minimal core suitable for bootstrapping. ... Think of a handful of ops. Think very low level. (Think something a little higher than the universal Turing machine and the lambda calculus and maybe a little bit more VMmy than a good Forth implementation, and you have it.) If you've come up with something that can replace XS, stop. You're there. Do not continue. That's what you need." (Chromatic, January 2013)

    This is in Chromatic's response to Moe, which sounds -Ofun and seems like a good idea.

    And now that I've crawled out from under my rock, I see microperl, an ancient tool included with 5.8. Alright, I don't recall ever hearing about microperl. It's not in my copy of the Programming Perl book, and that sucker is a thousand pages.

    And lately, haven't I seen grumblings about the insularity and stubborn bikeshedding nature of the Perl community?

    So how about this. Microperl isn't the "handful of ops" that Chromatic is suggesting, and it ain't XS, but it's tiny and, well, it's Perl. Therefore, what are the issues around forking and modifying microperl (oh so gently) to bootstrap a variant of Perl5? I mean, it's already written, and it's tiny -- minimal, if Simon is to be believed. I mean, the thing built in a 300k footprint when compiled with a proper optimizer. So what's not to like?

    So I re-ask my question. Doesn't microperl seem to be at least halfway to a bootstrap up to a useful and somewhat regularized, normalized, unpackable, what-have-you-able variant of Perl5?

    Just for reference, here's a sample microperl script written in zentara's discussion. It seems pretty handily powerful for being a tiny subset of perl. Is there documentation for microperl?

Recursively executed function in regex RHS, AKA "wait, why does this work?"
4 direct replies — Read more / Contribute
by Cody Fendant
on Oct 21, 2014 at 07:21

    I was playing around with some code to render a kind of templated HTML, and I came up with the code below.

    The HTML contains special markup, <include id="n"> where n is an integer. These are looked up in a db but here they're just returned by a sub to make this question simple and self-contained. In the example below, there are nested includes where include 3 contains a reference to include 4 and 4 contains a reference to 5.

    I did the rendering by re-calling the rendering function, inside the rendering function as the right-hand side of a regular expression.

    My question is—why does this work? How come it renders all the way down instead of stopping after the first level? I thought it would need some kind of while loop to keep it rendering until it had processed all possible nested includes.

    #!/usr/local/bin/perl print render(1); sub render { my $id = shift; $HTML = return_html($id); $HTML =~ s/<include id="(\d+)">/render($1)/eg; return $HTML; } ### this sub is only here to mimic database lookups sub return_html { my $id = shift; if ( $id == 1 ) { return '<html> <head> <include id="2"> <title>hello world</title> </head> <body> <div class="container"> <h1>Hello world</h1> <include id="3"> </div> </body> </html>'; } if ( $id == 2 ) { return '<link rel="stylesheet" href="foo.css">'; } if ( $id == 3 ) { return '<div id="footer">copyright <include id="4"></div>'; } if ( $id == 4 ) { return '2014 <include id="5">'; } if ( $id == 5 ) { return 'and 2015'; } }
A convenient way to debug your code
5 direct replies — Read more / Contribute
by sotona
on Oct 21, 2014 at 04:46
    Hi Monks,

    I wonder is there any Perl debugger which allows you to attach to an exact process which runs a Perl script in order to debug one?

    Also I wasn't able to find in any debugger I tried to use such a valuable feature like 'set next statement', which allows you to jump to any line in order to change, for instance, something before current statement to make it work properly.

    Does anyone have any ideas?
    Thank you in advance.
No need to escape right brace in regex
2 direct replies — Read more / Contribute
by syphilis
on Oct 21, 2014 at 00:13
    In recent versions of blead (including current 5.21.5) I get:
    C:\>perl -le "$_ =~ /sub mymy{ }/;" Unescaped left brace in regex is deprecated, passed through in regex; +marked by <-- HERE in m/sub mymy{ <-- HERE }/ at -e line 1. C:\>
    That's easily fixed:
    C:\>perl -le "$_ =~ /sub mymy\{ }/;" C:\>
    But why not also (or instead) deprecate the unescaping of the *right* brace ?

Human-readable date format strings
3 direct replies — Read more / Contribute
by thewebsi
on Oct 20, 2014 at 22:28

    I'd like to work with human-readable date format strings - ie, allow the user to select the date format for input and output using a human-readable string such as 'yyyy-mm-dd'.

    Unfortunately, the popular date manipulation strftime()/strptime() functions from POSIX and Time::Piece use a non-human-friendly '%X' format. In my search, I found Time::Format::time_format(), and this does exactly what I want for output (strftime), but there is no input (strptime) equivalent.

    Barring any better options, I'll probably write a regex block to convert format strings from a human-readable format to the POSIX format so I can use the standard functions. Before I embark on that however, does anyone have other suggestions? Thanks.

    Edit: To clarify, instead of POSIX::strftime ( '%Y %m %d', localtime() ), I'd like to use SomeModule::SomeFunction ( 'yyyy-mm-dd', localtime() ), as Time::Format::time_format() does, but also need support for strptime().

Comments detected in strings
1 direct reply — Read more / Contribute
by astroboy
on Oct 20, 2014 at 17:09
    Hi all - I'm trying to identify comments in code (so that ultimately I'll strip them out). I've tried using Regexp::Common::comment, but it appears to detect comments in strings when they should really be ignored. Am I using it wrong, or is it a limitation of the module?
    use strict; use Regexp::Common qw /comment/; my @tests = ( { language => 'PL/SQL', description => 'PL/SQL Comment in String', code => q{ declare j varchar2(2); begin j := '--'; end; } }, { language => 'PL/SQL', description => 'PL/SQL no comment', code => q{ declare j varchar2(2); begin j := 'xx'; end; } }, { language => 'SQL', description => 'SQL Comment in String', code => q{ select '--' from dual; } }, { language => 'SQL', description => 'SQL no comment', code => q{ select 'xx' from dual; } }, { language => 'Perl', description => 'Perl Comment in String', code => q{ my $j = '#'; } }, { language => 'Perl', description => 'Perl no comment', code => q{ my $j = 'xx'; } } ); foreach my $test (@tests) { print $test->{description}."\n"; if ($RE{comment}{$test->{language}}->matches($test->{code})) { print "\tcontains comment\n"; } else { print "\tno comment\n"; } }
    which outputs:
    PL/SQL Comment in String contains comment PL/SQL no comment no comment SQL Comment in String contains comment SQL no comment no comment Perl Comment in String contains comment Perl no comment no comment
Net::Telnet::Cisco - command timed-out error
1 direct reply — Read more / Contribute
by ArifS
on Oct 20, 2014 at 17:06
    I am using the following script, and it works fine with Host1-
    HostName1#, host =, port =23

    Configuration register is 0x2102
    Press any key to continue . . .

    but it doesn't work with Host2-
    HostName2#, host =, port =7012

    I get an error:
    command timed-out at c:\temp\dir5CFE.tmp\ line 6
    3 use Net::Telnet::Cisco; 4 5 my $cs = Net::Telnet::Cisco->new( Host => '', Port => +'7012', ); 6 $cs->login( 'user', 'pass' ); 7 8 # Execute a command 9 my @cmd_output = $cs->cmd( 'show ver | inc Configuration' ); 10 print @cmd_output; 11 12 $cs->close;
    But both devices work fine while I use Putty to telnet in. Please let me know.
Net::FTP::Recursive - how to use rls
2 direct replies — Read more / Contribute
by zalezny
on Oct 20, 2014 at 15:29
    Hi All, on the beginning warm welcome to every one. Since few hours I`m trying to use Net::FTP::Recursive to list recursively all files on the some FTP server. At the moment its not possible for me because for some reason filehandler doesnt work for rls method. Maybe somebody will be able to help me here and solve my issue.
    #!/home/tom/scripting/perl5/bin/perl use Net::FTP::Recursive; use warnings; use strict; use FileHandle; my $ftp = Net::FTP::Recursive->new("", Debug => 1); $ftp->login("anonymous",'me@here.there'); $ftp->cwd('/'); open FH,"> rlist.db"; my @ftpfiles; @ftpfiles = $ftp->rls(<FH>); print @ftpfiles; $ftp->quit;
    I would like to be able to print output from $ftp->rls to screen and if its possible save it to file. It seems that I dont understand perl "filehandle". Maybe somebody will be able to help me here pasting working solution. FTP server is public(Centos mirror). How to list all files with using "rls" command on that server ? Thanks in advance for Your support. Zalezny

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (8)
    As of 2014-10-21 21:56 GMT
    Find Nodes?
      Voting Booth?

      For retirement, I am banking on:

      Results (111 votes), past polls