Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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
Perl can't make some easy arithmetics :(
2 direct replies — Read more / Contribute
by rsFalse
on Oct 24, 2014 at 16:21
    Today I spend time to understand why do I get wrong answers, and I don't know an answer. Later I tried and found how to overcome problem: use bigint and add zeroes. See this:
    while(<>){ ($a, $b)=split/ /; print $a - $b, " "; print (($a+0)-($b+0), " "); use bigint; print $a - $b, " "; print (($a+0)-($b+0), "\n"); }
    STDIN: 9 7 900000000000000009 900000000000000007 STDOUT: 2 2 2 2 0 0 0 2
Making PDF Tables
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 24, 2014 at 15:13
    Hi Monks!
    I need to create a PDF document with a few tables in it, from the code sample of the module PDF::Table its simple to have one table in the same page of a PDF, but I canít figure it out how I would add another table into the same PDF page. Has any of you tried or had to do the same thing using the same module?
    Code Sample
    use PDF::API2; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => "table_of_lorem.pdf"); my $page = $pdf->page; # some data to layout my $some_data =[ ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequat quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit."], #... and so on ]; $left_edge_of_table = 50; # build the table layout $pdftable->table( # required params $pdf, $page, $some_data, x => $left_edge_of_table, w => 495, start_y => 750, next_y => 700, start_h => 300, next_h => 500, # some optional params padding => 5, padding_right => 10, background_color_odd => "gray", background_color_even => "lightblue", #cell background color for +even rows ); # second table in the same PDF page $pdftable->table( # required params $pdf, $page, $some_data, x => $left_edge_of_table, w => 500, start_y => 755, next_y => 705, start_h => 305, next_h => 505, # some optional params padding => 5, padding_right => 10, background_color_odd => "navy", background_color_even => "red", #cell background color for even r +ows ); # do other stuff with $pdf $pdf->saveas();

    Thanks for looking!
compare always true in grep
5 direct replies — Read more / Contribute
by RonW
on Oct 24, 2014 at 14:05

    The following worked fine on Linux, but is not working on Win7 Pro (Strawberry Perl 5.18.2 64bit)

    (This uses the expression form of grep)

    #!perl -w use warnings; use strict; my @times = ( 1, 2, 3, 4); print STDERR "Times: @times\n"; if (grep ((0 + $_) >= 99999999), @times) { die('Time values must be < +99999999'); }


    >perl Times: 1 2 3 4 Time values must be < 99999999 at line 9. >
Mocked query returns wrong result
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 24, 2014 at 10:37

    Hi perl monks

    I'm trying to mock an sql call (for practice purposes) using DBD::Mock and i'm getting a wrong result.


    #!use/local/bin/perl use Test::More qw (no_plan); use Data::Dumper; use DIR::DBTeams; use constant DB => "dbi::Mock:"; use constant DB_USER => "mock"; use constant DB_PWD => "mock"; my $db; my $query; my $columns; my @boundparams; my @results; my $test_teams; $query = q(select decode(substr(te.TEAM_ID,1,2),'MU','Manchester Unite +d', 'RM', 'Real Madrid')) from Teams t inner join TEAMS_EUROPE.TEAM_REF te + on t.TEAM_REF = te.TEAM_REF and t.TEAM_REF=:ref); $boundparams=('123'); #123 is reference for MU $columns=('TEAM_ID'); @results=('Manchester United'); $db = &connectDB(DB,DB_USER,DB_PWD); my $session = DBD::Mock::Session->new('my_sesion' => ( { statement => $query, bound_params = [@boundparams], results => [[$columns],[@results]] } ) ); $db->{mock_session}=$session; $test_teams = new DBTeams($db, $boundparams[0]); is($test_teams, "Manchester United", "Test Teams"); sub connectDB($$$) { my $dbh = DBI->connect($_[0],$_[1],$_[2]); return $dbh; }

    Code : DBTeams

    package DIR::DBTeams; use DBI; sub new { my $class = shift; my $self = {}; my ($dbh,$ref) = @_; $self->{dbh} = $dbh; &getTeam($ref); } sub getTeam { my $me = shift; my $ref = shift; my $ret; my $cur = $me->{dbh}->prepare( q(select decode(substr(te.TEAM_ID,1,2),'MU','Manchester United', 'RM', 'Real Madrid')) from Teams t inner join TEAMS_EUROPE.TEAM_REF te + on t.TEAM_REF = te.TEAM_REF and t.TEAM_REF=:ref)); $cur->bind_param(':ref',$ref); $cur->execute() or croak $cur->errstr; $ret = $cur->fetchrow_array(); $cur->finish; return $ret; }

    The code seems ok, but when it runs, instead of getting 'Manchester United', the method returns 1.

    I tried debugging and several alternatives and none seem to work.

    Finaly i tried changing the original code:

    Code : DBTeams

    ... ($ret) = $cur->fetchrow_array(); ...

    By placing $ret in ( ), the code now returns what i want. However, i want to know if there is an alternative that allows me to get the result i want without changing the original code.

    Can someone help me? Thanks

