Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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
Loading an array with file names
5 direct replies — Read more / Contribute
by insta.gator
on May 04, 2015 at 16:22

    PERL noob here.

    I need to load an array with file names produced from a wildcard search on a Windows box. I would like to pass the wildcard to the PERL script if possible. Not having luck. Here is what I have

    use Cwd; my $dir = getcwd; opendir DIR, $dir; my @prfiles = grep { $_ eq $ARGV[0] && $_ ne '.' && $_ ne '..' } readd +ir DIR; closedir DIR;

    I created using examples that I have found. I think that partially, my problem is with the wildcarding.

    This is what I would like to do: "perl loadfiles.pl testfile*.dat". Assuming I have testfile1,2,3,4.dat in the directory, it would load the array with these file names.

    Any help would be greatly appreciated!

Regular Expression Hiccup
5 direct replies — Read more / Contribute
by Mindsword
on May 04, 2015 at 15:27

    Hello, I have been working on a code for work and I found an odd glitch in my regular expression. I am looking through several blocks of texts for the Titles of some documents. The first title is "Screening Ligands by X-ray Crystallography." The text with that clearly has Screening Ligands by X-ray Crystallography in it. However, my regualr expression (below) does not pick it up. I have removed the 40 lines of code before this for brevities sake.

    #!/usr/bin/perl use warnings; use strict; use feature qw(say); ... ... if ($text =~ /Screening Ligands by X ray Crystallography/) { say "MATCH!"; } else { say "NOPE!"; } }
    However, if I do the code below, I do get a match.
    if ($text =~ /Screening Ligands by/) { say "MATCH!"; } else { say "NOPE!"; } }
    I have narrowed down the issue to the "-" I think. However, I can't search for the titles without the "-" as maybe 2/3rds of them have it. Any thoughts on how to get around this?

    EDIT: Based on what you all wrote, I began looking at a few of the others that were failing and have proven its not the "-" Its still not working correctly, but I at least know its not what I thought. I may even know what the issue is, but I'll need to run some tests. I think it may be an issue from the original titles not matching what's actually in the document.

Mechanize::Firefox can't get it to scroll
1 direct reply — Read more / Contribute
by WonderLemming
on May 04, 2015 at 13:46
    Hello, everyone! I'm having trouble getting WWW::Mechanize::Firefox to scroll down lines or pages in my current window.

    What I'm trying to do is navigate through some dialogues that generate a PDF in the window. Then I use xpath to pull the text from a few specific divs. The problem I'm running into is that it only seems to pre-load the first 2 pages of the PDF document and I need to scroll down to load the rest before xpath will snag all of the text I need.

    Here is my code:

    my $mech = WWW::Mechanize::Firefox->new( activate => 1, ); $mech->get('https://managemymarket.com/managers/acReporting/SalesForMa +rket.aspx?id=2'); $mech->select("#marketID", '1740'); $mech->field("#startDate", '4/1/2015'); $mech->field("#endDate", $date); $mech->click({ selector => '#btnRun' }); my ($window,$type) = $mech->eval('window'); $window->scrollByLines(188);
    No scrolling occurs however. Any help would be greatly appreciated!

    Cheers,

    Nony

    UPDATE: Looks like scrollByLines does not work in the built in PDF viewer. However, I was able to iterate through the pages numbers using $mech->field("#pageNumber", $i) to produce the effect that I needed.
