505 507




Linux Unleashed, Third Edition:Perl





-->















Previous
Table of Contents
Next




Handling Data in Perl
At the simplest level, Perl deals with two different kinds of data: numbers and strings. This section describes how Perl handles these two forms of data and also how each can be used in Perl programs. Let’s start things off with a description of how variables are used in Perl programs.

Variables
Variables in Perl are similar in function and syntax to variables found in the shell programming languages. The biggest difference is that in the shell languages you get the value of a variable by preceding it with a dollar sign, and you assign a value to a variable by using the variable name without a dollar sign in front of it.

In Perl, you always use a dollar sign in front of the variable name no matter whether you are assigning a value to the variable or getting the value of the variable. The following command assigns the value hello to the variable named greeting.


$greeting=”hello”;


In the shell languages, this command is written as follows:



greeting=”hello”


or



set greeting = “hello” (when using tcsh)


Spaces on either side of the equal sign don’t matter to Perl (unlike the shell programming language) so you can use whichever method is most comfortable and familiar to you. Another difference is that you can perform integer operations directly on a variable without using another command like expr. For example, the command


$a = 1 + 2;


assigns the value of 3 to the variable $a. If you enter the following two commands


$a = 1 + 2;
$b = 3 * $a;


the $b variable is assigned a value of 9.
This is the expected result, but how does Perl know that $a is a numeric value and not a string? The answer is that it does not know. Whenever a variable or literal is used as an argument to a numeric operator, it is converted to a number. In this case, there is no problem because the value stored in $a is a number. There would be a problem, however, if the value stored in $a was a string that contained a character such as b because this type of string cannot be converted to a numeric value. Perl handles this situation in a not so elegant way. If the value is not convertible to a number, it simply makes it equal to 0. By default, Perl does not even warn you about this conversion. If you invoke Perl with a -w command-line option, though, it will warn you when this type of conversion takes place.
Numbers
Perl stores all numeric data as floating-point values. You can use integers in your Perl programs, but Perl treats them all as floating-point numbers.

Perl provides a set of operators that can be used to perform comparisons between two numeric values and for performing the standard mathematical operations on numbers. Table 28.1 lists some of the operators that Perl provides for numeric data.
Table 28.1. Numeric operators.



Operator
Description



$a = op1 + op2
Stores the value of op1 plus op2 into $a

$a = op1 - op2
Stores the value of op1 minus op2 into $a

$a = op1 * op2
Stores the value of op1 times op2 into $a

$a = op1 / op2
Stores the value of op1 divided by op2 into $a

$a = op1 ** op2
Stores the value of op1 to the power of op2 into $a

$a = op1 % op2
Stores the value of op1 modulus op2 into $a

op1 == op2
Returns True if op1 is equal to op2

op1 != op2
Returns True if op1 is not equal to op2

op1 < op2
Returns True if op1 is less than op2

op1 > op2
Returns True if op1 is greater than op2

op1 <= op2
Returns True if op1 is less than or equal to op2

op1 >= op2
Returns True if op1 is greater than or equal to op2







Previous
Table of Contents
Next














Wyszukiwarka

Podobne podstrony:
505 (3)
505 praktycznych skryptow dla webmastera
507 tabele
instrukcja obslugi dla zestawu s uchawkowego Nokia BH 505 PL
507,24,artykul
507 POL ED02 2001
507 509
503 505
507 512
505 (2)
A3 507 515 lab 1
505 508
13 (507)
demo cgi 505

więcej podobnych podstron