Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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
how can i be in the same entry if my validation fails in Tk?
No replies — Read more | Post response
by yedukondalu
on Nov 25, 2015 at 00:21

    Given above is my code.In that I am validating the name entry not to contain any numeric values.If it does the messageBox displays and goes to the next entry..I need the cursor to be in the same entry if the validation fails. And the other is when I give any character in age enter both messageBoxes i.e dob and ages displays are popping up.

    #!usr/bin/perl use warnings; use strict; use Tk; #$\="\n"; my $name; my $age; my $dob; open(my $fh,'+<','details.txt') || die "Can't open the file details.tx +t $!"; #creating a Mainwindow: my $window = MainWindow->new(); my $first_frame = $window -> Frame()->pack(-side => 'top',-ipadx => 10 +,-fill => 'x', -ipady => 1); $first_frame -> Label(-text => 'Fill the details listed below') -> gr +id(-row => 0 ,-column => 2,-rowspan => 1); my $frame = $window -> Frame(-background => 'grey')->pack(-side => 'to +p',-ipadx =>150,-fill => 'x',-ipady => 60); my $name_label=$frame -> Label(-text => 'Name') -> grid(-row => 2, -co +lumn=> 0); my $name_entry=$frame->Entry(-background => 'white',-foreground => 'bl +ack', -textvariable => \$name, -validate => 'focusout',-validatecomma +nd => \&check_name)->grid(-row =>2, -column=>1); my $age_label=$frame -> Label(-text => 'age')-> grid(-row => 4, -colum +n=> 0); my $age_entry = $frame -> Entry(-background => 'white',-foreground => +'black', -textvariable => \$age,-validate => 'focusout',-validatecomm +and => \&check_age) -> grid(-row =>4, -column=>1); my $dob_label=$frame -> Label(-text => 'DOB')-> grid(-row => 6, -colum +n=> 0); my $dob_entry = $frame -> Entry(-background => 'white',-foreground => +'black',-textvariable => \$dob, -validate => 'focusout',-validatecomm +and => \&check_dob) -> grid(-row =>6, -column=>1); $name=$name_entry -> get(); $age = $age_entry -> get(); $dob = $dob_entry -> get(); my $submit = $frame -> Button(-text => 'submit', -command =>sub {&subm +it} ) -> grid( -row =>8, -column=>1); $frame -> Label(-background => 'grey') -> grid(-rowspan=> 6); $frame -> Label(-text => 'After adding all the details click quit to e +xit') -> grid( -row => 16, -column=> 1); $frame -> Label(-background => 'grey') -> grid(-rowspan=> 6); my $quit = $frame -> Button(-text => 'Quit', -command =>sub {exit} ) - +> grid(-row =>25, -column=>1); sub submit { my $line= join(' ',$name,$age,$dob); print $fh $line,"\n"; if(!($?)) { my $button = $frame -> messageBox( -icon => 'info',-message => ' +Details added successfully to file', -type => 'Ok' ); $name_entry->delete('0', 'end'); $age_entry->delete('0', 'end'); $dob_entry->delete('0', 'end'); } } sub check_name { if (($name =~ m/[0-9]/)) { $name_entry -> messageBox( -icon => 'info',-message => 'Name shoul +d not contain numeric values' ,-type => 'Ok'); $name_entry->delete('0', 'end'); } } sub check_age { if (($age=~ m/[a-z]/i)) { $age_entry -> messageBox( -icon => 'error',-message => 'age shoul +d not contain characters' ,-type => 'Ok'); $age_entry->delete('0','end'); } } sub check_dob { if (!($dob=~ m/\d{1,2}[\/|\:]\d{1,2}[\/|\:]\d{4}/)) { $age_entry -> messageBox( -icon => 'error',-message => 'Enter vali +d date' ,-type => 'Ok'); $dob_entry->delete('0', 'end'); } } MainLoop;
