
Perl Data Types and Variables Data, variables, expressions, and much more Copyright 20062009 Stewart Weiss Data types in Perl Perl is unlike most high-level languages in that it does not make a formal distinction between numeric data and character data, nor between whole numbers and numbers with fractional parts. Most modern languages invented before Perl ask you to declare in advance whether a variable will store a character, a whole number, a floating point number, or something else. Not Perl. 2 CSci 132 Practical UNIX with Perl Typelessness in Perl To illustrate, if we declare a variable named $anything using the statement my $anything; then all of the following assignment statements are valid: $anything = "Now I am a string"; $anything = 10; $anything = 3.141592; In short, in Perl, variables are untyped. 3 CSci 132 Practical UNIX with Perl Three data classes However, Perl does distinguish the class of data, i.e., whether it is primitive or structured. Scalars are primitive and lists and hashes are structured: scalar data a single data item list data a sequence or ordered list of scalars hash data an unordered collection of (key,value) pairs Scalars may be numbers such as 12 or 44.3 or strings like "the swarthy toads did gyre and gimble in the wabe". There are other kinds of scalars as well, as you will now see. 4 CSci 132 Practical UNIX with Perl Literals A literal is a value explicitly represented in a program. For example, in print 56; the numeral 56 is a numeric literal, and in print "This is a string literal."; the string "This is a string literal." is a string literal. Some people call literals constants. It is more accurate to call them literals. 5 CSci 132 Practical UNIX with Perl Numeric literals in Perl Integers: -52 6994 6_994 #Note that _ can be used in place of ',' Fixed decimal: 3.1415 -2.635 1.00 Floating point (scientific notation): 7.25e45 # 7.25 x 10^45 -12e-48 # -12.0 x 10^(-48) 1.000E-5 # 1.000 x 10^(-5) Octal: 0377 # octal, starts with 0 Hexadecimal: 0x34Fb # hexadecimal, case insensitive 6 CSci 132 Practical UNIX with Perl Internal representation of numbers Internally, all numbers are stored and computed on as floating point values. Floating-point representation is like scientific notation; a number is stored as a mantissa and an exponent, either of which may be positive or negative or zero. The advantage of floating-point is that it can represent fractions, extremely large magnitudes such as the number of stars in the universe, and extremely small magnitudes such as the distance from a nucleus to an electron in meters. The disadvantage is that not all numbers can be represented accurately. This is not only because of scientific notation, but because of the nature of digital computers. 7 CSci 132 Practical UNIX with Perl Inaccuracy of numbers Not all numbers can be represented exactly in a digital computer, because computers can only represent numbers as sums of powers of 2. Try writing 1/3 as a sum of powers of 2 and you will see it cannot be done. How about 1/5th? Not that either. Not 1/10th either. In fact, most numbers are represented with some small inaccuracy. 8 CSci 132 Practical UNIX with Perl Gaps in numeric representation Imagine that the black rectangles below represent numbers that can be represented in floating point and blue are those that cannot. The rectangle below is like a piece of the number line showing that there are big gaps between representable numbers. The inaccuracies and gaps can lead to large errors in calculations. Numerical Methods is a branch of computer science concerned with methods of computation that do not produce large errors. 9 CSci 132 Practical UNIX with Perl String literals: Double-quoted strings Double-quoted strings can contain special characters, such as: \n, \t, \r, \177 (octal 177) You have already seen that \n is a newline character. \t is the tab. You can see a more complete list of these special characters in the textbook. \c is the prefix for inserting control characters into strings, as in: \cC \cD control-C and control-D 10 CSci 132 Practical UNIX with Perl String literals: Case conversion Other useful special characters are \L, \U, and \E. The pair \Ltext\E converts text to lowercase \Utext\E converts text to UPPERCASE: "\LABc\E" # is abc "\UaBc\E" # is ABC "\L123\E" # is 123 11 CSci 132 Practical UNIX with Perl Backslash escape character If you want to insert a character that has special meaning, you use the backslash to "escape" the special meaning. Since " is a special character, to insert a " into a string, write \" To insert a backslash, use \\. Example: print "Use \\ before \" to put a \" in a string."; This will print: Use \ before " to put a " in a string. 12 CSci 132 Practical UNIX with Perl Variables in double-quoted strings You can put a variable in a double-quoted string. When the string is evaluated, the variable's value will be substituted into the string in the given position. $greeting = "Welcome to my planet"; $count = 40_000_000_000; print "$greeting.\nThere are $count of us here.\n"; will print Welcome to my planet. There are 40000000000 of us here. 13 CSci 132 Practical UNIX with Perl String literals: Single-quoted strings Perl also has single-quoted strings, and they behave differently than double-quoted strings. Within a single quoted string, variables are not substituted and backslashed special characters have no meaning: $number = 100; print '$number';# prints $number print ' '; # prints a blank line 14 CSci 132 Practical UNIX with Perl String literals: single-quoted strings To put ' or \ into a single-quoted string, precede with a \ as in 'don\'t do it!' # don't do it! '\\/\\/' # \/\/ 15 CSci 132 Practical UNIX with Perl Scalar variables Scalar variables can store numeric data, strings, and references. We will get to references later. Although Perl does not require you to declare variables before using them, in this class, you will be required to use the strict pragma, which forces you to declare them first (or use another alternative.) The my declaration declares variables. To declare more than one, put them in parentheses, otherwise only the first will be declared properly: my ($count, $pattern); 16 CSci 132 Practical UNIX with Perl The scoop on my declarations When you use my to declare a variable, as in my $var; you are telling Perl two things: 1. That $var is a name that can be used from that point forward in the innermost enclosing block (pair of curly braces), and 2. that there is storage set aside for $var In this case you are also telling Perl that it is a scalar variable. 17 CSci 132 Practical UNIX with Perl Variables, names, and storage You should picture the relationship between the variable, its name, and its storage like this: $var The declaration creates the empty storage container and the name, and binds (attaches) the name $var to the container. The blue dot represents the connection between the name and the storage in the schematic. 18 CSci 132 Practical UNIX with Perl The assignment operator The assignment operator, =, is used to store a value into a variable, as in: $count = 0; $pi = 3.141592; $title = "A First Perl Program"; The assignment operator is also used for list assignments, as in: ($firstname, $initial, $lastname) = ('Alfred', 'E', 'Newman'); This assigns 'Alfred' to $firstname, 'E' to $initial, and 'Newman' to $lastname. 19 CSci 132 Practical UNIX with Perl Assignment statement semantics The effect of the assignment statement, $var = 100; on the schematic diagram of the name-variable relationship is as follows: $var $var = 100; $var 100 20 CSci 132 Practical UNIX with Perl Rules to remember Reading a value from a variable does not remove the value from the variable. Storing a value in a variable replaces any value that was there before. If you have not assigned a value to a variable, it will have the value 0 or undef or "", depending on the context in which it is used. undef is a special value in Perl that represents the idea of nothingness. 21 CSci 132 Practical UNIX with Perl Input You saw in the first Perl program an example of input: my $response = <STDIN>; declares the variable $response and assigns to it whatever the user enters on the keyboard, up to and including the first newline character. The <STDIN> symbol represents an input operation in Perl. For now, this is the only means you have of obtaining user input in your program. Later you will learn about other options. 22 CSci 132 Practical UNIX with Perl Reading Multiple Values To read multiple values, one way is to write repeated instructions like this: my $x1 = <STDIN>; my $x2 = <STDIN>; my $x3 = <STDIN>; and then enter the values on separate lines (using the ENTER key to separate them). Later you will see how to read a list of values from STDIN using a single instruction. 23 CSci 132 Practical UNIX with Perl Expressions An expression, informally, is either a simple variable or literal or function that returns a value, or a composition of these things built up by applying various kinds of operations such as arithmetic operations and other operations as well. These are examples of legal Perl expressions: 12 + 9 # 21 : 12 + 9 == 21 6 * 7 # 42 : '*' is 'times', 6*7 == 42 $sum/$count # division of $sum by $count 3 ** 2 # 9 : 3 raised to 2nd power $count - 1 # $count minus 1 16 % 10 # 6 : 16 % 10 is remainder of 16 / 10 24 CSci 132 Practical UNIX with Perl More examples Assume that $x == 5, $y == 11, and $z == 3.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages79 Page
-
File Size-