Detecting memory leaks.
3 direct replies — Read more / Contribute
by Steve_BZ
on May 04, 2015 at 10:28

    Hi Monks,

    I have a fairly mature but functionally independent, piece of code that precedes a call to a video player in wxPerl.

    When this code is not called the video (gstreamer) never crashes. When the code is called the video crashes occasionally, maybe 10% of the time.

    This issue is of fairly long standing, but because it is sporadic, no-one is quite sure when it started.

    While watching this happen in a performance monitor, I noticed that the memory utilisation was not stable and that it gradually increased over time. I tried tracking this through Valgrind and Valgrind showed some kilobytes of memory leaks, but didn't specify when they occurred (ie in real time). Just at the end in the leak summary.

    I have been using Memory::Usage as shown in http://perlmaven.com/eliminate-circular-reference-memory-leak-using-weaken to try to find trhe leak, but without much success. Every time I think I have found it, I modify the code only to find the Memory 'diff' move down the script.

    Here is a sample of the output:

    time vsz ( diff) rss ( diff) shared ( diff) code ( diff) + data ( diff) 0 774028 ( 774028) 193524 ( 193524) 43648 ( 43648) 8 ( + 8) 368928 ( 368928) before i_Booking->new 0 774028 ( 0) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) after i_Booking tbl_patient_exam 0 774028 ( 0) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) before i_Booking SetScreenHeader 0 774028 ( 0) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) after i_Booking SetScreenHeader 0 774028 ( 0) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) after i_Booking set focus 0 774028 ( 0) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) before i_Booking i_staticText 1 0 774028 ( 0) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) after i_Booking i_staticText 1 0 774648 ( 620) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) before i_Booking Ctl_Booking_Referring_Doctor_Ref +_4_Lbl 0 774648 ( 0) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) before i_Booking i_staticText 2 0 774648 ( 0) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) after i_Booking i_staticText 2 0 774648 ( 0) 193800 ( 276) 43924 ( 276) 8 ( +0) 368928 ( 0) before i_Booking health plan providor 0 774648 ( 0) 193800 ( 0) 43924 ( 0) 8 ( +0) 368928 ( 0) at i_Booking mid point 0 774648 ( 0) 193804 ( 4) 43924 ( 0) 8 ( +0) 368928 ( 0) at i_Booking DatePickerCtrl 0 774780 ( 132) 193932 ( 128) 43924 ( 0) 8 ( +0) 369060 ( 132) at i_Booking Examining Doctor Search 0 774912 ( 132) 194172 ( 240) 43924 ( 0) 8 ( +0) 369192 ( 132) at i_Booking Patient details 0 774912 ( 0) 194172 ( 0) 43924 ( 0) 8 ( +0) 369192 ( 0) before i_Booking $loc_window_layout->SetFooter 0 774912 ( 0) 194172 ( 0) 43924 ( 0) 8 ( +0) 369192 ( 0) after i_Booking $loc_window_layout->SetFooter 0 774912 ( 0) 194172 ( 0) 43924 ( 0) 8 ( +0) 369192 ( 0) at i_Booking exit

    I am suspicious of a number of pieces of code, but like bubbles in the wall-paper the bubble just moves when I press it. For instance in the above output, we see :

    0 774648 ( 620) 193524 ( 0) 43648 ( 0) 8 ( +0) 368928 ( 0) before i_Booking Ctl_Booking_Referring_Doctor_Ref
    The total code between this line and the previous output is shown here, together with the surrounding lines:
    # Referring Dr details from Dr file $booking_dlg->{Ctl_Booking_Referring_Doctor_Lbl} = Wx::StaticText- +>new($booking_dlg->{booking_main}, wxID_ANY, __t('Referring Doctor'), + wxDefaultPosition, wxDefaultSize, ); $mu->record('before i_Booking i_staticText 1'); $booking_dlg->{Ctl_Booking_Referring_Doctor_Txt} = i_staticText->n +ew($booking_dlg->{booking_main}, wxID_ANY, "", wxDefaultPosition, wxD +efaultSize, ); $mu->record('after i_Booking i_staticText 1'); $booking_dlg->{Ctl_Booking_Referring_Doctor_Txt}->SetFont(Wx::Font +->new($gl_cfg->{client_point_size}, wxMODERN, wxFONTSTYLE_ITALIC, wxF +ONTWEIGHT_BOLD, 0, $gl_cfg->{client_font})); $mu->record('before i_Booking Ctl_Booking_Referring_Doctor_Ref_4_L +bl'); $booking_dlg->{Ctl_Booking_Referring_Doctor_Ref_4_Lbl} = Wx::Stati +cText->new($booking_dlg->{booking_main}, wxID_ANY, __t("Ref 4"), wxDe +faultPosition, wxDefaultSize, ); $mu->record('before i_Booking i_staticText 2'); $booking_dlg->{Ctl_Booking_Referring_Doctor_Ref_4_Txt} = i_staticT +ext->new($booking_dlg->{booking_main}, wxID_ANY, "", wxDefaultPositio +n, __xy_size(100), ); $mu->record('after i_Booking i_staticText 2'); $booking_dlg->{Ctl_Booking_Referring_Doctor_Ref_4_Txt}->SetFont(Wx +::Font->new($gl_cfg->{client_point_size}, wxMODERN, wxFONTSTYLE_ITALI +C, wxFONTWEIGHT_BOLD, 0, $gl_cfg->{client_font}));

    But really the code here looks quite innocent.

    Any ideas would be welcome.

    Regards

    Steve.

