Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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
Capture error from one-liner
1 direct reply — Read more / Contribute
by dirtdog
on Sep 19, 2014 at 12:04

    Hi Monks, Does anyone know how i would capture the return code from a perl one-liner executed from a korn shell script?

    perl -i.bak -pe 's/AJZ/AZZ/g if /^AB/' <filename> if [[ $? != 0 ]];then exit 1 fi

    if the one-liner fails for any reason I'd like to be able to exit with a failed status from the shell, but what i have doesn't work.

    any help would be greatly appreciated. thanks

perl : Print a perl hash inside HTML tags in a tabular format
2 direct replies — Read more / Contribute
by sravs448
on Sep 19, 2014 at 11:32
    I am trying to send a html email (report) about the values in a hash. But I couldnt print the hash values in a table format. I am using HTML::Mason to execute my perl commands (looping through a hash) and printing it in the end of report. But how can I execute my perl code to loop through hash inside html ?
    use HTML::Entities; use Mail::Sendmail; use MIME::Lite; use Term::ANSIColor; use Time::localtime; use HTML::Mason; my $ti = localtime; my ($day,$month,$year)=($ti->mday,$ti->fullmonth,$ti->year); # DEFINE A HASH %coins = ( "Quarter" , 25, "Dime" , 10, "Nickel", 5 ); $html = <<END_HTML; <table border='1'> <th>Keys</th><th>Values</th> <%perl> while (($key, $value) = each(%coins)){ </%perl> <tr><td>$key</td>; <td>$value</td></tr>; <%perl> } </%perl> </table>; END_HTML $msg = MIME::Lite->new( from => '', To =>'', Subject =>'Report', Type =>'multipart/related' ); $msg->attach( Type => 'text/html', Data => qq{ <body> <html>$html</html> </body> }, ); $msg->attach( Type => 'image/gif', Id => 'banner.jpg', Path => 'C:\Users\Pictures\banner.jpg', ); MIME::Lite -> send ('smtp','' ); $msg -> send;
perl POST question in HTML
No replies — Read more | Post response
by justinzollar
on Sep 19, 2014 at 11:09

    I have an HTML/Perl form that allows a user to input data. The form where users input the data is called, and the form receiving the data is called has POST. is where I am having an issue. On there is a text box called "txtName" and a person will enter in a name. On, I need a varaible to accept the value entered, and set it as the variable "$Name". Can someone please help me figure out what I need on the page in order to accept the data? Here are my use statements at the top of

    use CGI::Carp 'fatalsToBrowser'; use warnings; use Net::SMTP; use HTTP::Request::Common qw(POST); use LWP::UserAgent;
    right now, I'm using "get" and have this to extract the data out of the URL and assign it to variables.
    for $parm (@PARM) { chomp ($parm); $parm =~ s/[+]/ /g; $parm =~ s/%([0-9a-fA-F]{2})/pack ('c', hex ($1))/eg; if ($parm =~ /^[a-z0-9]+ *=/i) { ($parmname, $parmvalue) = split (/ *= */, $parm, 2); if ($parmname eq 'txtRequester') { $Reviewer = $parmvalue; } elsif ($parmname eq 'person') { $Person = $parmvalue; } elsif ($parmname eq 'team') { $Team = $parmvalue; } else { $TIME{$parmname} = $parmvalue; } } }

    any help would be GREATLY appreciated.

Parallel::ForkManager and CPU usage?
1 direct reply — Read more / Contribute
by janmartin
on Sep 19, 2014 at 11:00

    This is about Parallel::ForkManager on 64 bit strawberryperl.

    I need to process 1000s of images. Each totally independent of the others.

    Processing has CPU intensive parts, and parts that hardly use the CPU, but the GPU instead. The hardware has 8 cores, so I think of 8 processes? Each process to start multi-thread programs.

    Each image is of different size, and I know the processing time is different according to it. Therefore processes should run a different length.

    If a process is finished, a new process should start. To always have 8 processes.

    This should level the CPU usage after a few rounds due to the different time each process runs. But it doesn't.

    Looking at the Windows 8 Resource Monitor I see that all 8 processes run perfectly synchronized even after a long time.

    It looks like 8 process are started, then only after all 8 are finished 8 new processes are started?

    Code is straight from:

    use Parallel::ForkManager; ... @links=( ["","rulez_data.txt"], ["","more_data.doc"], ... ); ... # Max 30 processes for parallel download my $pm = Parallel::ForkManager->new(30); foreach my $linkarray (@links) { $pm->start and next; # do the fork my ($link,$fn) = @$linkarray; warn "Cannot get $fn from $link" if getstore($link,$fn) != RC_OK; $pm->finish; # do the exit in the child process } $pm->wait_all_children;
