Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Perl code looping using XML::Twig

    I 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.

    Code:
    #!/usr/local/bin/perl
    use XML::Twig;
    use Net::Telnet();
    
    $db = "hrdm2";
    $comm = "stop-$db.sh";
    $cachedel = "No";
    
    print "$comm   $db\n\n";
    
    my $s= new XML::Twig( TwigHandlers=> { Product => \&product});
    $s->parsefile( 'testservers2.xml');
    #$s->print;
    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');
        
        print "$product{id}\n";
        print "$product{name}\n";
        
        #$product->delete;
        
          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;
            }
        
      }
    XML

    Code:
    <?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> 
    
    </Products>
    Resolution was found. Basically it involved an exit code to get out from
    Last edited by coding_new; 04-21-2009 at 02:20 AM. Reason: Resolution found. Involved returning from the twig xml once a codition was met.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •