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
Removing partially duplicated lines from a file
4 direct replies — Read more / Contribute
by Sandy_Bio_Perl
on Jul 26, 2016 at 11:52

    Hello Monks. I am trying to remove all lines where parts of the line are duplicated. In the file below, I would like to remove all 'duplicate' lines (leaving the first one) where column 2 (HLA) e.g. HLA-A*11:01 and column 3 (Peptide) e.g. YVNVNMGLK are the same. I would like to leave the other lines intact. I am a bit flummoxed, having tried and failed with regex!

    ---------------------------------------------------------------------- +------------- Pos HLA Peptide Core Of Gp Gl Ip Il Ic +ore Identity Score Aff(nM) %Rank BindLevel ---------------------------------------------------------------------- +------------- 117 HLA-A*11:01 YVNVNMGLK YVNVNMGLK 0 0 0 0 0 YVNVNM +GLK GQ924620_HBe_C_ 0.62268 59.3 0.40 <= SB 28 HLA-A*11:01 WGMDIDPYK WGMDIDPYK 0 0 0 0 0 WGMDID +PYK GQ924620_HBe_C_ 0.44617 400.4 1.60 <= WB 133 HLA-A*11:01 HISCLTFGR HISCLTFGR 0 0 0 0 0 HISCLT +FGR GQ924620_HBe_C_ 0.43660 444.0 1.70 <= WB ---------------------------------------------------------------------- +------------- Pos HLA Peptide Core Of Gp Gl Ip Il Ic +ore Identity Score Aff(nM) %Rank BindLevel ---------------------------------------------------------------------- +------------- 47 HLA-A*02:05 YVNVNMGLK FLPSDFFPS 0 0 0 0 0 FLPSDF +FPS X02763_HBe_A_po 0.77090 11.9 0.08 <= SB 40 HLA-A*02:05 ATVELLSFL ATVELLSFL 0 0 0 0 0 ATVELL +SFL X02763_HBe_A_po 0.75279 14.5 0.10 <= SB 1 HLA-A*02:05 MQLFHLCLI MQLFHLCLI 0 0 0 0 0 MQLFHL +CLI X02763_HBe_A_po 0.66669 36.8 0.30 <= SB 9 HLA-A*02:05 IISCTCPTV IISCTCPTV 0 0 0 0 0 IISCTC +PTV X02763_HBe_A_po 0.52206 176.1 1.40 <= WB 147 HLA-A*02:05 YLVSFGVWI YLVSFGVWI 0 0 0 0 0 YLVSFG +VWI X02763_HBe_A_po 0.51724 185.5 1.40 <= WB 55 HLA-A*02:05 SVRDLLDTA SVRDLLDTA 0 0 0 0 0 SVRDLL +DTA X02763_HBe_A_po 0.49966 224.4 1.70 <= WB 114 HLA-A*02:05 VVNYVNTNV VVNYVNTNV 0 0 0 0 0 VVNYVN +TNV X02763_HBe_A_po 0.48729 256.6 1.80 <= WB 93 HLA-A*02:05 ELMTLATWV ELMTLATWV 0 0 0 0 0 ELMTLA +TWV X02763_HBe_A_po 0.46686 320.0 2.50 8 HLA-A*02:05 LIISCTCPT LIISCTCPT 0 0 0 0 0 LIISCT +CPT X02763_HBe_A_po 0.45053 381.9 2.50 ---------------------------------------------------------------------- +------------- Pos HLA Peptide Core Of Gp Gl Ip Il Ic +ore Identity Score Aff(nM) %Rank BindLevel ---------------------------------------------------------------------- +------------- 117 HLA-A*11:01 IISCTCPTV YVNVNMGLK 0 0 0 0 0 YVNVNM +GLK AB219428_HBe_B_ 0.62268 59.3 0.40 <= SB 28 HLA-A*11:01 WGMDIDPYK WGMDIDPYK 0 0 0 0 0 WGMDID +PYK AB219428_HBe_B_ 0.44617 400.4 1.60 <= WB 133 HLA-A*11:01 HISCLTFGR HISCLTFGR 0 0 0 0 0 HISCLT +FGR AB219428_HBe_B_ 0.43660 444.0 1.70 <= WB

    Many thanks for any help you are able to give

