Perl Programming Page 5 - Scalars: Building a Currency Converter |
The die() function is how we handle severe errors in Perl. It takes a character string argument and prints it to standard error output (this normally prints to the screen like standard output does). If the argument string does not end in newline, the \n character, die() automatically appends to the output string the name of the Perl program and the line number of the program where thedie()was executed; this is very helpful—it tells us right where the error took place. Thendie()cleans up the program and exits with a non-0 exit status. Therefore,die()is a permanent solution—the program terminates: die "there was an error"; Here is an example of usingdie(): #!/usr/bin/perl -w use strict; print "please enter a string to pass to die: "; die($string); Executing this code would resemble $ perl die.pl Notice that the name of the script and the line number is automatically added to the output ofdie()because the argument todie()did not end in the newline character. Also notice that the lastprint()is not executed because the program terminated whendie()executed. Summary Perl’s basic data type is a scalar. A scalar can be either an integer, floating point number, or string. Perl converts between these three automatically when necessary. Double- and single-quoted strings differ in the way they process the text inside them. Single-quoted strings do little to no processing at all, whereas double-quoted strings interpolate escape sequences and variables. We can operate on these scalars in a number of ways—ordinary arithmetic, bitwise arithmetic, string manipulation, and logical comparison. We can also combine logical comparisons with Boolean operators. These operators vary in precedence, which is to say that some take effect before others, and as a result we must use parentheses to enforce the precedence we want. Scalar variables are a way of storing scalars so that we can get at them and change their values. Scalar variable names begin with a dollar sign ($) and are followed by one or more alphanumeric characters or underscores. There are two types of variables—lexical and global. Globals exist all the way through the program, and so can be troublesome if we don’t keep very good track of where they are being used. Lexicals have a life span of the current block, and so we can use them safely without worrying about clobbering similarly named variables somewhere else in the program. <STDIN>reads in from standard input, which is normally the user’s keyboard. We can store this input in a variable and then operate upon it, making our programs more flexible.<STDIN>reads up to and including the newline character, and we normallychomp() off the newline. Two ways to terminate our programs are by usingexit()anddie().die()is helpful because it prints its argument, and if that argument does not end in\n, it magically adds the script name and line number to the output which helps us locate the error. Exercises
blog comments powered by Disqus |
|
|
|
|
|
|
|