Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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
Indirect addressing problem
2 direct replies — Read more / Contribute
by tel2
on Aug 27, 2015 at 21:46
    Dearly beloved Monks, lend me your brains...please.

    Instead of coding this kind of thing:

    $mailbox = 'inbox'; $subscribe = 'yes'; $country = 'NZ'; $sel_mailbox{$mailbox} = 'test'; $sel_subscribe{$subscribe} = 'test'; $sel_country{$country} = 'test';
    I was wanting to do recode the last group of lines more elegantly, with something like this:
    for (qw(mailbox subscribe country)) { ${"$sel_$_{$$_}"} = 'test' } # <= Not working!!!
    But I can see that's not working, because there's nothing in $sel_mailbox(inbox).
    I think there are various ways to code this with references, but the above style what I usually use, but alas, I've failed.

    Please guide me back to the straight and narrow.


Safe image check
1 direct reply — Read more / Contribute
by luxs
on Aug 27, 2015 at 21:02
    I'm using Image::Grab library for downloading images from the web to my host. How can I make sure, that these images are normal images, not hidden scripts or other troublemaking files?
OT - SQL choosing a schema for index tables
2 direct replies — Read more / Contribute
by bangor
on Aug 27, 2015 at 17:53
    Dear Monks,
    Apologies for being off-topic but I come here seeking advice as I know the depth of experience in the monastery (and everything else in the project is built with Perl).

    I am creating a database for a heritage organisation who try to keep alive 100s of varieties of heirloom apple trees - the ones we will need when the climate goes really crazy!

    The tree table has a number of fields like:
    usage origin flowering ripe picking status rootstock is_triploid
    Some of these fields can have multiple values, so to store them in an index I intend to create extra tables. I have come up with two options but am unsure which to choose.

    Option 1
    Create a table for each field:

    CREATE TABLE tree_usage_index ( id int, name varchar(100), PRIMARY KEY (name,id) ); CREATE TABLE tree_flowering_index ( id int, name varchar(100), PRIMARY KEY (name,id) );
    The problem I have with this set up is that to do a search for
    trees with usage of 'cider' or usage of 'juicer' and with flowering of 'spring' and with ripe of 'autumn'
    I will have to query multiple tables, and the code I have written for this seems a bit complex.
    SELECT id from tree_usage_index WHERE name='cider' OR name='juicer' # if there are results SELECT id from tree_flowering_index WHERE id IN (results) and name='sp +ring' # if there are still results SELECT id from tree_ripe_index WHERE id IN (results) and name='autumn'
    I know you can nest SQL selects but as there are 12 fields that could be involved in a query the SQL could get a bit hairy. Also, I will probably need to add an index to the 'id' field of each table to speed up the WHERE id IN (results)

    Option 2
    Create one table for all fields:

    CREATE TABLE tree_field_index ( field varchar(100), id int, name varchar(100), PRIMARY KEY (field,name,id), );
    The advantage I see with this is that I will only need to query the one table to perform the same search:
    SELECT id from tree_field_index WHERE ( (field='usage' AND name='cider +') OR (field='usage' AND name='juicer') ) AND (field='flowering' AND +name='spring') AND (field='ripe' AND name='autumn')
    Any advice on the right path to take, or some other solution, greatly appreciated.
