[31870] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3133 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 16 06:09:22 2010

Date: Thu, 16 Sep 2010 03:09:07 -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           Thu, 16 Sep 2010     Volume: 11 Number: 3133

Today's topics:
    Re: add document tags to xml doc sln@netherlands.com
    Re: add document tags to xml doc <xhoster@gmail.com>
    Re: add document tags to xml doc <ben@morrow.me.uk>
    Re: Marc the Reaper (Alan Curry)
    Re: Marc the Reaper <marc.girod@gmail.com>
    Re: Marc the Reaper <marc.girod@gmail.com>
    Re: Marc the Reaper <derykus@gmail.com>
    Re: text::CSV <notvalid@cox.net.invalid>
    Re: Typeglobs and References <ben@morrow.me.uk>
    Re: Typeglobs and References <derykus@gmail.com>
    Re: Typeglobs and References <schaitan@gmail.com>
    Re: Typeglobs and References <ben@morrow.me.uk>
    Re: Typeglobs and References <nospam-abuse@ilyaz.org>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 15 Sep 2010 09:25:17 -0700
From: sln@netherlands.com
Subject: Re: add document tags to xml doc
Message-Id: <b9s196thiqooq38328tis7tjo14cjhrnqm@4ax.com>

On Tue, 14 Sep 2010 18:15:48 -0700 (PDT), DJ Stunks <djstunks@gmail.com> wrote:

>On Sep 14, 6:26 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>> Quoth DJ Stunks <djstu...@gmail.com>:
>>
>>
>>
>> > hey all,
>>
>> > I'm using XML::Parser to parse an xml file which is not well-formed.
>> > The source I receive it from formats it as:
>>
>> > $ cat data.xml
>> > <row><data foo="a"/></row>
>> > <row><data baz="b"/></row>
>>
>> > Obviously the parser chokes on this.  If I manually add document tags
>> > as follows, my script is fine:
>>
>> > $ cat fixed-data.xml
>> > <d>
>> > <row><data foo="a"/></row>
>> > <row><data baz="b"/></row>
>> > </d>
>>
>> > Question: script is below.  What is the easiest way to add the
>> > document tags such that the parser doesn't choke without the
>> > additional step of manually adding them?  I can pass a filehandle to
>> > the parser if there was a way to add document tags to the filehandle,
>> > but the file is very large and I couldn't think of an easy way to add
>> > the data to the filehandle without slurping into a scalar.
>>
>> See XML::Parser->parse_start. You will obviously have to handle reading
>> chunks from the file manually.
>
>Thanks very much, Ben.  With the modified code below I'm good to go.
>It took a bit of hunting on that method, thanks for pointing it out.
>
>-jp
>
>#!/usr/bin/perl
>
>use strict;
>use warnings;
>
>use XML::Parser;
>
>my $file = 'data.xml';
>
>my $parser = XML::Parser->new(Handlers => { Start =>
>\&handle_start });
>
>my $p = $parser->parse_start();
>$p->parse_more('<d>');
>
>open (my $fh, '<', $file) or die "Could not open '$file': $!";
>
>LINE:
>while (my $line = <$fh>) {
>	$p->parse_more($line);
>}
>
>sub handle_start {
>        my ($p, $el, %atts) = @_;
>
>        if ($el eq 'data') {
>                for my $k (keys %atts) {
>                        print "$k: $atts{$k}\n";
>                }
>        }
>
>}
>
>__END__
>
>

Its likely that you will want to finish the ExpatNB parse
with a call to XML::Parser::ExpatNB::parse_done because it
releases any circular data structure references.

But calling parse_done() validates your closures.
The sequence then requires a final call to parse_more('</d>').
Something like below.

-sln
----------
use strict;
use warnings;

use XML::Parser;

my $parser = XML::Parser::ExpatNB->new();
$parser->setHandlers(Start => \&handle_start);


print $parser,"\n";

{ local $/;
  $parser->parse_more('<d>');
  $parser->parse_more(<DATA>);
  $parser->parse_more('</d>');
  $parser->parse_done;
}

sub handle_start {
        my ($p, $el, %atts) = @_;
        if ($el eq 'data') {
                for my $k (keys %atts) {
                        print "$k: $atts{$k}\n";
                }
        }
}

__DATA__

<row><data foo="a"/></row>
<row><data baz="b"/></row>

-----------
XML::Parser::ExpatNB=HASH(0x18757d4)
foo: a
baz: b



------------------------------

Date: Tue, 14 Sep 2010 20:29:36 -0700
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: add document tags to xml doc
Message-Id: <4c916fb5$0$10972$ed362ca5@nr5-q3a.newsreader.com>