Windows folder access error
3 direct replies — Read more / Contribute
by ArifS
on Oct 24, 2014 at 10:31
    I am getting the following error when try to execute the following code-
    # c:\Folders\1Folder\1aFolder my $directory = "\\Folders\\1Folder\\1aFolder"; print "Folder: ", $directory, "\n"; opendir (DIR, $directory) or die $!; while (my $fldr = readdir(DIR)) { print "Files & Folders: ", $fldr, "\n"; }
    Folder: \Folders\1Folder\1aFolder Invalid argument at c:\temp\dir1A9A.tmp\ line ...##. Press any key to continue . . .
    Pointing to line - opendir (DIR, $directory) or die $!;

    Please let me know
Net::LDAP, Active Directory, Move accounts across domain in forest?
No replies — Read more | Post response
by TomJ_SC
on Oct 24, 2014 at 10:25

    I am not new to Net::LDAP but I am unsure how to approach this problem.

    I have no issues using moddn to move accounts within a domain. However, I have a need to move users from one domain to another in the same Active Directory Forest (2008R2 functional).

    My initial plan was to delete the account from one domain and then create the account in the new domain. This brings up obvious issues (need to migrate emails, password reset to generated default, etc).

    I see that there is an option to move accounts within the same forest. It also appears that moddn does not support this and I do not see any help from Net::LDAP::Extra::AD (although reset_ADpassword and change_ADpassword are awesome-THANKS!). ,/p>

    Any guidance pointers?

    Thanks for your time and reading this.


refine output of Dumper
5 direct replies — Read more / Contribute
by kaka_2
on Oct 24, 2014 at 06:04
    Hello Monks, i have following code which works well. only problem is output does not looks good.
    use strict; use warnings; use Data::Dumper; $Data::Dumper::Terse=1; my $StatChk="cat /tmp/teststatus.txt | grep -v OK"; my $GoodStat="OK"; my @SChk_Out; open (DChk, "$StatChk |") || die "Failed: $!\n"; @SChk_Out = <DChk>; close (DChk); print "Output of command is =".Dumper(@SChk_Out);
    and Output is:
    [ 'Station 7777: Not good. ', 'Station 7778: Not Good ' ]
    how can i have output just
    Station 7777: Not good. Station 7778: Not good.

    it is important that i use the Dumper. in my actual program i cant just print array. (kind of limitation)

    any help in this regard will be appriciated.
A free Bulletin Board / Discussion Forum for Commercial use
1 direct reply — Read more / Contribute
by perlron
on Oct 24, 2014 at 03:26
    Hi Monks,
    The background is im running out of time to develop a custom solution for a web customer. In the meantime, ive got a hang of CGI::Application while focussing on the rest of the site.

    Id like to know if i can get my hands on "trivial/simple" "customizable" bulletin board or discussion forum software from the perl source repositories.
    Since i have a hang of CGI::Application and a little Dancer , any BB's made using these two gr8 frameworks would be really fast for me to hack into and deploy. I am charging the client for developing a website, so i infer this is a commercial purpose im going to use the BB for.

    I hope this is the right forum to solicit such an opinion. else please redirect.

    Do not wait to strike till the iron is hot! Make it hot by striking - WB Yeats