Math::BigFloat bnok() question
2 direct replies — Read more / Contribute
by azheid
on Aug 27, 2015 at 14:15

    I have an issue that I think is probably a simple one. I do not know how to use Math::BigFloat::bnok(). I get the following error message "Can't call method "Math::BigInt::bnok" without a package or object reference at line 67."

    Despite reading the Math::BigFloat page and extensive googling, the solution seems beyond my ability to comprehend. Please help. I will post the files that this code uses if necessary, just ask.

    #!/usr/bin/perl use Getopt::Long; use Math::BigFloat; use Math::BigInt; my $codon_fname="codons.txt"; my $n_amer_fname="codon_pair.txt"; my $codon_usage_fname=""; my $mfactor=100; my $codon_usage_per=100; GetOptions( "c=s" => \$codon_usage_fname, ) or die ("Error in command line arguments\n"); open(CODON,'<',$codon_fname)||die "No $codon_fname file available\n"; my %codon_counts; while (my $line=<CODON>){ chomp $line; $codon_counts{$line}= 0 ; } close CODON; open(NAMER,'<',$n_amer_fname)||die "No $n_amer_fname file available\n" +; my @namer; while (my $line=<NAMER>){ chomp $line; push @namer, $line; } close NAMER; my %cd_lookup; open(CLT,'<',$codon_usage_fname)||die "Cannot find codon score lookup +table file $codon_usage_fname\n"; while(my $line=<CLT>){ chomp $line; my @array=split(/\t/,$line); $cd_lookup{$array[0]}=$array[1]; } close CLT; Math::BigFloat->accuracy(40); my $x=Math::BigFloat->new(40); my $y=Math::BigFloat->new(40); my $z=Math::BigFloat->new(40); my $d=Math::BigFloat->new(40); for(my $i=0;$i<@namer;++$i){ my $pvalue=0; my @choose_numerator_array; my %cchash=%codon_counts; for(my $j=0;$j<length($namer[$i])-2;++$j){ ++$cchash{substr($namer[$i],$j,3)}; } foreach my $codon(%cchash){ if($cchash{$codon}){ $x=$cchash{$codon}; $y=$cd_lookup{$codon}*$mfactor; $z=$y->Math::BigInt::bnok($x); push @choose_numerator_array,$z->copy(); } } $x=$choose_numerator_array[0]; for(my $j=1;$j<scalar(@choose_numerator_array);++$j){ $y=$choose_numerator_array[$j]; $x=$x*$y; } $y=$codon_usage_per*$mfactor; $z=scalar(@choose_numerator_array); $d=$y*$z; $x=$x/$d; print $namer[$i],"\t",$x,"\n"; }

    If any future perl users want the answer to how I solved this problem, below is the functional code

    #!/usr/bin/perl use Getopt::Long; use Math::BigFloat; use Math::BigInt; my $codon_fname="codons.txt"; my $n_amer_fname="codon_pair.txt"; my $codon_usage_fname=""; my $mfactor=100; my $codon_usage_per=100; GetOptions( "c=s" => \$codon_usage_fname, ) or die ("Error in command line arguments\n"); open(CODON,'<',$codon_fname)||die "No $codon_fname file available\n"; my %codon_counts; while (my $line=<CODON>){ chomp $line; $codon_counts{$line}= 0 ; } close CODON; open(NAMER,'<',$n_amer_fname)||die "No $n_amer_fname file available\n" +; my @namer; while (my $line=<NAMER>){ chomp $line; push @namer, $line; } close NAMER; my %cd_lookup; open(CLT,'<',$codon_usage_fname)||die "Cannot find codon score lookup +table file $codon_usage_fname\n"; while(my $line=<CLT>){ chomp $line; my @array=split(/\t/,$line); $cd_lookup{$array[0]}=$array[1]; } close CLT; use bignum; my $x,$y,$z,$a,$b,$c; #Math::BigFloat->accuracy(40); #$x=Math::BigInt->new($x); #$y=Math::BigInt->new($y); #$z=Math::BigInt->new($z); #$a=Math::BigFloat->new($a); #$b=Math::BigFloat->new($b); #$c=Math::BigFloat->new($c); for(my $i=0;$i<@namer;++$i){ my @choose_numerator_array; my %cchash=%codon_counts; my $count=0; for(my $j=0;$j<length($namer[$i])-2;++$j){ ++$cchash{substr($namer[$i],$j,3)}; ++$count; } foreach my $codon(%cchash){ if($cchash{$codon}){ $x=$cchash{$codon}; $y=$cd_lookup{$codon}*$mfactor; $z=binomial($y,$x); push @choose_numerator_array,$z->copy(); } } $b=$choose_numerator_array[0]; for(my $j=1;$j<scalar(@choose_numerator_array);++$j){ $a=$choose_numerator_array[$j]; $b=$b*$a; } $y=$codon_usage_per*$mfactor; $a=&binomial($y,$count); $c=$b/$a; print $namer[$i],"\t",$c,"\n"; } sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n--; $r /= $_ } $r; }
