http://www.perlmonks.org?node_id=479

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
creating a listbox in tkx
1 direct reply — Read more / Contribute
by penmkr
on Oct 23, 2017 at 13:37

    Hi Monks: I am attempting to learn perl/tkx using activeperl v5.24.1 on a windows 10. None of the examples in the book "learn ring perl/tk" by Nancy Walsh, or the tutorial "tkdocs\tutorial\morewidgets.html", work as written but I have managed to get them working until this one. The tutorial states that tcl method '_tk__listbox' is the way to create a listbox using tkx. When I tried this I got an error "method '_tk__listbox' is reserved by tkx". I have no idea why this error is occurring or how to fix it. I have read everything I can find on the subject with no success. Can you please provide me with an explanation of this error and also the proper method for creating a listbox in tkx.

Input Hash Values Into Subroutine
3 direct replies — Read more / Contribute
by FIJI42
on Oct 23, 2017 at 13:04

    So I've written a code that checks to see if a key from Hash1 exists in Hash2.

    When the select keys are found, I wish to pass the values associated with those keys (i.e. a nucelotide sequence) into a subroutine to space and label the rows/columns of the sequence. However, I'm not sure how to do this properly - possibly through referencing/dereferencing the hash?</p?

    The main body of the code is below:

    foreach my $samekeys (sort keys %hash1) { if (exists $hash2{$samekeys}){ ntSpacer(????); } } sub ntSpacer{ my $myseq= my $space; ### Prints number on top line to show groups of 10 nucleotides print " 1".$space." 2".$space." 3 +".$space." 4".$space." 5".$space. " 6".$space." 7".$space." 8".$sp +ace." 9".$space." 10"."\n"; ### Prints numbers per nucleotide, per groups of 10. print "Line".$space."1234567890".$space."1234567890".$space."1 +234567890".$space."1234567890".$space."1234567890".$space."1234567890 +". $space."1234567890".$space."1234567890".$space."1234567890" +.$space."1234567890"."\n"; ## For loop to set 100 nucleotides as maximum length o +f each line my $line_number=0; for (my $i=0; $i<length($myseq); $i=$i+100){ $line_number=$line_number+1; my $seq100=substr($myseq,$i,100); ## If line number is greater than or equal to 10, print +with custom spacing. if ($line_number>=10) { print " $line_number "; } else { print " $line_number "; } ## Divides every 100 nucleotides into groups of 10 with spac +es. for (my $j=0; $j<length($seq100); $j=$j+10) { my $seq10 = substr($seq100,$j,10); print "$seq10".$space; ## ^ Prints nucleotide sequence with spacers incorpora +ted into it. } } }

    The end result I want is something like this, for multiple values (i.e. nucleotide sequences):

    1 2 3 4 5 6 Line 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1 GGAAAAACAT TTCATCTTAA AACTTTCCTA AGACAAGGGA AAACAAAAAA CCATGCTCTA 2 TTTTTCTTAC AAAGAAAAAT TTAATATTCG ATGAGAGGTT GAACCAGGCT TAAAGCAGAC 3 ATGGTGCAGC CTGTAAGAAT GCCAGTTTGT AAGTACTGAC TTTGGAAAAG ATCATCGCCT 4 CTTAGGGTCC TGGTCTGGCA ATTTTGGCCT GATGTGATGC CACAAGACCC AACAGAGAGA 5 CCAGGATAAT GTTGACAGTG GTGTAGCCCT TTAGGAGAAA TGGCGCTCCC TGCGGCTGGT 6 CATTGGCACC GAAGGAACCA GGAGGATAAG AATATCCATA ATTTCAGAGC TGCCCTGGCA 7 CCCGTCGGAG GCTCTCACTG GCAAATGACA GCTCTGTGCA AGGAGCACTC CCAAGTATAA 8 ACAGTTTTAT TCTGAAGAAC ATTTTGCATT TTAATAAAAA AGGATTTATG TCAGGAAAGA 1 2 3 4 5 6 Line 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1 GGAAAAACAT TTCATCTTAA AACTTTCCTA AGACAAGGGA AAACAAAAAA CCATGCTCTA 2 TTTTTCTTAC AAAGAAAAAT TTAATATTCG ATGAGAGGTT GAACCAGGCT TAAAGCAGAC 3 ATGGTGCAGC CTGTAAGAAT GCCAGTTTGT AAGTACTGAC TTTGGAAAAG ATCATCGCCT 4 CTTAGGGTCC TGGTCTGGCA ATTTTGGCCT GATGTGATGC CACAAGACCC AACAGAGAGA 5 CCAGGATAAT GTTGACAGTG GTGTAGCCCT TTAGGAGAAA TGGCGCTCCC TGCGGCTGGT 6 CATTGGCACC GAAGGAACCA GGAGGATAAG AATATCCATA ATTTCAGAGC TGCCCTGGCA 7 CCCGTCGGAG GCTCTCACTG GCAAATGACA GCTCTGTGCA AGGAGCACTC CCAAGTATAA 8 ACAGTTTTAT TCTGAAGAAC ATTTTGCATT TTAATAAAAA AGGATTTATG TCAGGAAAGA etc.

    Any suggestions are much appreciated.

Timeout Failing
3 direct replies — Read more / Contribute
by beckmanel
on Oct 23, 2017 at 10:07

    Gentlepersons - I surrounded db change code with a timeout, hoping that if there was a problem with the DB I/O, and it hung, the timeout would free up the program and allow it to continue. However, it does not seem to be working, and the program is hanging up. This is on a Windows 10 server. Did I miswrite the timeout, or is there an issue with Windows, or something else ?

    my $sth; my $res; eval{ local $SIG{ALRM} = sub { die "db_timeout" }; alarm 12; $sth = $dbh->prepare($sql); if (! defined $sth) { print LOG "ERROR: insert prepare failed\n"; } else { $res = $sth->execute; } alarm 0; }; if ($@) { if ($@ !~ /db_timeout/) { print LOG "ERROR: Alarm during insert execute othe +r than db_timeout: $@\n"; } else { print LOG "ERROR: db_timeout: $@\n"; } }
curl post
2 direct replies — Read more / Contribute
by bigup401
on Oct 23, 2017 at 08:27

    HTTP Error 411. The request must be chunked or have a content length.

    my $sender = $cgi->param('sender'); my $nums = $cgi->param('nums'); my $msg = $cgi->param('msg'); my $url = "link"; if ($sender, $nums && $msg) { my $json = encode_json { username => 'D2028', password => '8AEBC', "'{ \"to\": $nums, \"from\": \"$sender\" \"message\": \"$msg\" }'" }}; my $response = $http->post( $url => { content => $json, headers => { 'Content-Type' => 'application/json' }, }); $res = $response->{'content'};

    this is the api

    curl -X "POST" http://link -H 'username: tttt' -H 'password: ttt' -H " +Content-Type: application/json" -d '{"to": "444444","from":"Reach","m +essage":"cURL Message"}'
Server Issues and https-Requests
1 direct reply — Read more / Contribute
by amitsq
on Oct 23, 2017 at 07:56
    Hey all, you may or may not been aware that to my recent problems i got recommend to reinstall perl ActiveState. Since i couldn't do that (due to old programs which need to keep running for the company), i installed portable Strawberry Perl 5.26.1.1 64bit on Windows Server 2008 R2. My knowledge about Server matters is restricted, but following issues came up:

    1) I opened up an own application pool on the IIS-Manager for strawberry perl applications. Always when a module is not known or when trying to set a path a library (see code), it totally crashes and close down the pool with the msg:  HTTP Error 503. The service is unavailable. Any idea why and how to prevent that?

    2)Furthermore there is a proxy installed. So when using LWP-requests, i can not get content of https pages. The following code receives the following error msgs, which is strange since the demanded module is installed!

    LWP will support https URLs if the LWP::Protocol::https module is inst +alled.
    Update: /running the script via windows shell:
    can't connect to moz.com:443, the given host is not known at D:/Straw +berry_Perl/perl/vendor/lib/LWP/Pr otocol/http.pm line 46.
    Code:
    #!d:\Strawberry_Perl\perl\bin\perl.exe use strict; use warnings; use LWP::Protocol; use LWP::UserAgent; use Mozilla::CA; use CGI qw(:standard); use CGI::Carp 'fatalsToBrowser'; #use lib 'd:\\Strawberry_Perl\\perl\\vendor\\lib\\LWP\\Protocol\\'; #a +nother attempt which failed #use https; my $q= new CGI; print $q->header; my $B = new LWP::UserAgent (keep_alive => 1, agent => 'Mozilla/5.0', c +ookie_jar =>{}); my $proxy = 'http://localhost:5800'; my $Skript = "http:\/\/$ENV{SERVER_NAME}$ENV{SCRIPT_NAME}"; my $Server = "http:\/\/$ENV{SERVER_NAME}"; $B->proxy('http', $proxy); $ENV{HTTPS_PROXY} = $proxy; $ENV{'PERL_LWP_SSL_CA_PATH'} = "D:\\cert\\input\\certs"; $B->ssl_opts( SSL_ca_file => Mozilla::CA::SSL_ca_file() ); $B->ssl_opts( verify_hostname => 0 ); my $GET = $B->get('https://moz.com')->content; print $GET;
    Did I install Strawberry Perl right? I downloaded the portable version, transfered to the server, unpacked it, executed the bash file and then just set the path to the perl file ("hello world" script works)
