Pretty basic problem but it's got me tearing my hear out. Short, sweet description: I'm calling a sub. At the beginning of the sub, I'm initializing a variable. The variable appears to be acting like a C static and retaining it's value between calls. Here's the code:
use strict; open TXT, "<masks.txt" or die "Can't open masks.txt\n"; while(<TXT>) { chomp; my @words = split(/ +/); print "Checking mask $words[2]\n"; my $maskvalid = ValidateMasks($words[2]); print "Mask is $maskvalid\n\n"; } sub ValidateMasks() { return 0 unless defined $_[0]; my @octets = split (/\./, $_[0]); print "\toctets array is @octets\n"; my $valid = 1; my $index = 0; my $flagvalue = 0; while($index < 4) { $valid = ValidateOctet($octets[$index]); $index++; } return $valid; sub ValidateOctet() { return 0 unless defined $_[0]; # bad if no argument passed return 0 unless $valid; # return bad if any previous argument + is bad my $oc = $_[0]; return 0 if $oc > 255; # bad if value is above 255 print "\tchecking octet $oc\n"; return 1 if ($oc eq $flagvalue); # good if equal to flag valu +e print "\t\tUnequal: oc is $oc, flagvalue is $flagvalue\n"; return 0 if $flagvalue; # bad if this is a second non-zero an +d it's not equal to 255. $flagvalue = 255; # All further octets after this one must be + 255 # This is the first non-zero octet. # Should be equal to a power of 2 minus 1 # X is a power of 2 if (X & X-1) = 0 # $oc + 0 forces value to number vice string my $bitand = ($oc + 0) & ($oc + 1); return ( $bitand ? 0 : 1); } }
And the data file:
field1 10.1.253.11 0.0.0.0 field1 10.1.254.0 0.0.0.64 field1 10.1.254.128 0.0.0.63 field1 10.1.158.0 15.255.0.255 field1 10.1.160.0 0.0.0.37 field1 10.1.161.0 0.0.146.255
And the (truncated) output:
Checking mask 0.0.0.0 octets array is 0 0 0 0 checking octet 0 checking octet 0 checking octet 0 checking octet 0 Mask is 1 Checking mask 0.0.0.64 octets array is 0 0 0 64 checking octet 0 checking octet 0 checking octet 0 checking octet 64 Unequal: oc is 64, flagvalue is 0 Mask is 0 Checking mask 0.0.0.63 octets array is 0 0 0 63 checking octet 0 Unequal: oc is 0, flagvalue is 255 checking octet 0 Unequal: oc is 0, flagvalue is 255 checking octet 0 Unequal: oc is 0, flagvalue is 255 checking octet 63 Unequal: oc is 63, flagvalue is 255 Mask is 0
Right here: Checking mask 0.0.0.63
octets array is 0 0 0 63
checking octet 0
Unequal: oc is 0, flagvalue is 255

$flagvalue is 255. It should have been initialized to 0 when ValidateMasks() was called, shouldn't it? It appears to be retaining the value from the previous time it was called. Am I overlooking something obvious or do I really not understand Perl initialization and scope?

In reply to Variable initialization / reinitialization by Anonymous Monk

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.