Error in HTTPS connection with error: No connection could be made because the target machine actively refused it. at C:/Perl/lib/LWP/Protocol/http.pm line 47
2 direct replies — Read more / Contribute
by jliu5
on Jul 26, 2016 at 11:36

    Hi Monks,

    I am trying to create a REST client to do a simple GET service at a https server, but got this error.

    Here is my sample code, please advice.:

    #! /usr/bin/env perl use REST::Client; use MIME::Base64; my $username = 'me'; my $password = 'me!!'; my $headers = {Accept => 'application/xml', Authorization => 'Basic ' +. encode_base64($username . ':' . $password)}; my $client = REST::Client->new(); $client->getUseragent->ssl_opts(verify_hostname => 0); $client->getUseragent->ssl_opts(SSL_verify_mode => SSL_VERIFY_NONE); $client->getUseragent->no_proxy('server_ip_string', 'server_ip_string' +); $client->GET("https://server_ip_string:port/upm-server/nbi/organizatio +n", $headers ); my $response = $client->responseContent(); print $response;
Extracting Tag and attributes from a Web Page
2 direct replies — Read more / Contribute
by Kaustubh
on Jul 26, 2016 at 08:23
    Hi Monks,

    My programming experience with perl is just 2 weeks.I am stuck with a problem.I need to get the value $8.39 from the following code snippet.This code snippet is obtained by selecting the view source option when we right click a web browser:-

    <span class="details-title-text"> <strong>$8.39</strong> </span>
    I am using the mechanize package.I have tried various permutations and combinations of find() & attr() methods.Basically whatever I have tried is throwing errors:-

    foreach my $b ($mech->find('span')) { my $attr_class= $b->attr('class'); if(defined($attr_class) ) { my $tag_strong=$mech->find('strong'); print Dumper($tag_strong); } }
IO::Compress::Gzip file close issues
2 direct replies — Read more / Contribute
by Paul.Unix
on Jul 26, 2016 at 07:08
    I encountered an issue with IO::Compress::Gzip. After a successful gzip I want to unlink the original file but sometimes the unlink fails with a "permission denied" I made a pragmatic work around by adding a delay and try the unlink again. But a good solution would be preferred. The same issue happens with moving the new .gz to another directory. The files are not in use by another process. Running Strawberry Perl 5.22.0 on Windows 2008 and 2012
    use warnings; use strict; use POSIX; use Cwd; use Cwd 'abs_path'; use Time::Local 'timelocal_nocheck'; # used for SCHEDULE option in +the MQX.ini use Net::Domain qw(hostname hostfqdn hostdomain domainname); use File::Basename; use File::Copy; use IO::Compress::Gzip qw(gzip $GzipError) ; use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; # module used in the state messages use Digest::MD5; # modules used in put mode use threads; use threads::shared; use Thread::Queue; if ( gzip $archfile => "$archfile.gz", AutoClose => 1, BinModeI +n => 1 ) { my $delete_try_counter = mqx_get_setting('ARCHIVEMOVETRY'); while ( -f $archfile and not unlink $archfile and $dele +te_try_counter != 0 ) { $global_EVENTLOG_IN_LQ->enqueue("WARNING, $arch_ha +shref->{'EVENTLOG'},Can not delete $archfile. Error: $! try: $delete_ +try_counter"); $delete_try_counter -= 1; sleep(mqx_get_setting('ARCHIVEMOVEWAIT')); # wait x sec +onds before retrying } $archfile .= ".gz"; # add .gz to the file name } else { # if the gzip failed try to arc +hive the original file $global_EVENTLOG_IN_LQ->enqueue("ERROR, $arch_hashref->{' +EVENTLOG'}, gzip failed on $archfile\n$GzipError"); }