Can't locate module added via APT
1 direct reply — Read more / Contribute
by mldvx4
on Oct 22, 2017 at 23:37

    I've tried adding a module with APT:

    # apt-get install libhtml-wikiconverter-markdown-perl

    and it seems to have put things in the right place

    # locate HTML | grep WikiConverter
    /usr/share/man/man3/HTML::WikiConverter::Markdown.3pm.gz
    /usr/share/perl5/HTML/WikiConverter
    /usr/share/perl5/HTML/WikiConverter/Markdown.pm
    /var/cache/man/cat3/HTML::WikiConverter::Markdown.3pm.gz

    Yet when I try the following:

    perl -e 'use HTML::WikiConverter;'

    I get this error:
    Can't locate HTML/WikiConverter.pm in @INC (you may need to install the HTML::WikiConverter module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at -e line 1.

    The manual says:

    SYNOPSIS use HTML::WikiConverter; my $wc = new HTML::WikiConverter( dialect => 'Markdown' ); print $wc->html2wiki( $html );
Installing Mail::Box
3 direct replies — Read more / Contribute
by gsd4me
on Oct 22, 2017 at 17:33

    Can anyone tell me please how to install Mail::Box (and its constituents) as I am failing miserably:

    I am using W10, PPM and when I do ppm install Mail::Box I get

    Can't find any package that provides Mail::Message for Mail::Box

    and

    Can't find any package that provides Mail::Reporter for Mail::Transport

    aren't these sort of things supposed to be recursive?
