Date post: | 19-Mar-2017 |
Category: |
Education |
Upload: | ram-n-sangwan |
View: | 101 times |
Download: | 2 times |
Session 2 Variables in PerlRA M N SA N GWA N
WWW. R N SA NGWA N.COM
1
Agenda
• Variables in Perl
• Scalars
• Scalars: Declaration
• Scalars: Numbers
• Usage of scalars
• List : one-dimensional array
• Data Types
• Numbers and Strings
• User Input
• Special Variables
2
Variables in Perl
• You DO NOT have to declare variables in Perl.
◦ Unless you force it to force you to declare variables.
• Three basic types of variables:
◦ Scalar variables : $a, $b, $c
◦ Array variables : @array
◦ Hash variables : %hash
• Variable type (int, char, ...) is decided at run time$a = 5; # now an integer
$a = “perl”; # now a string
Scalars
• Scalar variables store “single values”.
• This “single value” can be any of the following types:◦ int, float, double, char, string, or references.
• In Perl these various types fall into one of threecategories:◦ numbers, strings, and references.
• When you first use (declare) a variable use the mykeyword to indicate the variable’s scope◦ Not necessary but good programming practice
Scalars: Declaration
• All scalar variables begin with a $.◦ $ is NOT part of the variable!
$num = 14;
$fullname = “John H. Smith”;
◦ Variable Names are Case Sensitive
$Program_Version = 1.0;
• Next character is either a letter or ‘_’.
• Remaining characters are a mix of letters, numbers,and ‘_’.
• Correct: $x, $myvar123, $avg_height
• Wrong: $2values, $avg-height, $good$day.
Scalars: Numbers
• Unlike C/C++, all numeric data in Perl are stored asdouble precision floating points.◦ Ex: 37 3.7 .37 37. 3E7 3e7 3E-7
◦ Hex: 0x2aff, 0xAA3, 0XFFF
◦ Octal: 077 0276
◦ Underscore: 3_212_567 → 3,212,567.
• Again, you do not need to specify the type.
Usage of scalars
• All of the following statements are valid
print ("pi is equal to: $pi\n");
print "pi is still equal to: ", $pi, "\n";$c = $a + $b
• Important! A scalar variable can be "used" before it isfirst assigned a value◦ result depends on context
◦ either a blank string ("") or a zero (0)
◦ this is a source bugs
◦ if variable name is mispelled — what should be theresult?
◦ do not let yourself get caught by this – use the "-w"flag in the bang path:#!/public/bin/perl -w
7
List : one-dimensional array
• Declaration/Usage
@memory = (16, 32, 48, 64);
@people = (“Alice”, “Alex”, “Albert”);
• First element numbered 0
• Single elements are scalar: $names[0] = “Fred”;
• Slices are ranges of elements
@guys = @people[1..2];
• How big is my list?
print “Number of people: “, scalar @people, “ \n”;
Data Types
• Integer25 750000 1_000_000_000
8#100 16#FFFF0000
• Floating Point1.25 50.0 6.02e23 -1.6E-8
• String‘hi there’ “hi there, $name” qq(tin can)
print “Text Utility, version $ver\n”;
• Boolean0 0.0 “” "0" represent False
all other values represent True
10
Numbers and Strings
• Numbers and Strings are Interchangeable
• If a scalar variable looks like a number and Perl needsa number, it will use it as a number$a = 4; # a number
print $a + 18; # prints 22
$b = “50”; # looks like a string, but ...
print $b – 10; # will print 40!
User Input
• Entering a string and printing it:$text = <STDIN>;print "You just entered $text";
11
• $[
• Determines which index in a list is the first, the default is 0.
my @mylist = (Larry, Moe, Curly);
print $mylist[1];
$[ = 1;
print $mylist[1];
Moe
Larry
Special Variables
• $& Entire pattern from most recent match
• $/ Input record separator, default is \nundef $/;
open(FILE,<input.txt);
$buffer=<INFILE>;
$buffer contains the entire file
Special Variables
• $. Current line number
• $, Default separator used when a list is printed,default is ‘’.$,=‘ ‘; will add a space between each item if you print out a list.
• $\ Default record separator, default is ‘’
$\ = “\n”; will add a blank line after each printstatement.
Special Variables
• $^T time the perl program was executed
• $| autoflush
• $$ process ID number for Perl
• $0 name of perl script executed
• %ENV hash containing environmental variables.
Special Variables
@ARGV is a list that old all the arguments passed tothe Perl script.
@_ is a list of all the variables passed to thecurrent subroutine
Special Variables
$_ is a variable that hold the current topic.
e.g.;
while (<FILE1>){
print “line $. $_”
}
Special Variables
This is the current line number
This is the current line being
processed in FILE1
Thankyou
18