next up previous contents
Next: Invoking perl Up: Introduction Previous: How is Perl like   Contents


How is perl different from other languages

Perhaps the main difference between perl and other languages is the flexibility of perl's syntax. As you're studying perl, you should always remember the perl motto: ``There's more than one way to do it.'' Because of the richness of the language, you are encouraged to develop your own style. There really is no right or wrong way to program in perl - the focus is on getting the job done, not obeying a list of programming rules or ``good programming practices''.

Much of this flexibility is achieved through perl's non-standard language design. For example, every variable in perl must be preceded by a symbol informing perl as to what kind of object is being stored in that variable. This allows perl to recognize names without the prepended symbol (``barewords'' in perl lingo) as being function calls or character strings. So while perl will most certainly recognize an expression like:

     $label = join(' ',@parts);
as a call to the function named join, you could just as easily write
     $label = join ' ', @parts;
Unlike most languages, perl doesn't need the parentheses to distinguish functions from variables, so parentheses are optional when calling a function in perl.

Another area where perl differs from other languages is the way that it reads data from a file or the keyboard. Perl uses angle brackets (< and >) as an ``operator'' to read a line from an already-opened file. Thus, to read a single line typed from the keyboard (known as ``standard input'' in the UNIX world), you could use a construct like:

     $response = <STDIN>;
In fact, perl makes it even easier than that. When the angle brackets are empty (<>), perl automatically reads a line from the standard input, so the previous line of code is equivalent to
     $response = <>;
One of the most unusual, but also very useful, constructs in perl involves the use of empty angle brackets inside a while loop:
     while(<>){
         if(length > 80){
             print;
         }
     }
This program fragment reads each line of input, and prints any line which contains more than 80 characters. As the previous paragraph pointed out, perl can recognize length and print as functions, even without parentheses, but here they're being called with no arguments as well! The secret lies in the magic behind the angle bracket while loop. When this construct is used, a special variable called $_ is created to hold the contents of the currently read line. In perl, when a function is called without any arguments, this special variable is passed to the function. (There are many situations in perl where $_ is used as a default variable.) So the line
     if(length > 80){
is equivalent to the line
     if(length($_) > 80){
although most perl programmers would not specify this argument explicitly.

Opening a file uses a non-standard syntax as well. You pass an argument (a ``filehandle'' in perl lingo) to the open function, along with the name of the file you wish to open, and additional information regarding whether you wish to read from or write to the file. Since the open function returns a zero if it's successful, and a non-zero value if it's not, a very commonly encountered construct in perl is the following:

    open(FILE,"<myfile") || die "Couldn't open myfile";
The die function prints its argument and then gracefully exits. After a successful call to open, the filehandle (in this case FILE) can be used inside of angle brackets to read the contents of the file line by line. A similar function, warn, prints a message, but does not terminate the program.


next up previous contents
Next: Invoking perl Up: Introduction Previous: How is Perl like   Contents
Phil Spector 2002-10-18