Moose boolean type values: Noob
2 direct replies — Read more / Contribute
by jorba
on Oct 22, 2017 at 16:54
    I have a moose class with a boolean attribute. When I create a new instance of that class I try to pass either a 0 or 1 (perl boolean vals I thought) I get an error stating this is not a valid value. Either that or I'm misunderstanding the error message.

    Here's the package.

    package AXField; # Our libraries use lib 'C:\Users\Jay\Desktop\SBS DEV\CODE\perl\Utilities'; use Moose; # Attributes has 'Name' => (is => 'rw', isa => 'Str', required => 1); has 'Value' => (is => 'rw', isa => 'Str', reader => 'Value', writer => + 'SetValue'); has 'isKey' => (is => 'rw', isa => 'Boolean'); has 'Len' => (is=>'rw', isa => 'Num'); has 'DecimalPos' => (is => 'rw', isa => 'Num'); has 'Type' => (is => 'rw', isa => 'Str'); has 'OriginalValue' => (is => 'rw', isa => 'Str'); has 'ParentRec' => (is => 'rw', isa => 'Str', required => 1); has 'Populated' => (is => 'rw', isa => 'Str'); # Contains a single field sub BUILD # Constructor { my $self; $self = shift; $self->Populated(0); $self->OriginalValue(' '); $self->SetValue(' '); } sub Value { my $self; $self = shift; return $self->Value; } sub SetValue { my $self; $self = shift; $self->Value = shift; if ($self=>Populated() eq 'N') { $self=>Populated() eq 'Y'; } } 1;
    Here's the line that creates a new "Field"
    &Fld = AXField->new(Name => $Prop[0], Value => ' ', isKey => 1, Len => + $Length, DecimalPos => $prec, Type => $Prop[4], ParentRec => $self-> +Name);
    We're interested in the isKey attribute.

    Here's the error message

    Attribute (isKey) does not pass the type constraint because: Validatio +n failed f or 'Boolean' with value 1 (not isa Boolean) at C:\Perl64\lib\Moose\Obj +ect.pm lin e 24
    What am I missing
