Databases Reference
In-Depth Information
$answer=(1+2)-(3*4/5);
but using parentheses, you could specify that it should be evaluated as:
$answer=1+((2-3)*4)/5;
We recommend that you make liberal use of parentheses to keep your code readable
and to avoid ambiguity.
More on Variables
Variables can be used to store things other than numbers. In Example 16-1, we use
variables to store and display text and numbers.
Example 16-1. Perl script to add several variables and display the totals
#!/usr/bin/perl
use strict;
# Declare variables to store animal names, and assign values to them
my $AnimalNameOne="cats";
my $AnimalNameTwo="dogs";
my $AnimalNameThree="fish";
# Declare variables to store animal counts, and assign values to them
my $AnimalCountOne=3;
my $AnimalCountTwo=7;
my $AnimalCountThree=4;
# Calculate the sum of the animal counts
my $Total=$AnimalCountOne+$AnimalCountTwo+$AnimalCountThree;
# Display the counts and total
print "Pet roll call:\n".
"===========\n".
"$AnimalNameOne:\t$AnimalCountOne\n".
"$AnimalNameTwo:\t$AnimalCountTwo\n".
"$AnimalNameThree:\t$AnimalCountThree\n".
"===========\n".
"Total:\t$Total\n";
In this program, we store animal names and counts in variables, and place the total
count into the $Total variable. Save this program as animals.pl and run it; you'll see the
following output:
Pet roll call:
===========
cats: 3
dogs: 7
fish: 4
===========
Total: 14
 
Search WWH ::




Custom Search