how to get active web page by Perl
1 direct reply — Read more / Contribute
by liuweichuan
on Jul 26, 2016 at 02:45
    how to get flighting info from this web site. http://flight.qunar.com/site/oneway_list.htm?searchDepartureAirport=%E5%A4%A7%E8%BF%9E&searchArrivalAirport=%E5%8C%97%E4%BA%AC&searchDepartureTime=2016-07-28&searchArrivalTime=2016-07-31&nextNDays=0&startSearch=true&fromCode=DLC&toCode=BJS&from=flight_dom_search&lowestPrice=null i used these code, but i cann't get any info.
    use LWP::UserAgent; use Encode; my $content = LWP::UserAgent->new->get('http://flight.qunar.com/site/o +neway_list.htm?searchDepartureAirport=%E5%A4%A7%E8%BF%9E&searchArriva +lAirport=%E5%8C%97%E4%BA%AC&searchDepartureTime=2016-07-28&searchArri +valTime=2016-07-31&nextNDays=0&startSearch=true&fromCode=DLC&toCode=B +JS&from=flight_dom_search&lowestPrice=null')->content;
    who know how to do it, get an active page or ajax page, plz help me get this url's web info.
how to save dynamically added html rows to database using perl
3 direct replies — Read more / Contribute
by marichika
on Jul 26, 2016 at 01:20
    <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" /> <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTa +ble')" /> <TABLE id="dataTable" width="900px" align="center" border="1"> <TR> <TH>Chk</TD> <TH>From</TH> <TH>TO</TH> <TH>Activities Performed</TH> <TH>Format</TH> <TH>Time</TH> </TR> </TABLE> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var cell1 = row.insertCell(0); var element1 = document.createElement("input"); element1.type = "checkbox"; element1.name="chkbox[]"; cell1.appendChild(element1); var from = row.insertCell(1); var element2 = document.createElement("input"); element2.type = "text"; element2.style['width'] = '100px'; element2.name = "txtbox[]"; from.appendChild(element2); var to = row.insertCell(2); var element3 = document.createElement("input"); element3.type = "text"; element3.style['width'] = '50px'; element3.name = "txtbox[]"; to.appendChild(element3); var act = row.insertCell(3); var element4 = document.createElement("input"); element4.type = "text"; element4.style['width'] = '500px'; element4.name = "txtbox[]"; act.appendChild(element4); var format = row.insertCell(4); var element5 = document.createElement("input"); element5.type = "text"; element5.style['width'] = '50px'; element5.name = "txtbox[]"; format.appendChild(element5); var time = row.insertCell(5); var element6 = document.createElement("input"); element6.type = "text"; element6.style['width'] = '50px'; element6.name = "txtbox[]"; time.appendChild(element6); } function deleteRow(tableID) { try { var table = document.getElementById(tableID); var rowCount = table.rows.length; for(var i=0; i<rowCount; i++) { var row = table.rows[i]; var chkbox = row.cells[0].childNodes[0]; if(null != chkbox && true == chkbox.checked) { table.deleteRow(i); rowCount--; i--; } } }catch(e) { alert(e); } } </script>
    Here is my perl script
    <table width='100%' > <tr><td align=center><input type=button name='add' value='Add' + onclick='Add_Form(this.form)' accesskey="s" title="alt+s"></td></tr> + </table> function Add_Form(frm) { alert('came'); var url = 'http://newsdpr.etpl.org/dpr/Add_App_Entry?s +ession=$session_id'; frm.action=url; frm.submit(); }
    how to send this dynamically added table data to Add_App_Entry page? Thanks in advance Lakshmi