Output to STDOUT and print last line to console
3 direct replies — Read more / Contribute
by waytoperl
on Oct 24, 2014 at 00:31

    I have a situation to write output to a file and also print on console or CMD. Need feedback on my non-working code. Thanks.

    #!/usr/bin/env perl -w use strict; use warnings; use Fcntl; use Tie::File; #-- saving result to output.txt open STDOUT, ">", "output.txt" or die "$0: open: $!"; #-- write to output.txt and print on console print "##Start of SCRIPT.PL##\n"; my $run = ""; system($run); #-- open the file tie my @rows, 'Tie::File', 'output.txt', autochomp => 0 or die "error: + $!\n"; #-- print last line of output.txt on console print "$rows[-1]";
Debugging Bioperl warnings for Genebank files that are missing info
1 direct reply — Read more / Contribute
by Sosi
on Oct 23, 2014 at 18:33

    Oh thy masters of Perl wisdom, please enlighten me. I am struggling with some problems reading Genebank files (.gbff) through Bioperl.

    I am trying to extract CDS and translation sequences using $feat->spliced_seq->seq and $feat->get_tag_values("translation")). My problem is that many of the genebank files are incomplete or are not matching the "correct" (example) format:

    LOCUS SCU49845 5028 bp DNA PLN 21-JUN-1 +999 DEFINITION Saccharomyces cerevisiae TCP1-beta gene, partial cds, and +Axl2p (AXL2) and Rev7p (REV7) genes, complete cds. ... JOURNAL Submitted (22-FEB-1996) Terry Roemer, Biology, Yale Univer +sity, New Haven, CT, USA FEATURES Location/Qualifiers source 1..5028 /organism="Saccharomyces cerevisiae" /db_xref="taxon:4932" /chromosome="IX" /map="9" CDS <1..206 /codon_start=3 /product="TCP1-beta" /protein_id="AAA98665.1" /db_xref="GI:1293614" /translation="SSIYN...RTANRQHM" gene 687..3158 /gene="AXL2" CDS 687..3158 /gene="AXL2" /note="plasma membrane glycoprotein" /codon_start=1 /function="required for axial budding pattern of +S. cerevisiae" /product="Axl2p" /protein_id="AAA98666.1" /db_xref="GI:1293615" /translation="MTQL...GRIPEML" gene complement(3300..4037) /gene="REV7" CDS complement(3300..4037) /gene="REV7" /codon_start=1 /product="Rev7p" /protein_id="AAA98667.1" /db_xref="GI:1293616" /translation="MNRWVEK...IFGSLF" ORIGIN 1 gatcctccat ata...tgatc //

    Many of the files that I have either do not have the "Origin" field at the bottom (example), or have multiple "Origin" fields (example), each just after a "CDS" field, resulting in warnings and die errors that prevent me from doing what I need to do. Most of the warnings indicate that Bioperl hasn't been able to infer the

    So my questions are the following:

    1. Could you give me any tips on how I can find which of the files have this incorrect file format? I am figuring that a if($feat->spliced_seq->seq) fails, push those filenames to a list and manually download them again :( But I haven't been able to test this correctly yet, and maybe there is something already in Bioperl for these cases?

    2. How can I prevent the automatic die everytime a warning comes out, so that I can find the whole list of files that is not designed as it should? Curiously, through the ~1000 files that I am running, the script runs for a few hundreds, outputing those errors but quits at some point. I must say that I have use autodie; in the preamble, but I think the die command is being given by Bioperl.

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 imbibing at the Monastery: (8)
    As of 2014-10-24 21:38 GMT
    Find Nodes?
      Voting Booth?

      For retirement, I am banking on:

      Results (137 votes), past polls