read CSV file line by line and create hash table
2 direct replies — Read more / Contribute
by lshokri02
on Sep 19, 2014 at 10:53

    This is my first time writing perl and I want to take a CSV file which looks similar to this:

    mask No. , block, size, base addr (hex), end addr (hex)--about 20 h +eader names fpg23, b002, 16384, 60000, 63FFF fpg23, b002, 16384, 800000, 803FFF fpg23, b003, 0, F00000, F00000 fpg23, b003, 4, F00000, F00004 fpg23, b003, 8, F00000, F00008 --so on for a variable amount of lines

    so each line is represented as its own block, but I want to be able after reading the file, to sort the file by base address and be able to access other information in its line such as the end addr. my coworker said to use hashes, so I'm thinking of a multidimensional hash. I'm thinking that the column names are the keys of the hash, so can I sort a specific key (aka Base addr. (hex) key? this is my current code, from what I understand it is making the column name keys and it is reading line by line, but those values, are they private to that while loop? I believe I have to incorporate them in my hash table that I'm calling %reghash, just unclear of how to do that. I'd appreciate any help/feedback. Thank you!

    #!/usr/bin/env perl use strict; use warnings; use diagnostics; use Text::CSV_XS; my $filename = 'test1.csv'; my %reghash; open(my $fh, '<', $filename) or die "Can't open $filename: $!"; my $csv = Text::CSV_XS->new ({ binary => 1, #allow special character. always set this auto_diag => 1, #report irregularities immediately }); $csv->column_names ($csv->getline ($fh)); #use header while (my $row = $csv->getline_hr ($fh)){ printf "Base Address: %s\n", $row->{"Base Addr. (Hex)"}; } close $fh;
Constructing HTML tables with
1 direct reply — Read more / Contribute
by sravs448
on Sep 19, 2014 at 10:00
    Hi Monks,
    I am trying to create a html table. I followed the instructions in the Re: Constructing HTML tables with
    What am I missing here?
    #!/usr/local/bin/perl -w use strict; use CGI; my %host=( 'camel', 'flea', 'frog', 'green'); cgiout(); sub cgiout { my $q= new CGI; my $tablecontent=[$q->th(['key', 'value'])]; for (sort keys %host) { push @$tablecontent, $q->td([ $_, $host{$_} ]) ; } print $q->table( { border => 1, -width => '100%'}, $q->Tr( $tablecontent), ); }

    The output I got

    <table width="100%" border="1"><tr><th>key</th> <th>value</th></tr> <t +r><td>camel</td> <td>flea</td></tr> <tr><td>frog</td> <td>green</td>< +/tr></table>
Navigating through pages using WWW::Mechanize
5 direct replies — Read more / Contribute
by cliff_t
on Sep 19, 2014 at 06:43
    Hello ! I am very new to scraping and right now I am writing my first script to get some data from a website. What I am trying to do is to navigate through all the pages, I think you know what I mean, those pages like Page1, Page2... My problem is that I don't know how to find what is the number of the last page in order to make all the parsing in a loop. Any help or ideas would be very appreciated ! I can tell you that I have counted 13 pages. The data is info about persons in a company. What happends is that every time I click the next page a post method is generated, and I noticed that only the post parameters change. So I was thinking to call the post method from a loop and pass all the post arguments, and change only the value for __EVENTARGUMENT which will get the number of the page. The url for all the pages remains the same
File date
4 direct replies — Read more / Contribute
by natol44
on Sep 19, 2014 at 03:19

    I need to select files created after a date. I do this check... and see that all files show the same date 31/11/69??

    Where am I wrong? :)


    #!/usr/bin/perl -w
    use File::stat;
    opendir(DIR, $dir) or die $!;
    while (my $file = readdir(DIR))
    next if ($file =~ m/^\./);
    my $mtime = (stat $file)[9];
    my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
    print "file = $file $mday/$mon/$year\n";

Help with regex
4 direct replies — Read more / Contribute
by reaper9187
on Sep 19, 2014 at 01:04
    Hello dear monks, I'm back here to seek your guidance and help with regexes. Basically, I'm parsing through an excel file that has whole database of entries as a table. Now, the table has headers corresponding to every column like so :
    "Header 1" "Header 2(%)" "header 3 " ......
    I need to read through the columns matching these keywords. The catch here is that keywords will be the same, but it may or may not include special characters, upper/lowercase characters,whitespaces in the header.

    So, if my keywords are "header1", "header2", and so on,....the regex should be able to identify all possible combinations of the keyword with variations as mentioned above.
I got HTTP POST error message: Internal Server Error
4 direct replies — Read more / Contribute
by iamAnne
on Sep 18, 2014 at 23:34

    Hi, I had a problem when I run my perl script it displays error in HTTP Post error code 500 and error message : Internal Server Error. Please advise how will I handle this. Thank you in advance.

    My script

    use warnings; use strict; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $url = "https://host/WebService/webservice.php"; # set custom HTTP request header fields my $req = HTTP::Request->new(POST => $url); $req->header('content-type' => 'application/json'); $req->header('x-auth-token' => 'kfksj48sdfj4jd9d'); # add POST data to HTTP request body my $post_data = '{"List": {"Data" : [ {"value" : "SRYSR656", "value2" +: "STYDTY ","date" : "140810","time" : "2132" },{ "value" : "001695 +568GP,"value2" : "EEFGE54","date" : "140812","time" : "1205"},{ "val +ue" : "YYHG","value2" :"JKTYRT6F","date" : "140811","time" : "1302" } +, {"value" : "ERETUT","value2" : "DYTUYU","date":"140812","time" : "1 +102" },{"value" : "SRT4543","value2" : "DASDRGF","date" : "140811","t +ime" : "2245"} ] }}'; $req->content($post_data); print $req->as_string; my $resp = $ua->request($req); if ($resp->is_success) { my $message = $resp->decoded_content; print "Received reply: $message\n"; } else { print "HTTP POST error code: ", $resp->code, "\n"; print "HTTP POST error message: ", $resp->message, "\n"; }

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 chanting in the Monastery: (11)
    As of 2014-09-19 16:26 GMT
    Find Nodes?
      Voting Booth?

      How do you remember the number of days in each month?

      Results (143 votes), past polls