Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Practical Extraction & Report

Language

By: B.T.R Naidu


Email: tiru.naidu@gmail.com
Copyright Amstar Technologies

Copyright Amstar Technologies

PERL
Basics

Copyright Amstar Technologies

Perl Data Types


Three Main Datatypes
Arrays and Hashes )

Scalars,

Scalar
A single number, string or reference.
$society = Redbrick;

Scalars can be subdivided into three


groups
Integers or Floating-point numbers
Strings
References
Copyright Amstar Technologies

Data Types - Scalars


Preface a scalar variables name with a $.
The name we use for a scalar variable
may
contain
letters,
number
and
underscores. However, it must start with
the $ symbol, which stops it from
confliction with reserved words in Perl.
A scalar variables name can be long.
Although
the
length
is
platform
dependent, a variables name can be at
most 255 characters long.
Copyright Amstar Technologies

Data Types - Scalars


To place an integer value we use
$var1=5;

String assignment work much the same way


$str1=Hello World\n;

Number fall into one of two categories, integer, or floatingpoint.


In addition, both types of number can be
represented as a numerical string, which is converted to the
appropriate format when used.
Integers can be specified in several different formats
123
regular decimal integer
0b1101
binary integer
0127
octal integer
0Xabcd
hexadecimal integer
12_345
underscore annotated integer
0Xca_fe_ba_be underscore annotated hexadecimal integer

Copyright Amstar Technologies

Data Types - Scalars


When Perl stores a value as an integer ( as
opposed to a numerical string ), the
maximum size of the integer value is limited
by the maximum size of integers on the
underlying platform. On a 32-bit architecture
this means integers can rage from
0 to 4294967295
(unsigned)
-2147483648 to 2147483647 (signed)

Note: If an integer calculation falls outside


the range that integers can support then
Perl automatically converts the result to a
floating-point format.
Copyright Amstar Technologies

Data Types - Scalars


Perl allows floating-point numbers to be written in one of
two forms, either fixed point or scientific, consisting of a
mantissa with the actual number value and an exponent
representing the power of 10 that the mantissa is multiplied
by
123.456
# fixed point
-1.2345e2
# scientific, lowercase, negative
+1.2345E2
# scientific, uppercase, explicitly positive
Likewise, fractions can be expressed either in fixed-point
notation or as a negative exponent
0.000034
# fixed point
-3.4e-4
# scientific, lowercase, negative
+3.4E-4
# scientific, uppercase, explicitly positive

Copyright Amstar Technologies

Data Types - Scalars


Converting Between Octal, Decimal, And Hexadecimal
To convert a number from hexadecimal to decimal, use the
hex function
print hex 1AB;

# Output: 427

To convert decimal number to a hexadecimal number


represented by a string, we use the Perls sprintf function with
the %x conversions
print sprintf %x, 16;

# Output: 10

To convert form octal to decimal, we use the oct function


print oct 10;

# Output: 8

To convert a decimal number to an octal number represented


by a string, we use the Perls sprintf function, but this time
with %o conversion
print sprintf %o, 16;

# Output: 20

Copyright Amstar Technologies

Data Types - Scalars


Rounding Numbers

To round a number to a specific number of decimal places, we use the


again the sprintf function.
print sprintf %.2f, 3.1415926;

# Output: 3.14

To round a number for four decimal places

$variable1 = sprintf %.4f, 3.1415926;


print $variable1;
# Output: 3.1416

Using Strings in Scalar Variables

Besides numbers, scalar variables can hold strings like this


$variable1=Hello;

We can use the Perls concatenation operator, which is a dot (.) to add
two strings
$var1=Hello ;
$var2=there\n;
print $var1 . $var2;

# Output: Hello there

We can create string values using single or double quotes


$var1=Hello ;
$var2=Hello again.;

Copyright Amstar Technologies

Data Types - Scalars


There is a difference between these two methods.
Perl
will
evaluate
variables
and
certain
expressions when enclosed by double quotes.
Enclosing a string in single quotes makes Perl
treat it as a literal rather than interpreting it.
We can use the escape characters in strings. For
example, to place a double quote in our text, we
can use the \ escape character this way:
print I said, \Hello\.;
# Output: I said, Hello

Copyright Amstar Technologies

10

Data Types - Scalars


The escape characters and there meaning
are
Escape Means
\
\
\t
\n
\u
\l
\U
\L
\Q
\E
\r
\f
\b
\a

Single quote
Double quote
Tab
Newline
Make next character Uppercase
Make next character Lowercase
Make all the following characters Uppercase
Make all the following characters Lowercase
Add a backslash to all following no alphanumeric characters
End of \L, \U, or \Q
Return
Form feed
Backspace
Alarm(Bell)

Copyright Amstar Technologies

11

Data Types - Scalars


Using String Interpolation
When we enclose a string that includes variable name in
double quotes, Perl substitutes the value stored in that
variable into the string.
$text=Hello;
print Perl says: $text!\n;
Hello!

# Output: Perl says:

This process is called interpolation.


However, if we use single quotes, Perl will not perform
interpolation
$text = Hello;
print Perl says: $text!\n;
$text!\n

#Output: Perl says:

This means that we use single quotes when we dont want


Perl to try to evaluate an expression.

Copyright Amstar Technologies

12

Data Types - Scalars


If we want to interpolate a variable as part of another word
and not a word by itself then we use { and } to set off the
name of the variable we want to interpolate as part of a
word
$text=un;
print Dont be ${text}happy.;

# Output: Dont be unhappy

Quotes and Quoting


Single word text strings without quotes are called
barewords. But if we need to use more than one word, its
not a bareword anymore, and just a simple assignment
wont work
$text=Hello;
print $text;
$text=Hello there!;
print $text;

# Output: Hello
# Error: No Good
# Error: Doesnt work

Copyright Amstar Technologies

13

Data Types - Scalars


Besides skipping the quotes, we can also use Perl to add
quotes automatically using the constructs shown bellow
Construct
q//
qq//
qx//
qw//
//
s///
y///

Resorts In

``
()
m//
s///
y///

Interpolated? Stands For


No
Literal
Yes
Literal
Yes
Command
No
Word List
Yes
Pattern Match
Yes
Substitution
No
Translation

print I said, \Hello.\; # Output: I said, Hello.

To avoid too many escaped characters, we can use the qq//


construct to take care of the double quotes in a string
print qq/I said, Hello./;

# Output: I said, Hello.

Copyright Amstar Technologies

14

Data Types
In fact, we dont need to use / and / to enclose
the string in such cases.

We can use nearly any character ( as long as we


use the same character at the beginning and end
of the string )
print qq|I said, Hello.|;

# Output: I said, Hello.

We can even use parentheses, which are usually


used to enclose arguments passed to subroutines
print qq(I said, Hello.);

#Output: I said, Hello.

Copyright Amstar Technologies

15

Copyright Amstar Technologies

16

You might also like