DJ Stunks wrote:
> hey all,
> 
> I'm using XML::Parser to parse an xml file which is not well-formed.
> The source I receive it from formats it as:
> 
> $ cat data.xml
> <row><data foo="a"/></row>
> <row><data baz="b"/></row>
> 
> Obviously the parser chokes on this.  If I manually add document tags
> as follows, my script is fine:
> 
> $ cat fixed-data.xml
> <d>
> <row><data foo="a"/></row>
> <row><data baz="b"/></row>
> </d>
> 
> Question: script is below.  What is the easiest way to add the
> document tags such that the parser doesn't choke without the
> additional step of manually adding them?  I can pass a filehandle to
> the parser if there was a way to add document tags to the filehandle,
 > but the file is very large and I couldn't think of an easy way to add
 > the data to the filehandle without slurping into a scalar.

Ben's answer is better, of course, but here is a dirty way to get
such file handle if you really need a file handle:

open my $fh, q{echo "<d>"; cat $file; echo "</d>"|} or die $!;

It's not portable, not tolerant of special characters in $file, and may 
not fail as expected if $file is not readable.

Xho


------------------------------

Date: Thu, 16 Sep 2010 04:03:47 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: add document tags to xml doc
Message-Id: <j1e8m7-2ma.ln1@osiris.mauzo.dyndns.org>


Quoth Xho Jingleheimerschmidt <xhoster@gmail.com>:
> DJ Stunks wrote:
> > 
> > Question: script is below.  What is the easiest way to add the
> > document tags such that the parser doesn't choke without the
> > additional step of manually adding them?  I can pass a filehandle to
> > the parser if there was a way to add document tags to the filehandle,
>  > but the file is very large and I couldn't think of an easy way to add
>  > the data to the filehandle without slurping into a scalar.
> 
> Ben's answer is better, of course, but here is a dirty way to get
> such file handle if you really need a file handle:
> 
> open my $fh, q{echo "<d>"; cat $file; echo "</d>"|} or die $!;

Eww, yuck :).

It's a nice idea, though, if you use open "-|" instead, and do the
writing from Perl.

Ben



------------------------------

Date: Wed, 15 Sep 2010 21:52:32 +0000 (UTC)
From: pacman@kosh.dhis.org (Alan Curry)
Subject: Re: Marc the Reaper
Message-Id: <i6rf6v$d1b$1@speranza.aioe.org>

In article <da9f5d91-33ed-43a1-a503-7e6123af748a@t7g2000vbj.googlegroups.com>,
Marc Girod  <marc.girod@gmail.com> wrote:
>Hello,
>
>I have a nightly build script which forks a lot of processes which may
>last long or not.
>I wanted to reap them before the end of the script.
>This addition made, I lose the exit code of my builds: they all
>pretend to fail, which I suspect to be false.
>So, I guess I was not able to read the perlipc page correctly.
>
>Here is a short script which behaves in the same way as my nightly
>build;

Your child process inherited the signal handler from its parent. The signal
handler is stealing the child's exitcode before system() can get it. Since
your signal handler isn't necessary in the child process, you should be able
to fix this by setting $SIG{CHLD}='DEFAULT' before calling system().

-- 
Alan Curry


------------------------------

Date: Wed, 15 Sep 2010 21:50:10 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: Marc the Reaper
Message-Id: <6a20beaf-c820-46d5-9627-fa4eb94fa5ce@f26g2000vbm.googlegroups.com>

On Sep 15, 10:52=A0pm, pac...@kosh.dhis.org (Alan Curry) wrote:

> Your child process inherited the signal handler from its parent. The sign=
al
> handler is stealing the child's exitcode before system() can get it. Sinc=
e
> your signal handler isn't necessary in the child process, you should be a=
ble
> to fix this by setting $SIG{CHLD}=3D'DEFAULT' before calling system().

Thanks.
Marc


------------------------------

Date: Thu, 16 Sep 2010 00:18:55 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: Marc the Reaper
Message-Id: <b9171d5f-db7b-45f5-89e1-f0061ce6284f@t3g2000vbb.googlegroups.com>

Hi,

Maybe this issue with $?, and even resetting the handler in child
processes, should be mentioned in the perlipc chapter on reapers?
I know it is pretty long already.

I believe btw that I found one minor glitch there: $child was renamed
(or so I guess) to $waitedpid (with a change in scope) from one
example to an other, but the 'my' definition was left (forgotten)
useless.

Marc


------------------------------

