[30661] in Perl-Users-Digest
Perl-Users Digest, Issue: 1906 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 7 18:09:44 2008
Date: Tue, 7 Oct 2008 15:09:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 7 Oct 2008 Volume: 11 Number: 1906
Today's topics:
Re: <Help> How to use routines from another perl script <tim@burlyhost.com>
Re: Apache2::ASP installation <jdrago.999@gmail.com>
Re: Data File <ben@morrow.me.uk>
efficient way to write multiple loops code <hirenshah.05@gmail.com>
Re: efficient way to write multiple loops code xhoster@gmail.com
Re: efficient way to write multiple loops code <tim@burlyhost.com>
Re: efficient way to write multiple loops code <hirenshah.05@gmail.com>
Re: efficient way to write multiple loops code <tim@burlyhost.com>
Re: efficient way to write multiple loops code <g_r_a_n_t_@dodo.com.au>
Re: How it works?(about while loop and regex as conditi sln@netherlands.com
Re: How it works?(about while loop and regex as conditi <tim@burlyhost.com>
Re: perl cgi script - reload <tim@burlyhost.com>
Re: sysread <hansmu@xs4all.nl>
Re: update XML file with perl or other...? sln@netherlands.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 07 Oct 2008 13:16:48 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: <Help> How to use routines from another perl script
Message-Id: <3jJGk.41327$Nu3.34362@newsfe14.iad>
Petr Vileta (fidokomik) wrote:
> Tim Greer wrote:
>> Petr Vileta (fidokomik) wrote:
>>
>>> --- example (/var/myroutines/ex1.pl) ---
>>> 1;
>>
>> ^ Down there, maybe --> ?
>>
>
> Yes, of course ;-) But for some reason (I forgot why) I tend to write
> it to top of file.
>
>>> sub myfunc1 {
>>> my ($param1,$param2) = @_;
>>> my $to_return='';
>>> # some code here
>>> return $to_return;
>>> }
>>
>> 1;
>>
>>> --- example ---
>>>
>
>> use warnings;
>>
>
> use strict;
> no warnings;
>
> :-)
>
>>> require "/var/myroutines/ex1.pl;
>>
>>>> die, or die, or use eval.
>
> Of course, "die" should be there, but this is a fast-written example
> only.
>
All humor aside, it's a good idea to give examples that are correct and,
if you can, add some common error checks. I wrote a pretty lengthly
blog article about this and why it's a good idea (it basically prevents
follow up questions when your example doesn't work for the OP).
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
------------------------------
Date: Tue, 7 Oct 2008 12:12:24 -0700 (PDT)
From: JohnDrago <jdrago.999@gmail.com>
Subject: Re: Apache2::ASP installation
Message-Id: <b3bb45ec-1d7e-40b5-8525-02ce932a4119@a19g2000pra.googlegroups.com>
On Sep 22, 7:50=A0pm, DJ <d...@fluidnetwork.com> wrote:
> Hello all,
>
> Just a quick question: am running into difficulty getting module
> Apache2::ASP working, but I cannot seem to find a support repository
> outside of googling for individual pages or installation hints other
> thanhttp://www.devstack.com. =A0Would someone please point me in the
> right direction?
Currently you can use the CPAN forum for discussion and RT to submit
bug reports:
CPAN Forum: http://www.cpanforum.com/dist/Apache2-ASP
RT: http://rt.cpan.org/Public/Dist/Display.html?Name=3DApache2-ASP
There is no public mailing list at this time.
> Just in case this is common and I have missed it, the actual error
> that I get out of my Apache logs is:
> Use of uninitialized value $fixed in substitution (s///) at /usr/lib/
> perl5/site_perl/5.10.0/Apache2/ASP/Config.pm line 224.
This is not an error - it is a warning. Make sure that $fixed has a
value before doing a s/// substitution on it.
Otherwise, do "no warnings 'uninitialized';" within the contextual
scope.
> [Mon Sep 22 20:52:47 2008] [error] [client 192.168.1.10] Can't call
> method "lib" on unblessed reference at /usr/lib/perl5/site_perl/5.10.0/
> Apache2/ASP/Config.pm line 63.\n
Make sure that $ENV{APACHE2_ASP_APPLICATION_ROOT} is set correctly
using a full path - i.e. "/usr/local/app-name/site-name" or "/var/www/
site-name"
>
> Something that I tried hours ago made me think that the configuration
> file was not being read, but I can no longer reproduce or even
> remember what exactly I was doing at the time...
>
Best regards,
John Drago
------------------------------
Date: Tue, 7 Oct 2008 22:21:38 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Data File
Message-Id: <2g0tr5-tkl.ln1@osiris.mauzo.dyndns.org>
Quoth "friend.05@gmail.com" <hirenshah.05@gmail.com>:
> On Oct 6, 6:01 pm, Jürgen Exner <jurge...@hotmail.com> wrote:
> > "friend...@gmail.com" <hirenshah...@gmail.com> wrote:
> > >On Oct 6, 12:52 pm, Jürgen Exner <jurge...@hotmail.com> wrote:
> > >> [Forwarded from CLP.Modules]
> >
> > >> "friend...@gmail.com" <hirenshah...@gmail.com> wrote:
> > >> >I have a large file in following format:
> >
> > >> >ID | Time | IP | Code
<snip>
>
> How can read file in Hash Array.
>
> open(INFO, $file);
*Always* check the return value of open.
Use lexical filehandles.
Use 3-arg open.
open(my $INFO, "<", $file) or die "can't read '$file': $!";
> @lines = <INFO>;
Do you have 'use strict' at the top of your program?
> # Instead of reading it in array. I want to put it in Hash Array(%).
> And may just ID and IP fields.
my %lines;
@lines{qw/ID Time IP Code/} = <$INFO>;
If you want just some fields, it is a little messier:
my %lines;
($lines{ID}, undef, $lines{IP}, undef) = <$INFO>;
Ben
--
And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
* ben@morrow.me.uk *
------------------------------
Date: Tue, 7 Oct 2008 13:31:50 -0700 (PDT)
From: "friend.05@gmail.com" <hirenshah.05@gmail.com>
Subject: efficient way to write multiple loops code
Message-Id: <4d1cfa08-ae0a-40e0-8073-258ef6484d50@a29g2000pra.googlegroups.com>
Hi,
I am trying to analyze some data. I have big data files.
I have 3 different files in following format. ($file_1, $file_2,
$file_3)
ID | Time | IP | Code
Following is psuedo code which I am writing. I want to know another
efficient way to do same thing.
open(INFO_1,$file_1);
open(INFO_2,$file_2);
open(INFO_3,$file_3);
@file1_lines = <INFO_1>;
@file2_lines = <INFO_2>;
@file3_lines = <INFO_3>;
foreach $file1_line (@file1_lines)
{
@file1 = split('\|',$file1_line);
#some code
foreach $file2_line (@file2_lines)
{
@file2 = split('\|',$file2_line);
#some code
#if condition between File1 data and File2 data
{
#some code
foreach $file3_line (@file3_lines)
{
@file3 = split('\|',$file3_line);
#some code
#if condition
}
}
}
}
So I am going thorugh each data of file 1 and depending on if data is
present in file2 and again depending on some if condition I look for
that data in file3.
So each data of file1 will have to go through each data of file2 and
each data of file2 will have to go thorugh file3.
So this code is taking lot of time. I want some suggestion for
efficient code.
Can I use Hash Array (by reading file in hash array)
Thanks
------------------------------
Date: 07 Oct 2008 20:44:49 GMT
From: xhoster@gmail.com
Subject: Re: efficient way to write multiple loops code
Message-Id: <20081007164452.068$nz@newsreader.com>
"friend.05@gmail.com" <hirenshah.05@gmail.com> wrote:
> foreach $file1_line (@file1_lines)
> {
> @file1 = split('\|',$file1_line);
> foreach $file2_line (@file2_lines)
> {
> @file2 = split('\|',$file2_line);
> #if condition between File1 data and File2 data
...
>
> So this code is taking lot of time. I want some suggestion for
> efficient code.
>
> Can I use Hash Array (by reading file in hash array)
Whether you can use a hash to speed this up depends on whether
"If condition between File1 data and File2 data" can be reduced
to (or protected by) fast hash look ups. We can't answer this for you
without knowing what the nature of that condition is.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Tue, 07 Oct 2008 13:48:18 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: efficient way to write multiple loops code
Message-Id: <nWPGk.3647$qu.388@newsfe04.iad>
friend.05@gmail.com wrote:
>
> I have 3 different files in following format. ($file_1, $file_2,
> $file_3)
>
> ID | Time | IP | Code
>
> Following is psuedo code which I am writing. I want to know another
> efficient way to do same thing.
>
> open(INFO_1,$file_1);
> open(INFO_2,$file_2);
> open(INFO_3,$file_3);
>
> @file1_lines = <INFO_1>;
> @file2_lines = <INFO_2>;
> @file3_lines = <INFO_3>;
>
>
> So I am going thorugh each data of file 1 and depending on if data is
> present in file2 and again depending on some if condition I look for
> that data in file3.
>
>
> So each data of file1 will have to go through each data of file2 and
> each data of file2 will have to go thorugh file3.
>
> So this code is taking lot of time. I want some suggestion for
> efficient code.
>
> Can I use Hash Array (by reading file in hash array)
>
>
The answer very much depends on what #some code is actually doing. Is
the data fixed in the files, what specific checks are you doing? Could
the data be anywhere in a file, inside of a line of data, or are you
trying to match lines from ^ start to $ end of line per file, or are
you doing some other type of processing?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
------------------------------
Date: Tue, 7 Oct 2008 14:17:42 -0700 (PDT)
From: "friend.05@gmail.com" <hirenshah.05@gmail.com>
Subject: Re: efficient way to write multiple loops code
Message-Id: <157b1617-f8ee-4562-9584-d543157c984b@r37g2000prr.googlegroups.com>
On Oct 7, 4:48=A0pm, Tim Greer <t...@burlyhost.com> wrote:
> friend...@gmail.com wrote:
>
> > I have 3 different files in following format. ($file_1, $file_2,
> > $file_3)
>
> > ID | Time | IP | Code
>
> > Following is psuedo code which I am writing. I want to know another
> > efficient way to do same thing.
>
> > open(INFO_1,$file_1);
> > open(INFO_2,$file_2);
> > open(INFO_3,$file_3);
>
> > @file1_lines =3D <INFO_1>;
> > @file2_lines =3D <INFO_2>;
> > @file3_lines =3D <INFO_3>;
>
> > So I am going thorugh each data of file 1 and depending on if data is
> > present in file2 and again depending on some if condition I look for
> > that data in file3.
>
> > So each data of file1 will have to go through each data of file2 and
> > each data of file2 will have to go thorugh file3.
>
> > So this code is taking lot of time. I want some suggestion for
> > efficient code.
>
> > Can I use Hash Array (by reading file in =A0hash array)
>
> The answer very much depends on what #some code is actually doing. =A0Is
> the data fixed in the files, what specific checks are you doing? =A0Could
> the data be anywhere in a file, inside of a line of data, or are you
> trying to match lines from ^ start to $ end of line per file, or are
> you doing some other type of processing?
> --
> Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
> Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
> and Custom Hosting. =A024/7 support, 30 day guarantee, secure servers.
> Industry's most experienced staff! -- Web Hosting With Muscle!- Hide quot=
ed text -
>
> - Show quoted text -
I am checking data from a line not whole line.
I want to check if IP and Code of file1 is present in file2 and if it
is present in file2 then check again if it is there in file3.
I am doing all this processing analyze some data.
Let me know if still it is not clear.
Thanks.
------------------------------
Date: Tue, 07 Oct 2008 14:27:18 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: efficient way to write multiple loops code
Message-Id: <WuQGk.1071$aM4.152@newsfe13.iad>
friend.05@gmail.com wrote:
> On Oct 7, 4:48Â pm, Tim Greer <t...@burlyhost.com> wrote:
>> friend...@gmail.com wrote:
>>
>> > I have 3 different files in following format. ($file_1, $file_2,
>> > $file_3)
>>
>> > ID | Time | IP | Code
>>
>> > Following is psuedo code which I am writing. I want to know another
>> > efficient way to do same thing.
>>
>> > open(INFO_1,$file_1);
>> > open(INFO_2,$file_2);
>> > open(INFO_3,$file_3);
>>
>> > @file1_lines = <INFO_1>;
>> > @file2_lines = <INFO_2>;
>> > @file3_lines = <INFO_3>;
>>
>> > So I am going thorugh each data of file 1 and depending on if data
>> > is present in file2 and again depending on some if condition I look
>> > for that data in file3.
>>
>> > So each data of file1 will have to go through each data of file2
>> > and each data of file2 will have to go thorugh file3.
>>
>> > So this code is taking lot of time. I want some suggestion for
>> > efficient code.
>>
>> > Can I use Hash Array (by reading file in  hash array)
>>
>> The answer very much depends on what #some code is actually doing.
>> Is the data fixed in the files, what specific checks are you doing?
>> Could the data be anywhere in a file, inside of a line of data, or
>> are you trying to match lines from ^ start to $ end of line per file,
>> or are you doing some other type of processing?
>> --
>>
>> - Show quoted text -
>
> I am checking data from a line not whole line.
>
> I want to check if IP and Code of file1 is present in file2 and if it
> is present in file2 then check again if it is there in file3.
>
> I am doing all this processing analyze some data.
>
> Let me know if still it is not clear.
>
> Thanks.
I'd personally just either create a hash key and value based on it, if
there's not a lot of data involved, and open the next file and check if
it exists that way, which you can check per line with a while loop
against file 2 and 3 (if needed), instead of reading all three files
into arrays. If the files are potentially large, you'll want to avoid
that because it'll read a lot of data into memory that wouldn't be
necessary. I'd open the first file, do a split on a while loop and
create a hash, close it and then open file 2 and do a while loop and
check to see if the hash key/val exists. If not, repeat for file 3.
There is probably a better way than that, but that's a generally better
idea off the top of my head with what you're attempting now.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
------------------------------
Date: Wed, 08 Oct 2008 08:41:30 +1100
From: Grant <g_r_a_n_t_@dodo.com.au>
Subject: Re: efficient way to write multiple loops code
Message-Id: <60lne4h1f3cfml54fd8ue91ivju5ldqfbr@4ax.com>
On Tue, 7 Oct 2008 13:31:50 -0700 (PDT), "friend.05@gmail.com" <hirenshah.05@gmail.com> wrote:
>Hi,
>
>I am trying to analyze some data. I have big data files.
>
>I have 3 different files in following format. ($file_1, $file_2,
>$file_3)
>
>ID | Time | IP | Code
>
>Following is psuedo code which I am writing. I want to know another
>efficient way to do same thing.
Who knows, without seeing your data and requirements, but I'll offer
this optimised database table loader as an example that follows all
the speedup clues from the camel book. Loads a ~100k record data
table followed by a ~250 record table on a slow 500MHz Celeron box
in about 3 seconds:
...
do_log("read: $indexfile");
open FILE, "< $indexfile" or do_die("$indexfile $!");
flock FILE, 1;
$ip2c_cn = 0;
while (<FILE>) {
next if /^$/; next if /^#/; next if /^junkview/; chomp;
( $ip2c_lo[++$ip2c_cn],
$ip2c_hi[$ip2c_cn],
$ip2c_cc[$ip2c_cn]
) = split /\s+/, $_;
}
close FILE;
do_log("read: $namesfile");
open FILE, "< $namesfile" or do_die("$namesfile $!");
flock FILE, 1;
%cc_name = ();
while (<FILE>) {
next if /^$/; next if /^#/; next if /^junkview/; chomp;
my ($cc, $name) = split /:/, $_;
$cc_name{$cc} = $name;
}
close FILE;
}
You can see that as far as possible you avoid useless processing of
irrelevant data, so plan on how to skip (with 'next') over sections
of your loop code rather than using 'if ... processing', avoid complex
regexps, don't chomp records that are about to be discarded.
From log file:
2008-10-07.21:28:17 - read: /etc/ip2cn-server.conf
2008-10-07.21:28:17 - read: /usr/local/share/ip2cn/ip2c-data
2008-10-07.21:28:20 - read: /usr/local/share/ip2cn/ip2c-names
2008-10-07.21:28:20 - listen: localhost:4743
Context: http://bugsplatter.id.au/ip2cn/ip2cn-server.txt
Grant.
--
http://bugsplatter.id.au/
------------------------------
Date: Tue, 07 Oct 2008 20:46:33 GMT
From: sln@netherlands.com
Subject: Re: How it works?(about while loop and regex as condition)
Message-Id: <2dine49hd4nl77itnhjt9nr2dn5jet713j@4ax.com>
On Mon, 6 Oct 2008 02:41:30 -0700 (PDT), "havel.zhang" <havel.zhang@gmail.com> wrote:
>dear perl-gurus,
>i don't understand how this function works. can you please give me
>further
>explanation:
>
I tried but you didn't listen.
The function does not work well for what you are doing.
Not at all, never will.
<snip>
>I want to using this program pick out hrefs and lables like
>"link1","text.txt","fes.iso".
No you don't, this is not how to do it. It fails easily.
>Can any perl guru give me answer?
The answer was given in detail, and at great time expense.
Next time there will be no answer.
sln
------------------------------
Date: Tue, 07 Oct 2008 13:59:44 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: How it works?(about while loop and regex as condition)
Message-Id: <55QGk.2075$bc2.1566@newsfe02.iad>
havel.zhang wrote:
> foreach(@l){
> if ($_ =~ /<a\b([^>]+)(.*?)<\/a>/ig){
> $html=$_;
> while($html =~ m{a\b([^>]+)(.*?)</a>}ig){
> my $Guts = $1;
> my $Link = $2;
> print "$Guts\n$Link\n";
> }
> }
It steps through the @l array, and for each element within it, it checks
$_ (which is by default the value of the for/foreach/while, so you
don't actually need to declare it).
It then checks that $_ can find an opening HTML tag that starts with
"a", which is an anchor (hot link), most likely anyway with a word
boundary \b to ensure it's not some other tag that starts with "a",
such as <applet> (just an example), and takes anything that's not an
ending HTML tag (>) an captures it into $1. Then, it captures anything
else between that last match and the ending anchor tag (</a> -- seen as
<\/a>) and captures it into $2. It does this check globally and
without letter case. Of course, that regex doesn't make sense, and
neither does the check, to be honest, but no matter.
After the above check, which I assume is to see if there's a matching
anchor tag, and if there is, then it continues, it then assigns the
$html variable the value of $_, does a while look and case
insensitively and globally, checks for the same exact thing it just did
above and assigns and prints the $Guts and $Link variables the values
of the first and second match it captured ($1 and $2, respectively) and
prints it out. The above code really isn't very good and doesn't make
sense, it's repeating things that can be done in one check, it captures
values it's never going to use, etc. It should instead just use the
one and even that one is not correct. It should be
m{a\b([^>]+)>(.*?)</a>}ig
Notice the addition of ">" between ([^>]+) and (.*?). Otherwise $2 will
always start with < (is that what you want? It also would match any
non valid values when checking the anchor tag, which doesn't seem like
it would do any good. If it works, great, but there are some wastes of
processing and bugs so you should expect the unexpected if you run it
against many HTML files.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
------------------------------
Date: Tue, 07 Oct 2008 13:20:59 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: perl cgi script - reload
Message-Id: <_mJGk.41405$Nu3.19891@newsfe14.iad>
ckck wrote:
> I have cgi script which is written in perl. The script contains text
> field, radio group and submit button. And I like to add a button for
> reloading. Please help.
> Thanks
>
> ck
This doesn't sound like an actual Perl question. What do you mean by
"reload"? Do you mean to reload the page, and if so, did you want to
do this with something like JavaScript or were you talking about a
submit button in CGI where it would load a new page, or something else?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
------------------------------
Date: Tue, 07 Oct 2008 23:54:21 +0200
From: Hans Mulder <hansmu@xs4all.nl>
Subject: Re: sysread
Message-Id: <48ebdb43$0$185$e4fe514c@news.xs4all.nl>
Ben Morrow wrote:
> Quoth Larry <dontmewithme@got.it>:
>> In article <slrngek930.67p.tadmc@tadmc30.sbcglobal.net>,
>> Tad J McClellan <tadmc@seesig.invalid> wrote:
>>
>>> http://perl.plover.com/FAQs/Buffering.html
>> Thanks, I read that. Too bad my system (Mac OS X) doesn't support:
>> setvbuf();
>
> Modern perls (as of 5.8) use PerlIO instead of stdio, so what your
> system supports is irrelevant.
The stdio on my Mac OS X system supports setvbuf(), but perlIO does not.
For example, if I do:
use IO::Handle '_IOFBF';
open FH, "</etc/motd" or die $!;
FH->setvbuf($buffer_var, _IOFBF, 8151);
, I get:
IO::Handle::setvbuf not implemented on this architecture at t.pl line 3.
This is using the perl 5.8.6 that came bundled with Mac OS X 10.4.2
-- HansM
------------------------------
Date: Tue, 07 Oct 2008 19:01:46 GMT
From: sln@netherlands.com
Subject: Re: update XML file with perl or other...?
Message-Id: <mjvme4907eumof042vjhmdmfo95kf39sb0@4ax.com>
On Mon, 6 Oct 2008 19:46:33 -0700 (PDT), inetquestion <inetquestion@hotmail.com> wrote:
>On Oct 6, 10:25 pm, inetquestion <inetquest...@hotmail.com> wrote:
>> I have an XML document which I would like to modify based on the
>> results of a test. A subset of information contained in the XML file
>> is shown below. If a 'test' to each server:port were to fail, then
>> the xml file should be modified such that the attribute 'off' is set
>> to a value of 1. I was thinking of doing this in perl, but would
>> like to get some suggestions based on ease of modifying files, etc...
>> I've written some basic perl scripts to read an xml file before, but
>> ran into some confusion when trying to write them back out. Any
>> suggestions or pointers?
>>
>> ...
>> ...
>> <app name="hokiepokie">
>> <instance host="server01" port="8080" Off="0"/>
>> <instance host="server02" port="8081" Off="0"/>
>> <instance host="server03" port="8082" Off="0"/>
>> </app>
>> ...
>> ...
>>
>> -Inet
>
>
>for example, lets say the test on server02 failed, what procedure
>would need to occur to change the xml file as follows? If possible I
>would prefer to do this via traditional xml methods and not text file
>parsing. I'm just a little lost as to how to do it...
>
> <app name="hokiepokie">
> <instance host="server01" port="8080" Off="0"/>
> <instance host="server02" port="8081" Off="1"/>
> <instance host="server03" port="8082" Off="0"/>
></app>
Here is one approach. This avoids regenerating the entire xml file
from scratch. The raw xml is kept intact. Only the lines you pick and
modify are changed.
The capture buffers are just array's of sequence number which point to a
central data repository. The Dump function uses the sequence number
to display the raw data. Modifications are made using the sequence reference.
This will be released soon.
sln
===========================================================
<<XML;
<!--
Notes: This xml file contains server/port information.
-->
<root>
...
...
<app name="hokiepokie">
<instance host="server01" port="8080" Off="0"/>
<instance host="server02" port="8081" Off="0"/>
<instance host="server03" port="8082" Off="0"/>
</app>
...
...
</root>
XML
# Your program.pl
# ------------
use strict;
use warnings;
use RXParse; # VERSIN 2
my $p = new RXParse();
sub starth
{
my ($obj, $el, $term, @attr) = @_;
my $buffer = lc($el);
if ($buffer eq 'instance')
{
$obj->CaptureOn ( $buffer);
}
}
sub endh
{
my ($obj, $el, $term) = @_;
my $buffer = lc($el);
if ($buffer eq 'instance')
{
$obj->CaptureOff ( $buffer, 1);
}
}
$p->setMode( 'resume_onerror'=> 1 );
$p->setHandlers ( 'start' => \&starth, 'end' => \&endh);
my $fname = 'c:\temp\hokie.xml';
open my $fh, $fname or die "can't open $fname ...";
$p->CaptureOn ( 'ALL');
my $parse_errors = $p->parse ( $fh);
$p->CaptureOff ( 'ALL');
print STDERR "Parse errors = $parse_errors\n";
close $fh;
$p->DumpCaptureBuffs (); # to view buffers
## Process the 'instance' buffer raw data. Can use rxparse built-ins if needed.
## There are many ways to do this, this is just one.
## ...
## Xml-Simple example, straight forward but not tested
#
if (0)
{
use XML::Simple;
## Get 'instance' buffer ref's to its raw data
my @instrefs = $p->GetCaptureBuffer ( 'instance'); # this function is not firm yet
## Process it
foreach my $iref (@instrefs)
{
if (defined $$iref) # In this case it will always be defined
{
my $simpref = XMLin ( $$iref, SuppressEmpty => '');
my ($host, $port, $off) = ($simpref->{host}, $simpref->{port}, $simpref->{off});
## Check the host/port status for on/off
if (1) {
$simpref->{off} = 1; # Turn it off
} else {
$simpref->{off} = 0; # Turn it on (or skip if on by default)
}
## Write it back to the instance buffer (if 'off' modified)
$$iref = XMLout ( $simpref);
}
}
## All done, write the 'all' buffer out to a file (if 'off' modified)
if (1)
{
my $fname = 'c:\temp\hokie_new.xml';
open my $fh, $fname or die "can't open $fname ...";
$p->WriteCaptureBuffer ( 'all', $fh); # this function is not firm yet.
close $fh; # can pass in file handle or ref to recieving buf.
}
}
__END__
BUFFER: instance
=====================================
index seqence
----- --------
[0] 2 <instance host="server01" port="8080" Off="0"/>
[1] 4 <instance host="server02" port="8081" Off="0"/>
[2] 6 <instance host="server03" port="8082" Off="0"/>
BUFFER: all
=====================================
index seqence
----- --------
[0] 1 <!--
Notes: The file contains server/port information.
-->
<root>
...
...
<app name="hokiepokie">
[1] -2
[2] 3
[3] -4
[4] 5
[5] -6
[6] 7
</app>
...
...
</root>
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1906
***************************************