porting C code to Perl
4 direct replies — Read more / Contribute
by Discipulus
on Oct 22, 2017 at 15:27
    Hello nuns and monks,

    if you know me or not, I'm completely unaware of other programming languages; I just know a little Perl but I found myself in the rare situation where I need to translate a little code from C to Perl.

    Infact I rapidly (*) understood that is a task to be done by hand, and I've been told many times that these two languages share a lot in their syntax.

    My attempt is below and does not produces the output I expected (**).

    I looked a bit to some description of the C syntax to try to understand if there were some difference between the C operator and the correspective Perl's one. For example for ++ autoincrement or arithmentics ones. I found nothing relevant: many operators seems to act the same. Doubts remain about the C array syntax ( int A[len] ?? that I read as the elelment len of the array A is an int but..).

    Here below my attempt: can someone be so kind to point me where I lost in the translation? After a plain translation, when I possibly end with some working Perl code I'll arrange it into a more perlish version.

    use strict; use warnings; # #include <math.h> # #include <stdio.h> # #define N 100 my $n=100; # int len = floor(10 * N/3) + 1; my $len = 1 + int (10 * $n / 3); # int A[len]; my @a; $#a=$len-1; #? -1 ???? # for(int i = 0; i < len; ++i) {A[i] = 2;} for (my $i = 0; $i < $len; $i++){ $a[$i]= 2; } # int nines = 0; my $nines = 0; # int predigit = 0; my $predigit = 0; # for(int j = 1; j < N + 1; ++j) { for (my $j = 1; $j < $n + 1; ++$j){ # int q = 0; my $q = 0; # for(int i = len; i > 0; --i) { for(my $i = $len; $i > 0; $i--){ # int x = 10 * A[i-1] + q*i; my $x = 10 * $a[$i-1] + $q * $i; # A[i-1] = x % (2*i - 1); $a[$i-1] = $x % (2 * $i - 1); # q = x / (2*i - 1); } $q = $x / (2 * $i - 1); } # A[0] = q%10; $a[0]=$q%10; # q = q/10; $q=$q/10; # if (9 == q) { ++nines;} if (9 == $q){ ++$nines; } # else if (10 == q) { elsif(10 == $q){ # printf("%d", predigit + 1); printf("%d", $predigit + 1); # for (int k = 0; k < nines; ++k) { printf("%d", 0); } for (my $k = 0; $k < $nines; $k++){ printf("%d", 0);} # predigit, nines = 0; $predigit = $nines = 0; # } } # else { else{ # printf("%d", predigit); printf("%d", $predigit); # predigit = q; $predigit = $q; # if (0 != nines) { if(0 != $nines){ # for (int k = 0; k < nines; ++k) { for (my $k = 0; $k < $nines; $k++) { # printf("%d", 9); printf("%d", 9); # } } # nines = 0; $nines = 0; } } } # printf("%d", predigit); printf("%d", $predigit);

    (*) Not so rapid: I found a thread here at PM with a link to a C to perl translator, but I missed the <ironic> tags and my hands called rapidly gcc -P -E file.c > file.pl using the compiler I have shipped within strawberry perl. Was not useful and the next line gcc -Larry -Wall file.c > file.pl revealed me it was an humoristic faq or well a iaq..

    (**) Well i wanted to verify that the C code printed what I expected and I tried blindly using gcc to compile it (??) and using Inline::C but I had no success to not even install it on my strawberry perl.

    If you need my C source you can, obviously perl -lne 'print if s/\s?#\s//' my_post.pl

    Thanks

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
How do tell Perl to break only on undef?
1 direct reply — Read more / Contribute
by myuserid7
on Oct 22, 2017 at 05:18

    Hello, while working on a package I ran into a problem for which I could not find a pretty solution. I broke things down to come up with the example code below to demonstrate my issue. When this script is executed, it will print only "12568", because 0 is considered as false and this breaks the while loop. Now I could use while(1) and add some more lines to check $i for undef and then do a "last", but the package is supposed to be used by others so I want to keep it as simple as possible. What would be a pretty/easy way to make while only treat undef as a breaking argument?

    #!/usr/bin/perl -w use strict; use warnings; my $t = test->new(); while (my $i = $t->get) { print $i; }; package test; use strict; use warnings; my @list; sub new { my $self = shift; my $this = {}; @list = ( 1, 2, 5, 6, 8, 0, 9 ); bless($this, $self); return($this); }; sub get { return(shift(@list)); }; 1;

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


  • 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.