Error saying “Undefined subroutine xxx called at…”
2 direct replies — Read more / Contribute
by bill5262
on Jul 25, 2016 at 22:01

    I have a module imported in my main perl file as the following:

    use rns_fsq_jw;

    The above contains a bunch of sub functions, and it seems to be working well except for one sub fuction:

    ConnectFSQ()

    This what I have under the rns_fsq_jw library:

    sub ConnectFSQ #ipaddress { (do something blah blah blah) }

    However, when I call the ConnectFSQ() function from my main script, I get the error saying "Undefined subroutine &main::ConnectFSQ called at ..."

    my $IP_FSQ = "xxx.xxx.xxx.xxx"; # ip address ... (blah blah blah) ConnectFSQ($IP_FSQ);

    Am I doing something wrong?

print all files is soo slow! Why?
4 direct replies — Read more / Contribute
by harangzsolt33
on Jul 25, 2016 at 21:30

    I want to write a simple perl program that goes through the current folder recursively and looks at each item, if it's a file, print its contents and moves on to the next one.

    I am a beginner Perl programmer, and I don't know why this is happening, but this perl program stops responding, and then I have to kill it every time.

    I am using TinyPerl 5.8 under Windows 7.

    I am testing this program in a folder that has about 50 text files and no folders. When I launch my perl program, it seems to work perfectly fine, but it doesn't go back to command prompt. It just hangs. The cursor stops blinking, and I have to kill the process.

    If I only read the first 100 bytes of each file, then there's no issue. I am back to command prompt immediately. If I read the first 2000 bytes, then there's a 3-second delay. And I tried reading the entire file and print the content, but then after executing the script, it stopped responding and I had to kill it.

    use strict; use warnings; my $PATH = '.'; my $CONTENT; explore($PATH); sub explore { my $PATH = shift; my $FILE; my $SUB; opendir(my $DIR, $PATH) or return; while (my $SUB = readdir $DIR) { next if $SUB eq '.' or $SUB eq '..'; $SUB = "$PATH/$SUB"; # If it's a folder, explore it. # If it's a file, print it. if (-d $SUB) { explore($SUB); next; } if (-f $SUB) { open($FILE, '<:raw', $SUB) or next; read($FILE, $CONTENT, 2000) or next; close($FILE); print $CONTENT; $CONTENT = ''; # don't need this data anymore } } close $DIR; }
perl dancer route template hashref pass complex json file to server issue
2 direct replies — Read more / Contribute
by RamiD
on Jul 25, 2016 at 16:11
    Hi , I have a problem with perl dancer post parameter to server with template hashref way, the param is json object, if the json object is simple , it work fine mean that perl subroutine template hashref send the json param obj as it should but if I have more complex json file eg. {"name":"jsonFileexample","problem":"thisIsThrProblem\"withComma"} if this is the json file with comma inside one of the json file param , perl hash ref delete the \ from the file , it will look like this {"name":"jsonFileexample","problem":"thisIsThrProblem"withComma"} which will be not a valid json file , how can I send paran (json file) from perl dancer route template param to server as is , without perl changing my json file !!
@INC not set for perlbrew
3 direct replies — Read more / Contribute
by Alessandro
on Jul 25, 2016 at 14:07

    Hi monks,

    I am on os x El capitan and have perlbrew installed. However, when I install a perl module I can't get it working, because it seems perl is not looking at the right place.

    Can't locate WWW/Mechanize.pm in @INC (you may need to install the WWW::Mechanize module) (@INC contains: /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2/darwin-thread-multi-2level /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .)

    also

     which perl returns  /opt/local/bin/perl5

    my understanding is that it should return the path to perlbrew install and not the system perl.

    and  perl -v returns  This is perl 5, version 22, subversion 2 (v5.22.2) built for darwin-thread-multi-2level and I indeed used perlbrew to install and switch to perl 5.22.2 but it seems somehow, the "switch" was only partial. What am I missing here?

    Thanks


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.