I have a script as below
#!/usr/bin/perl -w use strict; use warnings; use XML::Simple; my $xml = do {local $/='';'network.xml'} ; my $servers = XMLin($xml, ForceArray => 1); my %seen; foreach my $server (@{$servers->{server}}) { my $brlist; my $corebrip; my $brmask; my $brip; my $num; my $brlan; my @node = $server->{NodeName__1} . "\n"; my $lanip = $server->{CoreLanIP_1__1} ; my @mask = $server->{CoreLanNetmask_1} . "\n"; my $network = join("/", $lanip, @mask); $network =~ s/.[0-9]*\//.0\//g; push(my @networklist, $network) if ! $seen{$network}++; $brlan = $server->{BRLAN} ; if ( $brlan eq "y" ) { $brip = $server->{BR_IP}; $num = $server->{CoreLanNum} ."\n" ; } my @brnwlist; for my $n (1..$num){ $corebrip = $server->{'CoreLanIP_'.$n.'__1'} ; $brmask = $server->{'CoreLanNetmask_'.$n} ."\n"; } if ( $corebrip eq $brip ){ $brlist = join ("/", $brip, $brmask) ; $brlist =~ s/.[0-9]*\//.0\//g; push( @brnwlist, $brlist) unless $seen{$brlist}++; } else { print "coreip doesnot matches brip\n"; } } for $a (@brnwlist) { if ( $a ne "") { print "empty array"; my $vnum = $server->{CoreVlanNum} ."\n" ; for my $i (1..$vnum){ my $vlanip = $server->{'CoreVlan_IP_'.$i} ; my $vmask = $server->{'CoreVlan_Netmask_'.$i} ."\n"; if ($vlanip eq $brip){ my $vlist = join ("/", $brip, $vmask) ; $vlist =~ s/.[0-9]*\//.0\//g; push(my @vnwlist, $vlist) if ! $seen{$vlist}++; print @vnwlist; } } } } }

Now I am not able to figure out how to define the variable globally grepped from xml so that they can be used anywhere inside my script.

Most importantly I have to run a 'if' condition where if $brlan =y then there are two more conditions.

if the array from first for condition i.e. @brnwlist doesnot contains any value and is empty ( which should happen if $brip ne $corebrip) then second for loop should run searching for $vnum.

But I am not sure how to use these variables all across the foreach  my $server (@{$servers->{server}}) loop

my output at the moment
Use of uninitialized value $brip in string eq at nfs_security.pl line +48. Use of uninitialized value $brip in join or string at nfs_security.pl +line 50. Use of uninitialized value $brmask in join or string at nfs_security.p +l line 50. Use of uninitialized value $num in foreach loop entry at nfs_security. +pl line 44. Use of uninitialized value $corebrip in string eq at nfs_security.pl l +ine 48. Use of uninitialized value $brip in string eq at nfs_security.pl line +48. Use of uninitialized value $brip in join or string at nfs_security.pl +line 50. Use of uninitialized value $brmask in join or string at nfs_security.p +l line 50. Use of uninitialized value $num in foreach loop entry at nfs_security. +pl line 44. Use of uninitialized value $corebrip in string eq at nfs_security.pl l +ine 48. Use of uninitialized value $brip in string eq at nfs_security.pl line +48. Use of uninitialized value $brip in join or string at nfs_security.pl +line 50. Use of uninitialized value $brmask in join or string at nfs_security.p +l line 50. Use of uninitialized value $num in foreach loop entry at nfs_security. +pl line 44. Use of uninitialized value $corebrip in string eq at nfs_security.pl l +ine 48. Use of uninitialized value $brip in string eq at nfs_security.pl line +48. Use of uninitialized value $brip in join or string at nfs_security.pl +line 50. Use of uninitialized value $brmask in join or string at nfs_security.p +l line 50. Use of uninitialized value $num in foreach loop entry at nfs_security. +pl line 44. Use of uninitialized value $corebrip in string eq at nfs_security.pl l +ine 48. Use of uninitialized value $brip in string eq at nfs_security.pl line +48. Use of uninitialized value $brip in join or string at nfs_security.pl +line 50. Use of uninitialized value $brmask in join or string at nfs_security.p +l line 50. coreip doesnot matches brip coreip doesnot matches brip coreip doesnot matches brip coreip doesnot matches brip coreip doesnot matches brip coreip doesnot matches brip coreip doesnot matches brip coreip doesnot matches brip
Input xml
<servers> <server NodeName__1="mch30-nds-ins" CoreLanNum="2" CoreBondNum="1" CoreVlanNum="1" CoreVlan_IF_1="vlan2603" CoreVlan_IP_1="192.168.156.16" CoreVlan_Netmask_1="255.255.255.0" CoreLanIP_1__1="192.168.156.16" CoreLanNetmask_1="255.255.255.0" CoreLanIP_2__1="192.168.156.16" CoreLanNetmask_2="255.255.255.0" BRLAN="n" /> <server NodeName__1="mch30-nds-insc" CoreLanNum="2" CoreBondNum="1" CoreVlanNum="2" CoreVlan_IF_1="vlan2605" CoreVlan_IP_1="192.168.156.16" CoreVlan_Netmask_1="255.255.255.0" CoreLanIF_1="eth0" CoreLanIP_1__1="192.168.156.16" CoreLanNetmask_1="255.255.255.0" CoreLanIP_2__1="192.168.156.16" CoreLanNetmask_2="255.255.255.0" BR_IP="192.168.165.16" BR_Interface="vlan3525" BRLAN="y" /> <server NodeName__1="nds-test" CoreLanNum="4" CoreBondNum="2" CoreVlanNum="0" CoreLanIP_1__1="192.169.32.1" CoreLanNetmask_1="255.255.255.0" CoreLanIP_2__1="192.169.32.1" CoreLanNetmask_2="255.255.255.0" BR_IP="192.169.73.1" BRLAN="y" CoreLanIP_3__1="192.169.73.1" CoreLanNetmask_3="255.255.255.240" CoreLanIP_4__1="192.169.73.1" CoreLanNetmask_4="255.255.255.240" CoreLanBond__4="bond3" arp_Target_BnR_IP="192.169.73.254" RouteIP_1__1="192.169.73.0" RouteNetmask_1__1="255.255.255.0" RouteGateway_1__1="192.169.73.254" />

In reply to Use of uninitialized value $brip in join or string at script.pl line 50 by deep27ak

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.