Message Queue in Perl
4 direct replies — Read more / Contribute
by QuillMeantTen
on Aug 27, 2015 at 11:06

    Thanks to the comment of a more experienced monk I have been made aware of a vast field of software engineering known as message queue.
    After a bit of googling and a lot of stackoverflowing I went to have a look at Spread. Since it has a package on cpan I decided to install it (using someone else's better built code would save me a lot of debugging and rewriting). Then I got this :

    I'm using debian wheezy, it seems other peoples have encountered the same problem but I had no luck finding an answer, so if anyone here used that package successfully I'd love to know how (sh|h)e did it.

Outputting Unicode to DOS
3 direct replies — Read more / Contribute
by thekestrel
on Aug 27, 2015 at 10:04
    Hi, It's been a while since I've used Perl / Perlmonks and most useful knowledge has long since eroded its way through my skull and dribbled onto the floor.
    I'm using Active Perl (perl 5, version 20, subversion 2 (v5.20.2) built for MSWin32-x64-muli-thread) and want to be able to output some unicode characters (specifically ) so that I can automate some Imagemagick'ing.
    At the DOS prompt i can type just fine (German keyboard) and can create things manually.
    I 'assumed' something like the following would print out the same as seen in quotes, but it doesn't, it is replaced with other ASCII art.
    #!/usr/bin/perl use strict; use warnings; use encoding 'utf8'; my $str = "abc123"; print $str;
    I read through...
    ..and have tried a number of the listed variants, but I'm missing something because nothing is working (I always get other characters).
    Any hints at what I'm sure is a trivial problem would be appreciated.

SOAP::Lite - a hyphen in method name
1 direct reply — Read more / Contribute
by TheNode
on Aug 27, 2015 at 09:08
    Hello Monks,

    I'm new to SOAP::Lite.

    I have to write SOAP client for third party software. It seems pretty straightforward, but there's one obstacle. There is a hyphen in method name I want to invoke.

    I've copied wsdl to local filesystem and modified method name there - of course it returns an error - but the syntax is ok. Output works when sending it through SoapUI when I change the method name back.

    Method name: bpi-request

    When trying to run my script I got an error:
    Bad stub: Bareword "bpi" not allowed while "strict subs" in use at (ev +al 92) line 6. at /usr/lib/perl5/site_perl/5.8.8/SOAP/ line 2921.
    It seems that SOAP:Lite have problem with hyphens in wsdl files. Is there a way to modify xml before sending? Then I could change 'bpi-request' in wsdl to 'bpirequest' and then before sending change all 'bpirequest' to 'bpi-request'.



rename zip files and folders inside zip files
2 direct replies — Read more / Contribute
by mariog
on Aug 27, 2015 at 08:57


    I have to rename the file inside a zip archive as well as the zip archive itself. the zip file and folder is called and a file inside the zip is called identically ATP.xxxxx and I need to rename both the zip and the file inside the zip and remove the "ATP."

    I tried this code but it does not work
    #!/usr/bin/perl -w use warnings; use strict; use Archive::Zip qw( :ERROR_CODES ); my @files; @files = <ATP.xxxxxx*.zip>; foreach my $file (@files){ my $zip = Archive::Zip->new(); $zip->read('$file') == AZ_OK or die "read error $!"; $file=~ s{\.[^.]+$}{}; my $m1 = $zip->memberNamed('$file'); $file=~s/^.{4}//s; $m1->fileName('$file'); $zip->overwriteAs('$') == AZ_OK or die "write error $!\n" }
    the zip file cannot be read..
[OT] A C-language parser test file?
3 direct replies — Read more / Contribute
by BrowserUk
on Aug 27, 2015 at 08:54

    Does anyone know of a publicly available C source file specifically constructed for testing the edge cases of C-language parsers?

    I'm writing a parser for the C-language (and yes; I need to write my own), and I sat down to think of a good way to test it.

    My conclusion is that rather than just feeding it it reams and reams of C sources; most of which will simply be re-exercising the same paths; it would be better to try and construct a single, preferably relatively small, C-source that doesn't necessarily have to do anything useful, but simply uses all the constructs and language features that I need to support. (Which isn't the full standard; any of them.)

    I started to try and write notes on everything I think need to be in this file -- and I will continue that -- but then I thought maybe such a file already exists. I've done a few half-hearted searches for something that might fit the bill; but its proven to be a difficult thing to construct a discriminative search criteria for.

    So, this is just a hope that maybe someone here has knowledge of something that might lend itself to my purpose.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
Suppressing Warning/Error messages
1 direct reply — Read more / Contribute
by intoperl
on Aug 27, 2015 at 06:11

    Hi Monks, I want perl to NOT to display its own error messages, when i am handling it myself. Pls suggest me any way to suppress that. Below is the error messege i get from perl, followed by my own error message

    Argument "hu" isn't numeric in numeric gt (>) at ./ line 120, <STDIN> line 3. ---->(Perl Error)

    Bad Choice...Try again now.. ----> (my error msg)

    The condition i have in my code is as below :

    if(($SRVCH > 22)||($SRVCH < 1)||($SRVCH =~ /(\s+)/)||($SRVCH =~ /[a-z +A-Z]/)) { message("Bad Choice...Try again now.."); system("sleep 1.5"); disp_menu(); }

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
  • 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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (7)
    As of 2015-08-28 02:44 GMT
    Find Nodes?
      Voting Booth?

      The oldest computer book still on my shelves (or on my digital media) is ...

      Results (334 votes), past polls