Date: Thu, 16 Sep 2010 02:13:33 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Marc the Reaper
Message-Id: <d76556c5-6bd9-4207-84d3-963aa6612d8d@p22g2000pre.googlegroups.com>

On Sep 15, 6:22=A0am, Marc Girod <marc.gi...@gmail.com> wrote:

> ...

Another suggestion on the code. The parent's
sleep loop before exiting has a redundant
waitpid. The reaper handler is asynchronous
and is still reaping during the sleep loop.

So you could change the handler slightly and
eliminate the waitpid in the sleep loop.

> ...

  sub reaper {
 =A0  while ((my $child =3D waitpid(-1, WNOHANG)) > 0) {
 =A0 =A0  if (exists $family{$child}) {
 =A0 =A0 =A0  my $prd =3D delete $family{$child};
 =A0 =A0 =A0  warn "Reaped $prd($child) with exit $?\n";
 =A0 =A0 }
     $SIG{CHLD} =3D \&reaper;
 =A0 }

> ...

sleep 1 while keys %family;   # eliminate redundant waitpid

--
Charles DeRykus


------------------------------

Date: Wed, 15 Sep 2010 14:53:11 -0400
From: John Mason Jr <notvalid@cox.net.invalid>
Subject: Re: text::CSV
Message-Id: <i6r4mo$oid$1@news.eternal-september.org>

On 9/15/2010 12:59 AM, king wrote:
> I have a script which i converted into executable using PDK.
>
> when i run the executable using i get a error saying
> "
> C:\Users\hacharyx\Desktop\C_State_Residency>C_State_residency.exe
> Can't locate Text/CSV_PP.pm in @INC (@INC contains:) at (eval 12) line
> 2.
>   at perlapp line 843
> BEGIN failed--compilation aborted at C_State_residency.pl line 5."
>
> But when I run the .pl file it works fine.
>
> The script is as follows.
> "
> #!/c/perl64/bin
>
> use strict;
> use warnings;
> use Text::CSV;
> use Win32::OLE('in');
> ######################################################################
> sleep 4;
>
> system("typeperf \"\\Processor\(\*\)\\% C1 Time\" -sc 50 -o c1.csv");
>
> sleep 1;
> ######################################################################
> my $file_1 = 'c1.csv';
>
> my $csv_1 = Text::CSV->new();
>
> open (CSV_1, "<", $file_1) or die $!;
> my @c1_total;
> my @column_1;
> my $value_1;
> my $i;
> my $sum_1=0;
>
> while (<CSV_1>)
> {
> 	if ($csv_1->parse($_))
> 	{
> 		my @columns_1 = $csv_1->fields();
>
> 		#print "@columns_1\n";
> 		push(@c1_total, $columns_1[$#columns_1]);
>
> 	}
> 	else
> 	{
> 		my $err = $csv_1->error_input;
> 		print "Failed to parse line: $err";
> 	}
> }
> close CSV_1;
>
> shift(@c1_total);
> shift(@c1_total);
>
> foreach $value_1 (@c1_total)
> {
> 	print "$value_1\n";
> }
> for($i=0; $i<= $#c1_total; $i++)
> {
> 	$sum_1 = $sum_1 + $c1_total[$i];
> }
> my $avg_1 = $sum_1/($#c1_total+1);
>
> open(Mini_Bat,">>C:\\Tests\\Logs\\Mini_Bat\\Mini_Bat.txt");
> print "C1 Avg: $avg_1\n";
> print Mini_Bat "% of avg. C1 Time: $avg_1\n";
> ######################################################################
> "
>
> Can anybody help me why it is not working if run after converting into
> a executable?



Just tell perl app to include the module and you will be fine.

John


------------------------------

Date: Wed, 15 Sep 2010 18:04:54 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Typeglobs and References
Message-Id: <mua7m7-kks2.ln1@osiris.mauzo.dyndns.org>


Quoth Krishna Chaitanya <schaitan@gmail.com>:
> Hi everybody,
> 
> I've read in Perl books that when used as lvalues, typeglobs are
> "equivalent" to references...something like this is constantly quoted
> as an example of selective aliasing:
> 
> *b = \$a; # aliases $b to $a but leaves @b,%b,etc untouched
> 
> Also, I've read about the *foo{THING} notation...accessing $a as:
> 
> print ${*a{SCALAR}}; # prints the value of $a
> 
> But what is this following code I see in some places:
> 
> print ${*a}; # ALSO prints the value of $a
> 
> I am confused.....how is ${*a} equivalent to ${*a{SCALAR}} ? If these
> 2 are equivalent, why follow the *foo{THING} notation at all...? Seems
> like the '{THING}' part is quite useless here? Am I wrong, or am I
> missing anything subtle/obvious here?

Typeglobs are weird, and all the syntax involving them is a mess of
special cases? Certainly there are cases where *foo{THING} is necessary,
as for instance with *foo{IO}. Also, @{*x} will auto-viv @x, whereas
@{*x{ARRAY}} will not (this doesn't apply to scalars, as all current
versions of perl (appear to) always have a SCALAR slot in every glob).

Unless you know you can't do whatever you're trying to do without using
typeglobs, stay away from them.

Ben



------------------------------

Date: Wed, 15 Sep 2010 12:05:53 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Typeglobs and References
Message-Id: <8b4644c8-e2ab-4021-874b-37c5e88da994@g21g2000prn.googlegroups.com>

On Sep 15, 6:27=A0am, Krishna Chaitanya <schai...@gmail.com> wrote:
> Hi everybody,
>
> I've read in Perl books that when used as lvalues, typeglobs are
> "equivalent" to references...something like this is constantly quoted
> as an example of selective aliasing:
>
> *b =3D \$a; # aliases $b to $a but leaves @b,%b,etc untouched
>
> Also, I've read about the *foo{THING} notation...accessing $a as:
>
> print ${*a{SCALAR}}; # prints the value of $a
>
> But what is this following code I see in some places:
>
> print ${*a}; # ALSO prints the value of $a
>
> I am confused.....how is ${*a} equivalent to ${*a{SCALAR}} ? If these
> 2 are equivalent, why follow the *foo{THING} notation at all...? Seems
> like the '{THING}' part is quite useless here? Am I wrong, or am I
> missing anything subtle/obvious here?
>
> Pls. enlighten.....thanks a lot.
>
> -Chaitanya


The sigil '$' in ${*a} provides the context that
enables the parser to lookup the glob's SCALAR
slot. In this setting, you can omit the SCALAR
but, within docs, the tag provides a quick, clear
way of identifying an individual glob slot. This
becomes very handy in perlref in the discussions
of ref/glob equivalencies for instance:

     $scalarref =3D *foo{SCALAR};
     $arrayref  =3D *ARGV{ARRAY};
     ...

See: perldoc perlref

BTW, ${\$a} is also equivalent to ${*a}. You can
usually just use a reference and rarely need to
deal with the glob notation at all.

--
Charles DeRykus


------------------------------

Date: Wed, 15 Sep 2010 21:46:38 -0700 (PDT)
From: Krishna Chaitanya <schaitan@gmail.com>
Subject: Re: Typeglobs and References
Message-Id: <99b09aa8-c537-40ce-88aa-0fd6e2437c54@l25g2000prn.googlegroups.com>

Thanks a lot, Ben and Charles. It's clearer to me now...and yeah I've
gone through perlref and perlmod just now.

Ben, what I'm trying to do is to understand code someone else wrote -
which, for reasons best known to him, uses typeglobs in many
places...I want to understand this concept and replace that code with
reference-based code as much as possible (but certainly not blindly,
stupidly or without realizing why he used globs at all...)


------------------------------

Date: Thu, 16 Sep 2010 06:03:34 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Typeglobs and References
Message-Id: <62l8m7-qdb.ln1@osiris.mauzo.dyndns.org>


Quoth Krishna Chaitanya <schaitan@gmail.com>:
> 
> Ben, what I'm trying to do is to understand code someone else wrote -
> which, for reasons best known to him, uses typeglobs in many
> places...I want to understand this concept and replace that code with
> reference-based code as much as possible (but certainly not blindly,
> stupidly or without realizing why he used globs at all...)

Ah, I see :). Well, that's certainly a worthy goal. If the code (or the
coder) is old it's likely it's the way it is because Perl 4 didn't
support true refs, leading to lots of nasty tricks with globs of the
sort you described. If you have any issues with specific bits of code,
don't hesitate to ask about them.

Ben



------------------------------

Date: Thu, 16 Sep 2010 08:09:15 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Typeglobs and References
Message-Id: <slrni93k5b.hn0.nospam-abuse@powdermilk.math.berkeley.edu>

On 2010-09-15, C.DeRykus <derykus@gmail.com> wrote:
> BTW, ${\$a} is also equivalent to ${*a}.

Of course, it is NOT.

>perl -wle "$a=12; my $a=2; print ${\$a}; print ${*a}"
2
12

Hope this helps,
Ilya


------------------------------

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 3133
***************************************


home help back first fref pref prev next nref lref last post