Here is a way to make it work, which only creates a single XML::Twig object in the thread, then reuses it. (Also its 'use threads', not 'use Thread'. I made it as simple as I could for clarity. Threads need to go to the end of their code block in order to be joined, but a return will work as well. Anyways, this creates 1 thread, and 1 XML::Twig object, then reuses it.
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;
my $tgo;
my $tdie;
my $tfile;
share $tgo;
share $tdie;
share $tfile;
$tgo = 0;
$tdie = 0;
$tfile = '';
my $thread = threads->new(\&querySystem);
foreach my $file('xmlfile.xml', 'xmlfile.xml' ){
$tfile = $file;
$tgo = 1;
sleep 5;
}
$tdie = 1;
$thread->join;
print "done press any key to exit\n";
<>;
###################################################
sub querySystem
{
use XML::Twig;
$|++;
my $statusTwig = new XML::Twig(
twig_roots => {'Result/Status' => sub{
my($twig, $element) = @_;
my $mmsVersion = $element->first_child('MMS
+Version')->text;
my $partVersion = $element->first_child('Pa
+rticipantVersion')->text;
print "mmsVersion: $mmsVersion, partVersion
+: $partVersion\n";
} },
ProtocolEncoding=>"x-sjis-unicode");
while(1){
if($tdie == 1){ goto END };
if ( $tgo == 1 ){
my $result = $statusTwig->safe_parsefile($tfile);
$tgo = 0; #turn off self before returning
}else
{ sleep 1 }
}
END:
}
__END__
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.