Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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
Stuttering Children
1 direct reply — Read more / Contribute
by cmv
on Sep 19, 2014 at 14:21
    Hi Monks-

    Consider the attached script.

    I believe I should get 2 lines of output from each of the 2 children. Running this under perl, v5.8.8 built for darwin works fine. However, when I run it under perl, v5.8.8 built on solaris, strange things happen:

    TRY1: CHILD1: My pid = 21727 CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: My pid = 21728 CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING... TRY2: CHILD1: My pid = 22218 CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: My pid = 22219 CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING...
    First, why do I sometimes get 4 lines from one “bad” kid??

    Second, why does the “bad” kid sometimes change?

    Any pointers very much appreciated!

    Thanks

    -Craig

    #!/opt/exp/bin/perl use strict; use warnings; foreach my $i (1..2) { _spawn($i); } sub _spawn { my $id = shift || die "Missing id\n"; my $pid = fork(); defined $pid or die "bad open (pipe/fork): $!\n"; # Have parent/child run their respective code... if ( $pid ) { # PARENT CODE... return; } else { # CHILD CODE... print "CHILD$id: My pid = $$\n" ; while(<DATA>) { print "CHILD$id: $_" }; exit } } __DATA__ 1: THIS IS YOUR PARENT SPEAKING... 2: THIS IS YOUR PARENT SPEAKING...
File re-formatting
4 direct replies — Read more / Contribute
by lma3j
on Sep 19, 2014 at 14:11
    I am new to Perl and am trying to determine if Perl is the right tool for the job. We currently are processing flat files we receive from vendors. The flat files have a series of about 7 or 8 fixed length record types. We are changing our processes, which means our flat file layout is changing. We may be adding fields, deleting fields, or changing the length of fields in the various record types. For the time being, we will need to continue processing the old file formats along with the new file formats until all vendors switch to the new format. I was thinking of using Perl to read the file coming and determine if it is the old format or the new format. If it is the old format, we would reformat each record type, adding the new fields, deleting the obsolete fields, and changing field lengths where needed. Does this seem like something for which Perl would be good to use? Thanks, Larry
Capture error from one-liner
4 direct replies — 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 : HTML:: Mason Print a perl hash inside HTML tags in a tabular format
3 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 => 'abc@mydomain.com', To =>'def@mydomain.com', 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','xxxs.xxxx.xxxxx.com' ); $msg -> send;
perl POST question in HTML
1 direct reply — Read more / Contribute
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 ManagementEnhancement.pl, and the form receiving the data is called ManagementEnhancementUpdate.pl. ManagementEnhancement.pl has POST. ManagementEnhancementUpdate.pl is where I am having an issue. On ManagementEnhancement.pl there is a text box called "txtName" and a person will enter in a name. On ManagementEnhancementUpdate.pl, 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 ManagementEnhancementUpdate.pl page in order to accept the data? Here are my use statements at the top of ManagementEnhancementUpdate.pl

    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?
5 direct replies — 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: http://search.cpan.org/~szabgab/Parallel-ForkManager-1.06/lib/Parallel/ForkManager.pm

    use Parallel::ForkManager; ... @links=( ["http://www.foo.bar/rulez.data","rulez_data.txt"], ["http://new.host/more_data.doc","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
3 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 CGI.pm
2 direct replies — 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 CGI.pm
    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
    Greetings!

    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? :)

    Thanks!

    #!/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";
    }
    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!
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (4)
    As of 2014-09-22 01:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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











      Results (176 votes), past polls