am a fairly newbiee at coding in perl so please bear with me. I am sure this is probably a simple issue, but cant seem to figure it out myself. Basically I have this code that pulls in XML using XML::Twig, and then is supposed to process based on when it hits a certain criteria. Well the issue is that it works, but it loops 4 times with that code. I have figured out that it has to do with the XML having 4 entries, but am not sure how to clean it up and remove it. I have some variables that will be dynamic once I get past this section, so the code is not complete yet, so please bear with me. Can anyone help out? Below is my code and the XML that I am working with.
#!/usr/local/bin/perl use XML::Twig; use Net::Telnet(); $db = "hrdm2"; $comm = "stop-$db.sh"; $cachedel = "No"; my $s= new XML::Twig( TwigHandlers=> { Product => \&product}); $s->parsefile( 'testservers2.xml'); exit; sub product { my ($s, $product)= @_; my %product; $product{id}= $product->field( 'ServerID'); $product{name}= $product->field( 'DBName'); $product{commandstop}= $product->field( 'CommandStop'); $product{commandstart}= $product->field( 'CommandStart'); $product{cache}= $product->field( 'CacheDir'); if ($db eq $product{name} && $comm eq $product{commandstop} && $ +cachedel eq 'No') { print "DB Name: $product{name}\n" ; print "Command: $product{commandstop}\n"; print "Server: $product{id}\n"; print "Cache Dir:$product{cache}\n"; print "Cache Del: No\n"; $pass = "pass"; $t = new Net::Telnet (Timeout => 500); $t->open("$product{id}"); $t->login('root', $pass); @lines = $t->cmd("cd /apps/gcpsscripts"); @lines = $t->cmd("ls"); @lines = $t->cmd("sh $product{commandstop}"); print @lines; $product->delete; } elsif ($db eq $product{name} && $comm eq $product{commandstop} && +$cachedel eq 'Yes') { print "DB Name: $product{name}\n" ; print "Command: $product{commandstop}\n"; print "Server: $product{id}\n"; print "Cache Dir:$product{cache}\n"; print "Cache Del: Yes\n"; $pass = "pass"; $t = new Net::Telnet (Timeout => 500); $t->open("$product{id}"); $t->login('root', $pass); @lines = $t->cmd("cd /apps/gcpsscripts"); @lines = $t->cmd("ls"); @lines = $t->cmd("sh $product{commandstop}"); print @lines; $product->delete; } elsif ($db eq $product{name} && $comm eq $product{commandstart}) { print "DB Name: $product{name}\n" ; print "Command: $product{commandstart}\n"; print "Server: $product{id}\n"; $pass = "pass"; $t = new Net::Telnet (Timeout => 500); $t->open("$product{id}"); $t->login('root', $pass); @lines = $t->cmd("cd /apps/gcpsscripts"); @lines = $t->cmd("ls"); @lines = $t->cmd("sh $product{commandstart}"); print @lines; $product->delete; } open (LOGFILE, '>>C:\\temp\\stopstart.log') or die "Can't open + log file.\n"; print LOGFILE "\n"; print LOGFILE "@lines \n"; print LOGFILE "\n"; close LOGFILE; }
XML Code below
<?xml version='1.0'?> <Products> <Product> <DBName>hrdm2</DBName> <ServerID>psdevax03</ServerID> <CommandStop>stop-hrdm2.sh</CommandStop> <CommandStart>start-hrdm2.sh</CommandStart> <CacheDir>/apps/846hrdm2/appserv/hrdm2/CACHE</CacheDir> </Product> <Product> <DBName>hrdev</DBName> <ServerID>psdevax06</ServerID> <CommandStop>stop-hrdev.sh</CommandStop> <CommandStart>start-hrdev.sh</CommandStart> <CacheDir>/apps/846hrdev/appserv/hrdev/CACHE</CacheDir> </Product> <Product> <DBName>epdev</DBName> <ServerID>psdevax06</ServerID> <CommandStop>stop-epdev.sh</CommandStop> <CommandStart>start-epdev.sh</CommandStart> <CacheDir>/apps/846epdev/appserv/epdev/CACHE</CacheDir> </Product> <Product> <DBName>eptst</DBName> <ServerID>psdevax06</ServerID> <CommandStop>stop-eptst.sh</CommandStop> <CommandStart>start-eptst.sh</CommandStart> <CacheDir>/apps/846eptst/appserv/eptst/CACHE</CacheDir> </Product> </Products>

In reply to Perl code looping using XML::Twig by coding_new

Title:
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, 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:
    & &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.