Perl Example #4
Using Simple Perl Functions

About the Program

This program demonstrates the use of several simple functions in Perl. The first function used is "split" which breaks a string into array elements depending upon the string used as a marker for the split process. In the example, entries from the passwd file are selected, and then the fields are separated using the colon (:) as a marker. The delimiter used to mark the string are very flexible, and both the double quote (") and the slash (/) are used in the splitting examples. The opposite of split is join, but that function is not demonstrated.

The next function is "pop" which chops off the highest element from an array, and returns that value. Naturally, there is a push function, but it is not demonstrated. The sort command is shown, however.

The final part of the program shows how to implement the equivalent of a "case statement", since there is no "switch" command in Perl as there is in C or C++ languages. It uses the power of matching and regular expressions in order to achieve the same result.

# Using Simple Perl Functions

print "Enter pattern:";
$pattern = <STDIN>;
Enter pattern: Josh
# Scan through the passwd file. Create an array of lines with search pattern

@people = `cat /etc/passwd | grep $pattern`;
print @people, "\n";
jeddy:8J2fhwdxUqEiM:2626:1999:Joshua Eddy:/home/atlas2/jeddy:/usr/local/bin/bash
jsarfaty:xMD4HK533Jr/w:2702:1998:Josh Sarfaty:/home/atlas2/jsarfaty:/usr/local/bin/bash
jglasser:a7DEi4IKsVK2k:2707:1999:Joshua Glasser:/home/atlas2/jglasser:/usr/local/bin/bash
jgillon:GARzdVZX.8LNY:2826:1999:Josh Gillon:/home/atlas2/jgillon:/usr/local/bin/bash
jblake:aP9RpObogxMN2:2849:2001:Joshua Blake:/home/atlas2/jblake:/usr/local/bin/bash
# This section manipulates the strings grabbed from the passwd file 

for ($i = 0; $i<= $#people; $i++)
  {$_ = $people[$i]; 

   # Use "split" to break the string into separate elements between colons

   @passwd_data[0..6]= split(":");  # Using double quotes for the delimiter 
   # This field contains the full name of the user

   print $passwd_data[4];

   print "\n";

   # Use "split" to break apart first and last names
   @temp = split(/ /, $passwd_data[4]);  # Use the slash for a delimiter
   # Use the function "pop" to pull off the last name
   $lastnames[$j] = pop(@temp);

   #print last name first, then what's left in @temp (first name)

   print $lastnames[$j],", ", @temp , "\n\n"; 

Joshua Eddy
Eddy, Joshua

Josh Sarfaty
Sarfaty, Josh

Joshua Glasser
Glasser, Joshua

Josh Gillon
Gillon, Josh

Joshua Blake
Blake, Joshua
$,="\n ";  # Change the print separator to a carriage return

# Use the "sort" function to sort the array
print "Sorted by last name: ",sort @lastnames;
print "\n";
Sorted by last name:

$,="";    # Reset the print separator to null

# Emulating the "switch" statement

print "Do they like Perl?\n";

while (<>)  # Infinite loop requesting keyboard response
    $answer = "I don't understand. Type 'Q' to quit";
    REPLY:  # Skip other statements at "last REPLY".  Exit loop at "goto".
       # Beginning "y" followed by possibly "es" at end, and ignore case
       /^y(es)?$/i  && do { $answer= "Perl is Kewl!"; last REPLY; };

       # Beginning "n" followed by possible "o", ignore case
       /^no?$/i  &&  do { $answer = "What a shame..."; last REPLY; };

       # The exact word "maybe", but ignore case
       /^maybe$/i  &&  do { $answer = "Let's learn more."; last REPLY; };
       # Beginning "q", or the word "quit", ignoring case.  Jump out of "while"
       /^q(uit)?/i  &&  do { $answer = "QUIT"; print "Thanks!\n";goto EXIT; };

     print $answer, "\n";
     print "But, do they like Perl?\n";


print "Going on....\n";

Do they like Perl? yes
Perl is Kewl!

But, do they like Perl?   Y
Perl is Kewl!

But, do they like Perl?   nO
What a shame...

But, do they like Perl?   maybe
Let's learn more.

But, do they like Perl?   sure
I don't understand. Type 'Q' to quit

But, do they like Perl?   q
Going on....

The actual program:

The output: ex4.out