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
Looping through multiple arrays
2 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 05, 2015 at 06:30
    Hi I would like to loop through multiple arrays size of the array might not be same. I need to insert the data from the array to database
    @arrray1 = ['a','b','c']; @array2 = ['a','b']; @array3 = ['a','b','c','d'];
    while(my($array1,$array2,$array3) = $iteration->()){ warn " iteration : $array1,$array2,$array3"; $sthgetCompany->execute($aray2) or die 'Cannot execute the statement'. $dbh_oracle->errstr;; #insertion to the database goes here. } # end of while loop #.

    I tried using ListMore::Utils each_array but its trying to insert for 9 times with null statements where it can't find pairs from each array. How can I handle multiple arrays with different size.

Trying to identify unique lines in a log file
2 direct replies — Read more / Contribute
by aditya1977
on Mar 05, 2015 at 05:21

    I'm writing a program that will parse a log file and write data to a MySQL database based on the content of the log file.

    The script will run via cron and I want to make sure I skip lines that have been parsed before.

    One idea I had was to base 64 encode the line and make this a unique key in the database. Then the next time the script runs, if there is a matching key, skip the line.

    What I don't like about this is that the encoded line is quite a long string, which would make viewing the database difficult to read. Is there a way to encode a long string to a relatively short string string?

    Or perhaps there is a better way to do this?

Click on "any" href
2 direct replies — Read more / Contribute
by annonimous
on Mar 05, 2015 at 03:24
    Hi perl monks, im a new soul looking for wisdom im making a simple WWW::Mechanize routine here it is my code:
    #!/usr/bin/perl # use strict; use WWW::Mechanize; my $target = "http://www.somewebpage.com/subscription/showsubscribe"; my $mech = WWW::Mechanize->new(agent=>$useragent); $mech->get($target); my $response = $mech->click_button(value=>"subscriptionTop", value=>"loadingSubscript +ion();"); $mech->submit(); die "Error at '$target'\n", $response->status_line, "\n Aborting" unless $response->is_success; $response = $mech->response; for my $key ($response->header_field_names()) { print "response[$key] = ", $response->header($key), "\n"; } #print $mech->content;
    then on my linux box im getting something like click_button: No form has been selected at ./mech.pl line 9 in the code of the webpage i have it is showing:
    <input type="image" src="/image/suscribe-btn.png;jsessionid=uK6psR994V +LpkeMzmEEVte9n" onclick="loadingSubscription();" class="img_content_i +mg_home" id="subscriptionTop">
    any tip of where it is my fault? im reading in the cpan about www::mechanize but i cant get how to click on this link thanks for your help!
registry dump to file output differs from DOS cli and system ?
1 direct reply — Read more / Contribute
by gepebril69
on Mar 05, 2015 at 03:09

    Hi there

    I try to automate some stuff on Windows. One of this items is dumping the Windows registry to a file. When being admin (in cli and running Perl code) the output differs over 100Mbyte. The output on CLI is bigger.

    On command line

    regedit /e /y e:\temp\regdump.reg # result file(332Mb)

    In Perl

    $DumpFile = "e:\\temp\\registry.reg"; system("regedit", "/e", "/y", $DumpFile); #(resultfile 208Mb)

    What am I doing wrong?/Bug in Perl/Something else?

    I'm running on Windows 8

Generic multiline grepping
3 direct replies — Read more / Contribute
by vasuperl
on Mar 05, 2015 at 02:24
    Hi I need to parse a logfile in which i should do multiline grepping. Logfile: This logfile contains logs of multiple lines.In this file i need to grep three patterns. Eg logfile: Tm500 is tried to execute an attach script.
    rrcConnectionRelease{ attach request measurementReport{ Event A1 Id 13 Purpose . . . . . . .so on lines
    my $logpattern1=rrcConnection; my $logpattern2=measurementReport; my $logpattern3=Event; open(LOGFILE,"$filepath"); my @file=<FILE>; my $f=join("",@file); if ($f=~/.*$logpattern1.*?$logpattern2.*?$logpattern3/gs){ &statuslog("Found log marker : $logpattern1 $logpattern2 $logpattern3 + in TM500log\n"); } else{ &errorlog("Failed to find log marker: $logpattern2 $logpattern3 in $lo +gpattern1 in TM500log\n"); } close(LOGFILE);
    I had tried this code, its working. But i need a generic way where in i can grep for more than three log patterns. Is it possible to grep these patterns by storing into an array variable? Any help would be appreciated. Thanks in advance
Look Behind issues
3 direct replies — Read more / Contribute
by dominic01
on Mar 04, 2015 at 23:22

    I have a line like
    $Line = "Please;, read these&#xB0;, before you post 180;, if you're not absolutely &#x2013;, sure you're posting";

    I am trying to replace the ";," with ";". However it should not replace if the comma follows an entity.

    My output should be
    $Line = "Please; read these&#xB0;, before you post 180; if you're not absolutely &#x2013;, sure you're posting";

    Appreciate any help on framing the regex. I am using perl 5, version 20, subversion 1 (v5.20.1) built for MSWin32-x64-multi-thread

Changing path to home directory
2 direct replies — Read more / Contribute
by The_Last_Monk
on Mar 04, 2015 at 17:49
    Fellow Monks, The question i have to ask is regarding chdir. Apparently, if you call chdir without an argument, perl best tries to determine your home directory and changes the path to it. So what i tried to do was this
    if(/$/){ chdir or die "Unable to locate home"; }
    the die message always got thrown. This is for using on a windows comp. Am i calling chdir the wrong way?
Read / Write Server
4 direct replies — Read more / Contribute
by ShaZe
on Mar 04, 2015 at 16:19

    Hello Perl Monks!

    I have a question related to the read/ write operations happening on a server. I currently have a basic script that log each time a name and a time in a file. If the time is lower than the ones in the list, they are moved higher in the list. I then save the list and close the file.

    The code is functional and there isn't any error seen when the function is tested alone. However in a real case scenario, the file often appear incomplete. The file will end at a partially written name or time.

    Do you have any idea what could be wrong?

    Here's the code I have :

    #!/usr/bin/perl -- ################################################ use strict; use warnings; use Fcntl qw(:flock :seek); print "Content-type: text/html\n\n"; UpdateInfo("testname1", "240"); sub UpdateInfo { my $name = $_[0]; my $time = $_[1]; my $file = "file.dat"; my $lock = "file.sem"; open SEM, ">$lock" or die "Can't write-open $lock: $!"; flock SEM, LOCK_EX; my $output; my @lines; if(-e $file) { open(LEVELINFO, $file); while (defined(my $line = <LEVELINFO>)) { chomp $line; push (@lines, $line); } close(LEVELINFO); $lines[0] = "header\t0\t0\n"; my $currentEntry = 0; for (my $i = 1; $i < @lines; $i++) { (my $currName, my $currTime) = split(/\t/, $lines[$i]); if($currName eq $name) { if($time < $currTime) { $currentEntry = $i; } } $lines[$i] = $lines[$i] . "\n"; } if($currentEntry != 0) {splice @lines, $currentEntry, 1;} my $boolAdded = 0; for (my $i = 1; $i < @lines; $i++) { (my $currName, my $currTime) = split(/\t/, $lines[$i]); if($boolAdded == 0 and $time < $currTime) { splice @lines, $i, 0, "$name\t$time\n"; $boolAdded = 1; } } if(@lines < 11 and $boolAdded == 0) { push @lines, "$name\t$ti +me\n"; } open(LEVELINFO, ">$file"); for my $i (0..$#lines) { print LEVELINFO $lines[$i]; if($i > 0) { $output = $output . "$lines[$i]"; } } close(LEVELINFO); } else { push @lines, "header\t0\t0\n"; push @lines, "$name\t$time\n"; open(LEVELINFO, ">$file"); for my $i (0..$#lines) { print LEVELINFO $lines[$i];} close(LEVELINFO); } close(SEM); unlink($lock); return $output; }
    Let me know what you think
Regex match and replace
4 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 04, 2015 at 12:24

    Hi Monks, I'm new to Perl and trying to figure out how to only replace matches after the initial match using regex. For example, I would like to replace any matches of bar with foo after finding the first bar.

    Input foofoo
    Output foobar

    I have tried what I believe is look ahead assertion

    $s = s/bar(?=bar)/foo/;

    However it seems to get rid of everything and print an empty string.

HTML::ENTITIES for URL encoding
1 direct reply — Read more / Contribute
by aakashmgupta
on Mar 04, 2015 at 11:50
    i am not sure what is wrong with this code, i am trying to remove special characters from the URL for example:- http://google.com/>"<script>alert(2121)</script> this triggers the javscript, i want to encode the '<>&"' from URL so this is my code which i am trying to execute.
    my $cgi = CGI->new(); use HTML::Entities; sub escapeParams { my( $self ) = shift; my @params = $self->query->param('<>&"'); foreach my $param ( @params ) { # $self->query->param( $param, HTML::Entities::encode( +$self->query->param( $param ) ) ); $self->query->param( HTML::Entities::encode($param), HTML: +:Entities::encode( $self->query->param( $param ) ) ); print $self->query->param( $param ); } } escapeParams($cgi);

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.