Reading a huge input line in parts
8 direct replies — Read more / Contribute
by kroach
on May 04, 2015 at 09:20

    I need to read a line of numbers separated by spaces and ending with a 0, like this:

    1 2 3 4 7 20 12334 0

    Each number needs to only be processed individually, so I don't have to keep them all in memory. The problem is, input lines can be very long and reading them with <> and splitting consumes a lot of memory.

    I tried setting a space as the input record separator, but it doesn't get the last number correctly.

    use strict; use warnings; sub do_something { print '{', $_[0], "}\n" } local $/ = ' '; while (<>) { do_something($_); }

    I also tried to simulate C++ cin behaviour with the following function:

    sub cin_read { my $inchar = getc; $inchar = getc while $inchar =~ /^\s$/; my $result = ''; while ($inchar =~ /^\S$/) { $result .= $inchar; $inchar = getc; } return $result; }

    However, it's overly complicated and slow.

    How else could I go about doing this?

    EDIT: Updated sample input to include multi-digit numbers
Comparison between a string and an array
4 direct replies — Read more / Contribute
by Eth443
on May 04, 2015 at 06:32

    Hi, I'm learning Perl, and I've got a question, how could I compare a STDIN from the user and a string inside an array like I tried to do. I did this but doesn't work -->

    my @answer=( "Bad", "Good", "Not good enough" ); chomp(my $user_answer=<STDIN>); if ($user_answer eq @answer){ print "Ok\n"; }

    I know that I can print a value inside the array with [] , but I don't know if there's any way to try them all...

CSV files compare
5 direct replies — Read more / Contribute
by PERL_fresher
on May 03, 2015 at 23:54
    Hello Few Perl monks, I have very urgent requirement to perform below changes. I have one master file a csv file delimited by commas and a directory which has several files 8 to be precise. Master file has fields such as employee ID and name, surname of employee and other fields as well. Similarly other csv files in directory have employee ID's and name and surnames in it. what I need to do is using master file as source search employee ID's in all files in that directory and replace name and surname in the directory. for ex. Master file employee ID, name, surname, other fields 1,nitin, abc,... 2, aa, bb,.. 3,cc,dd,.. 4, ee, ff directory files employee ID, name, surname, other fields 1,nitin1, abc1,... 2, aa1, bb1,.. 3,cc1,dd1,.. 4, ee11, ff1 I need to replace this with the data in master file for all files in directory. positions of the fields in target directory are same as I have described. I am fairly new to perl and just making my way through it. any help is greatly appreciated. sample master file Employee Number~Category Number~Forename~Surname~Initials~NI Number~Title~Gender~Marital Status~Date of Birth~Home Address Line 1~Home Address Line 2~Home Address Line 3~Home Town/City~Home County~Home Post Code~Employee Contribution %~Employer Contribution %~Annual Salary~Date of Joining Scheme~Date of Joining Company~Exchanged %~Scheme Joiner D51231~ORDM~Charles~Smith~C T~NP307342C~Mr~Male~S~15/10/1971~45 Board Walk~Springer Lane~Mundesley~Norfolk~~NR28 3JK~0.00~8.00~72800.00~01/09/2014~04/08/2014~2.00~N F78321~SMG1~Janet~Patterson~J M~AA112233A~Ms~Female~~21/07/1975~12 Sheringham Way~Cromer~~~~NR21 9BG~0.00~31.00~320000.00~01/09/2014~25/08/2014~0.00~N Sample files which is to be replaced by master file Employee Number~Category Number~Forename~Surname~Initials~NI Number~Title~Gender~Marital Status~Date of Birth~Home Address Line 1~Home Address Line 2~Home Address Line 3~Home Town/City~Home County~Home Post Code~Employee Contribution %~Employer Contribution %~Annual Salary~Date of Joining Scheme~Date of Joining Company~Exchanged %~Scheme Joiner D51231~ORDM~Nitin~Rane~C T~NP307342C~Mr~Male~S~15/10/1971~45 Board Walk~Springer Lane~Mundesley~Norfolk~~NR28 3JK~0.00~8.00~72800.00~01/09/2014~04/08/2014~2.00~N F78321~SMG1~AAA~BBB~J M~AA112233A~Ms~Female~~21/07/1975~12 Sheringham Way~Cromer~~~~NR21 9BG~0.00~31.00~320000.00~01/09/2014~25/08/2014~0.00~N Here D51231 F78321 are employee nos and in master file D51231 Forename = CHARLES surname = SMITH I need to do this to sample file where D51231 Forename = Nitin surname = Rane Output should be D51231 Forename = CHARLES surname = SMITH. what I was hoping with below code is when I give directory name all files in that directory are accessible to me as tables so that I can simply use simple SQL to update source file.
    my $dbh = DBI->connect('dbi:CSV:', "", "", { f_dir => "$_ENV::LogsFilesDIR/", f_ext => ".txt", eol => "\n", sep_char => "~", RaiseError => 1, }) or die $DBI::errstr; print $ceridian_raw_files; if ($ceridian_raw_files =~ m/^$_ENV::new_entrants/i) { print "inside dbi $ceridian_raw_files\n"; my $sth = $dbh->prepare("select forename from $ceridian_raw_files"); $sth->execute(); my $res = $sth->fetchall_arrayref(); use Data::Dumper; print $res;
    However in my case Im not able to even initiate the object $sth. any suggestions? I tried this method with some comments from below members. apologies for any ignorance on my part as name suggests Im newbie in Perl world and relying on help from fellow perl monks :) Hello Monks Issue is now resolved. thanks for your help if anyone is intrested then below is the code
    my $dbh = DBI->connect("dbi:CSV:f_dir=$_ENV::LogsFilesDIR;csv_sep_cha +r=\\~") or die $DBI::errstr; $dbh->{'csv_tables'}->{'prospects'} = { 'file' => 'cimp_new_entrant.tx +t'}; $dbh->do("UPDATE prospects SET Forename = 'Nitin'"); my $sth = $dbh->prepare("select Forename from prospects"); $sth->execute(); while (@row = $sth->fetchrow_array) { print @row; } $sth->finish( ); $dbh->disconnect( );