To process xml files within multiple subfolders in perl
2 direct replies — Read more / Contribute
by codewalker
on Nov 24, 2015 at 23:04

    Hi, I Just want process all xml files within multiple subfolders

    But my current code is used to grep the files within the folder

    use strict; undef $/; my $path = $ARGV[0]; my $out = $ARGV[1]; opendir(DIR, $path) || die "cannot read folder path"; my @xmlfiles = grep {/\.xml$/i} readdir(DIR); closedir(DIR); foreach my $xml(@xmlfiles) { open(FIN, "$path/$xml") or die("Couldn't open input File!!"); my $file = <FIN>;

    Can anyone knows how to process within multiple folders in perl

Array from list in input file?
2 direct replies — Read more / Contribute
by SandraA
on Nov 24, 2015 at 16:13
    I have a file containing three columns of strings (the last one is a numeric value) I want a code that prints the string in the first column for every line where the third value is above a certain threshold. My first idea was to open the file and treat the values as an Array with Three scalars, split the scalars and ask perl to print the first one if the third one was larger than my threshold. I don't know if that is a good way to do it, and anyway I have no idea how to write it. Could anyone help me solve my problem?
efficient IPv6 subnetting
2 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 24, 2015 at 16:06

    Hello all, I'm trying to calculate and print out all subnets of prefix length x from a subnet with a prefix length y \. For example: I want to split a /40, say 2001:0DB8::/40 into /56 subnets. I thought about using NetAddr::IP , for example:

    use NetAddr::IP; my $sub6 = new NetAddr::IP '2001:db8::/40'; my $i=1; while ($sub6 < $sub6->broadcast) { # should've been <= but it does no +t seem to work if ($i==4722366482869645213696) { # that many addresses in a / +56 print "ip = $sub6\n"; $i=1; } $sub6 ++; $i++; } print "ip = $sub6\n"; # need to print last address

    the above probably works but seems so inefficient and terribly slow. Any ideas would be greatly welcomed!

Doubly link list implementation
7 direct replies — Read more / Contribute
by punitpawar
on Nov 24, 2015 at 14:24
    Hello , I am struggling to write code to implement a doubly link list. Could any of you please help me with it. This is what I have written so far. If you could point me to any material which shows how a doubly linked list is implemented in PErl that will be of great help to me...
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $head= undef; ## reference to first node my $tail=\$head; ## reference to the "next" field of the last node my $list; open FILE, "<datastored.txt" or die $!; # Creating a doubly linked list while (<FILE>){ my $node = { "data" => $_ , "next" => undef, "prev" => undef, }; $$tail=$node; $node->{"prev"}= $tail; $tail = \$node->{"next"}; } print Dumper $head; &print_list($head); sub print_list { $list=$_[0]; while ($list) { print Dumper $list; print "$list->{data}"; $list = $list->{next}; } }
Split with numbers
3 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 24, 2015 at 13:23
    Hello, I have a set of strings that may be made of up to three parts: "LETTERS_BEFORE"+"NUMBER"+"LETTERS_AFTER".
    For example AB23C ABC23 23BC ABC.
    Is there a way to split them into an arrays of 3 elements?
    The operation on previous strings should produce ('AB','23','C'), ('ABC','23',''), ('','23','BC'), ('ABC','','').

    Thank you in advance for any hint.
PTKDB - exception catched Not a SCALAR reference
1 direct reply — Read more / Contribute
by pjkang7
on Nov 24, 2015 at 12:13

    Hi all, So, I'm pretty sure this is an issue (or just process of) with perl ptkdb debugger tool that I use, I wanted to see if anyone has experienced similar issue. The error message I get is like this: ptkdb - >>> exception catched Not a SCALAR reference at (eval 49) line 4.

    and I believe this occurs from the following line of code

    my %hash = (); my $h_ref = \%hash; if(exists $$h_ref{'try'}) {

    I'm pretty sure my dereferencing of the hash reference is correct, and I think it's simply something catched by the ptkdb tool (happens when I do step by step using the tool). The script actually executes correctly without an error if run normally. I just wanted to see if perlmonks knew of this issue (if anyone uses ptkdb) and that I can safely simply ignore this. Thank you!! Happy Thanksgiving!!

CGI of url-encoded query string
2 direct replies — Read more / Contribute
by glendeni
on Nov 24, 2015 at 00:00

    On a website I run I've recently noted in the apache error logs a number of url-encoded requests ala

    GET /cgi-bin/script.cgi?c%3D1%3Bu%3D1%3B...
    which causes the script to fail, whereas using the comparable query string
    GET /cgi-bin/script.cgi?c=1;u=1;...
    is successful. My website uses perl v5.8.0 and apache 2.0.46, but I tested this on my less dated home server using perl 5.18.2 and apache 2.4.7 with the same result.

    The perl script uses

    use CGI ; my $query = new CGI; $cparam = $query->param('c'); $uparam = $query->param('u');
    Should this be happening? I had assumed the CGI module would handle url-encoded input. Is there a way to treat this?

    I should add that the url-encoded cases are very rare and may come from some kind of bot instead of a human user.

    LATER - decided that the submitted query string is invalid and should not be expected to be processed by CGI since query string delimiters have been encoded, making parsing of parameter name and value impossible. Will now try to delete this post, but leaving this in case I can't figure that out

How to make parent wait for all the child processes.
3 direct replies — Read more / Contribute
by gjoshi
on Nov 23, 2015 at 23:11
    Hello All,

    I Am forking child processes. I am waiting for them to finish in parent. If you see my code What I want to parent is not to move forward until it get info from each child. Next portion of the code is dependent on the status of the child. I want to do kind of wait in a loop in parent and keep checking for the child's response. Here is my problem I am not able to do it last logic. Please help me with suggestions.

    #!/usr/local/bin/perl -w use strict; use warnings; use Net::Telnet; use IO::Socket::INET; my $IsParent = 1; my $ListenPort = 5000; my $PPID = $$; print "I am the only process.\n"; my @children_pids; for my $count (1..3){ my $child_pid = fork(); if ($child_pid) { # If I have a child PID, then I must be the + parent push @children_pids, $child_pid; } else { # I am the child # close the sides of the pipes not used by the child my $end; local $SIG{HUP} = sub { $end = 1 }; my $Child_PID = $$; print "\n============================== \n"; print "CHILD's PID is: $Child_PID \n"; my $db1 = ConnectToServer('',$ListenPort,60); local $SIG{HUP} = sub { $end = 1 }; #Do something and then write to DB as done WriteValueToDB('done',$db1,$Child_PID . '_Status'); until ($end) { #print "Sleep 1\n"; sleep 1; } print "============================== \n"; function2(); } } foreach my $child (@children_pids) { print "Parent: Waiting on $child\n"; sleep 5; waitpid($child, 0); } # HERE I WANT TO GET THE STATUS OF ALL THE KIDS BECAUSE FROM HERE IT D +EPENDS ON EACH RESULT FURTHER PROCESSING. WAIT IN A LOOP OR SOMTETHIN +G # NOT GETTING IT HOW TO DO IT. I DON'T WANT TO CONTINUE UNLESS I GET A +LL KIDS STATUS. DON'T WANT TO GO TO END DB MAIN UNLESS I GET ALL CHIL +D PROCESSES STATUS. # my $localtime = localtime; # my $child_stat = ReadValue($child . '_Status',$dbmain); # accoding to the result in DB decide to kill it #print "$child CHILD STAT: $child_stat \n"; #kill HUP => $child; print "END DB MAIN: $dbmain \n"; KillServer($dbmain); print "All done.\n";
Function Prototypes
6 direct replies — Read more / Contribute
by KimberTLE
on Nov 23, 2015 at 20:18
    Having difficulty coming-up with what the prototype for my sub might look like. If coded as is (below), the sub works; the caller sends named parameters, the sub checks for useful data, does the caller's bidding. A sample of things I've tried for prototypes are:
    sub ConfigRead($ @ %); sub ConfigRead(;$ @ %); sub ConfigRead($ \@ \%); sub ConfigRead(;$ \@ \%);
    My actual code without the prototype resembles this:
    #!/usr/bin/perl -w use strict; my @aFieldHeaders; my %hFieldConfigs; my $sConfigFile = 'mydata.csv'; ConfigRead(sFILE => $sConfigFile, apHEAD => \@aFieldHeaders, hpFIELDCONF => \%hFieldConfigs); # my array and hash now have some cool stuff in 'em! sub ConfigRead { my %hArgs = ( sFILE => 'BAD', # lazy user alert! whine & die apHEAD => 'BAD', # ^^^ see comment above hpFIELDCONF => 'BAD', # by now you know the drill! @_, # user demands come from here ); # stick some cool stuff in their variables }
    Suggestions, solutions and/or winning lottery ticket numbers for Wednesday's drawing gratefully accepted!

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!
  • 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.
  • 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 perusing the Monastery: (8)
    As of 2015-11-25 08:25 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (672 votes), past polls