Scalar-Conversion Functions

There are many functions in Perl that convert scalar values from one form to another and functions that deal with variables that have not had values defined for them.

chop it removes the last character from a scalar value or a list
chomp chomp removes the last character/s only if it matches the system variable $/, you can use chomp on a scalar value or a list
crypt this function encrypts a string using the NBS Data Encryption Standard (DES) algorithm, the algorithm uses a salt key to make it more differcult to decode, the salt key is 2 characters in length, these two characters can be any leter or digit or one of the . and / characters.
hex this function assumes that a character string is a number written in hexadecimal format and converts it into a decimal number.
int this function turns a floating point number into a integer by remove everything after the decimal point
oct this function assumes that a character string is a number written in octal format and converts it into a decimal number.
ord and chr ord converts a single character to its numeric ASCII equivalent
chr converts a number to its ASCII character equivalent
scalar The scalar function enables you to specify the scalar meaning in an array context.
vec enables you to treat a scalar value as a collection of chunks, with each chunck consisting of a specified number of bits, this collection is known as a vector. Each call to vec accesses a particular chunk of bits in the vector (known as a bit vector).
define the define functions checks to see if a variable has been
undef undefines a scalar variable, array element or an entire array, basically means that it appears that the variable, element or array has never been assigned.
Examples
chop

$string = "hello World!";
chop ($string);                       # string is now "Hello World", removed the bang (!)
                                      # chop returns the last character chopped in this case the !

@list = qw ( one two three );
chop (@list)                          # list is now "on","tw","thre"
                                      # chop returns the last character chopped in this case e

Note: chop is normally used to remove the newline character when requesting user input from <STDIN>

chomp $string = "hello";
@list = qw( one two three );

$/ = "o";                             # remove the last character only if it's a 'o'
chomp $string;

$/ = "e";                             # remove the last character only if it's a 'e'
chomp @list;

print $string . "\n";
print "@list";                        # notice only one and three get chomped because they have a 'e'
                                      # as the last character

Note: you can chomp multiple characters as well

crypt $salt_key = "3h";                     # the salt key is 2 characters and can be any digit, character or
                                      # a dot (.) or forward slash (/)

print "Enter something to encrypt: ";

$encrypt = <STDIN>;

print "\n Your text encrypted: " . crypt($encrypt, $salt_key);   

Note: DES uses a salt key to change the DES algorithm to make it more differcult to decode.

hex $hex = "ffff";

print hex($hex);                     # prints out 65535

int $float = 34.56;

print int($float);                   # prints out 34

oct $octal = 177;

print oct($octal);                   # prints out 127

ord and chr $char = 'V';

print ord($char) . "\n";

print chr(ord($char));               # using ord to obtain the numeric ASCII of V (86)

scalar @array = qw (a b c);

@lengtharray = scalar(@array);

print "@lengtharray" . "\n";         # number of array elements of @array (3), is converted into a one
                                     # element list and assigned to @lengtharray

$| = 1;

print "Enter a string: \n";
@line = scalar (<STDIN>);            # specifying scalar with <STDIN> ensures that only one lin is read
                                     # from STDIN

print "@line";

vec # retval = vex (vector, index, bits)
$vector = pack ("B*", "11010011");

$val1 = vec ($vector, 0, 4);         # start at right get 0011 (4 bits) = 3 in decimal
$val2 = vec ($vector, 1, 4);         # start at left get 1101 (4 bits) = 13 in decimal

print ("high-to-low order values: $val1 and $val2");

Note: specifying 0 means start at the right end, 1 means start at the left end

define $array[1] = "hello";

if (defined ($array[0])) {
   print "element 0 in array is defined\n";
} else {
   print "element 0 in array is NOT defined\n";
}

if (defined ($array[1])) {
   print "element 1 in array is defined\n";
} else {
   print "element 1 in array is NOT defined\n";
}

undef undef ($myvar);
undef ($array[2]);
undef (@array);

Pack Function

The pack function enables you to take a list or the contents of an array variable and convert (pack) it into a scalar value in a format that can be stored in actual machine memory or used in programming languages such as C.

You use a packformat to determine how the list is to be packed, there are many packformat options

a ASCII character string padded with null characters
A ASCII character string padded with spaces
b String of bits, lowest first
B String of bits, highest first
c A signed character (range usually -128 to 127)
C An unsigned character (usually 8 bits)
d A double-precision floating-point number
f A single-precision floating-point number
h Hexadecimal string, lowest digit first
H Hexadecimal string, highest digit first
i A signed integer
I An unsigned integer
l A signed long interger
L A unsigned long interger
n A short integer in network order
N A long integer in network order
p A pointer to a string
s A signed short interger
S A unsigned short interger
u Convert to uuencode format
v A short integer in VAX (little-endian) order
V A long integer in VAX order
x A null byte
X Indicates "go back one byte"
@ Fill with nulls (ASCII 0)

Here are some examples

Example One

$integer = pack("i", 171)

$integer = pack("i*", 14, 26, 11, 241);

Note: takes number 171 converts it into the format used to store integers on your machine and returns the converted integer in $integer. This converted integer can now be written out to a file or passed to a program using system or exec functions

Example Two

$mystring = pack("a6", "test");

Note: This creates a string of 6 characters (4 for the word test and 2 null characters for padding)

Example Three

$mystring = pack("a @6 a", "test", "test2");

Note: This integer indicates the number of bytes the string must contain at this point. here the test string is convert to ASCII format, because this string is only 4 characters long, and the pack format @6 specifies that it should be 6 characters two null characters are added to the string before test2 is packed.

Pack Function and C data types

The most frequent use of pack is to create data that can be used by C programs

Pack and C data types $Cstring = pack ("ax", $mystring);         # a = ASCII character string padded with null characters
                                           # x = a null byte

Note: pack converts $mystring into an ASCII string, and the x character appends a null character to the end of the string. This format - a string followed by null - is how C stores strings.

Unpack Function

The unpack function reverses the operation performed by pack. It takes the value stored in machine format and converts it to a list of values understood by Perl

unpack $mystring2 = unpack("a6", $mystring);

print $mystring2;

unpack and skipp characters

$skipnum = unpack("@4i", $mystring);

Note: the @4 means skip 4 bytes then unpack

Unpack Function and uuencode

The unpack function enables you to decode files that have been encoded by the uuencode encoding program.

decode a uuencoded string $decoded = unpack("u", $uuencoded_line);