Form authentication for .NET sites
1 direct reply — Read more / Contribute
by Charli
on May 03, 2015 at 22:49
    Hi, I am new in Perl and I am trying to go through a login page and going to the actual site. For example, to be able to check emails in Yahoo, one must go through the Yahoo log in to enter credentials for username and password. I used WWW::Mechanize to go through the login page, see code snippet below:
    my $mech = WWW::Mechanize->new(); $mech->get($url); $mech->field("UserName", $username); $mech->field("Password", $password); $mech->submit_form();
    But with the code above, I am unable to go through the actual page and stuck in the login page. Any thoughts or suggestions?
[emacs] auto-completion of Perl code
1 direct reply — Read more / Contribute
by LanX
on May 03, 2015 at 17:37
    Hi

    I'm playing around with auto-complete.el combined pos-tip.el but it seems Perl isn't properly supported yet.

    see this picture PosTipScreenshotAutoComplete for motivation.

    I tried making perl-completion.el work but it had too many prerequisites und didn't seem well supported.

    Any other way?

    I can see that ECB is able to parse Perl using imenu and/or etags, would be nice to feed this into auto-complete. :)

    (update: I know that only perl can parse perl for 100%, but 90% is better than nothing)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Compare variables when the number of them vary
2 direct replies — Read more / Contribute
by stroke
on May 03, 2015 at 12:52

    Hi, this script queries the defined list of DNS servers and grabs the serial numbers for the listed zones. It then compares them against each other and logs if they match or not. It may not be pretty, but it works !

    But, the bit I'm struggling with now is how best to do the serial number comparison if the number of DNS server in the list changes - that is if I add in more or remove DNS server IPs to the list, I don't have to manually go and add/remove the serial number references for the check - I want to make the serial compare "dynamic", based on the number of DNS servers.

    I'm okay with just comparing all other serials to the first serial number, rather than comparing all possible combinations.

    fyi - I'm writing all the results out to a file for reference, in addition to sending errors only to STDOUT. Thanks

    #!/usr/bin/env perl use strict; use warnings; use Net::DNS; use 5.010; my @nameservers = qw(8.8.8.8 4.2.2.2); my @errors; open(my $resultsfh, '>', "results-serials.csv") or die "cannot open > results-serials.csv: $!"; print $resultsfh "Result,Zone"; foreach (@nameservers) { print $resultsfh ",Serial for: $_"; } print $resultsfh "\n"; while (<DATA>) { my $fqdn = $_; chomp $fqdn; my @serials; my $res = Net::DNS::Resolver->new; $res->tcp_timeout(2); $res->udp_timeout(2); foreach my $ns (@nameservers) { $res->nameservers($ns); my $reply = $res->query("$fqdn", "SOA"); if ($reply) { push(@serials, ($reply->answer)[0]->serial); } else { push(@serials, $res->errorstring); } } # Compare Serials no warnings 'numeric'; if ($serials[0] == $serials[1]) { say $resultsfh "MATCH,$fqdn,$serials[0],$serials[1]"; } else { say $resultsfh "MISMATCH,$fqdn,$serials[0],$serials[1]"; push @errors, "MISMATCH.$fqdn,$serials[0],$serials[1]"; } } if (@errors) { say "ERRORS:"; foreach (@errors) { say "$_"; } } __DATA__ google.com apple.com

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 examining the Monastery: (11)
    As of 2015-05-05 00:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      In my home, the TV remote control is ...









      Results (110 votes), past polls