- KickAss Perl Tips -

  

 

 

 

 

Example Subroutine #2

 

Changing our program to place the subroutine in a separate file is very easy. First step is to cut the subroutine from the original source file. Second step then is to create a new file named 'flagsToByte.pl' and place the subroutine in this file.

Finally, we add a new statement to the beginning of our source code file to direct the Perl interpreter to find and use the new file.

require 'flagsToByte.pl'; # Retrieve the subroutine from another file.

$AHZ1 = flagsToByte(4,@Zonelist); # populate it with data retrieved from HAL

The subroutine then operates exactly as if it were already incorporated into the source file.

#######################################################

sub flagsToByte {

# Takes a list of arrays which have a text boolean "TRUE/FALSE" as element[2]

# Plus an integer pointer into that array.

#

# Returns a BINARY byte where each bit reflects the status of the booleans in the

#######################################################

my($ptr,@incomingZoneList)=@_;

Note the use of 'MY' to designate local variables. Variables designated by 'My' are entirely private to the subroutine.

local @x,$z,$c,$byt,$ptr2,$ctr;

Note the use of 'local' to designate local variables. Variables designated by 'local' are private to the subroutine, but are shared with subroutines called by this subroutine. A subroutine called by this subroutine sees these 'local' variables exactly the same as it would similarly named global variables.

(intervening code deleted for simplicity. See actual V572.pl for actual source code.)

$byt = pack("C*",@byt); # Convert ASCII back to Binary byte.

return $byt; # We're done. Return the modified byte.

}

1;