Top Banner
7/23/2019 Perl - m02 - Basics http://slidepdf.com/reader/full/perl-m02-basics 1/16 Copyright © Amstar Technologies 1 P P ractical ractical E E xtraction & xtraction & R R eport eport L L anguage anguage By: B.T.R Naidu B.T.R Naidu Email: [email protected] Copyright © Amstar Technologies
16

Perl - m02 - Basics

Feb 18, 2018

Download

Documents

kharels
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 1/16

Copyright © Amstar Technologies 1

PPracticalractical EExtraction &xtraction & RReporteportLLanguageanguage

By:B.T.R NaiduB.T.R Naidu

Email: [email protected]

Copyright © Amstar Technologies

Page 2: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 2/16

Copyright © Amstar Technologies 2

PERL

Basics

Page 3: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 3/16

Copyright © Amstar Technologies 3

Perl Data Types

Three Main Datatypes ( Scalars,Arrays and Hashes )

Scalar• A single number, string or reference.

• $society = “Redbrick”;

Scalars can be subdivided into threegroups

• Integers or Floating-point numbers• Strings

• References

Page 4: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 4/16

Copyright © Amstar Technologies 4

Data Types Scalars

Preface a scalar variable’s name with a $.

The name we use for a scalar variablemay contain letters, number andunderscores. However, it must start withthe $ symbol, which stops it from

confliction with reserved words in Perl.

A scalar variable’s name can be long.Although the length is platformdependent, a variable’s name can be atmost 255 characters long.

Page 5: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 5/16

Copyright © Amstar Technologies 5

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 floating-point. In addition, both types of number can berepresented as a numerical string, which is converted to theappropriate format when used.

Integers can be specified in several different formats123 regular decimal integer0b1101 binary integer

0127 octal integer0Xabcd hexadecimal integer12_345 underscore annotated integer0Xca_fe_ba_be underscore annotated hexadecimal integer

Page 6: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 6/16

Copyright © Amstar Technologies 6

Data Types Scalars

When Perl stores a value as an integer ( asopposed to a numerical string ), themaximum size of the integer value is limitedby the maximum size of integers on the

underlying platform. On a 32-bit architecturethis means integers can rage from

0 to 4294967295 (unsigned)-2147483648 to 2147483647 (signed)

Note: If an integer calculation falls outsidethe range that integers can support thenPerl automatically converts the result to afloating-point format.

Page 7: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 7/16

Copyright © Amstar Technologies 7

Data Types Scalars

Perl allows floating-point numbers to be written in one oftwo forms, either fixed point or scientific, consisting of amantissa with the actual number value and an exponentrepresenting the power of 10 that the mantissa is multipliedby

123.456 # fixed point-1.2345e2 # scientific, lowercase, negative+1.2345E2 # scientific, uppercase, explicitly positive

Likewise, fractions can be expressed either in fixed-pointnotation or as a negative exponent

0.000034 # fixed point

-3.4e-4 # scientific, lowercase, negative+3.4E-4 # scientific, uppercase, explicitly positive

Page 8: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 8/16

Copyright © Amstar Technologies 8

Data Types Scalars

Converting Between Octal, Decimal, And Hexadecimal

To convert a number from hexadecimal to decimal, use thehex function

• print hex “1AB”; # Output: 427

To convert decimal number to a hexadecimal numberrepresented by a string, we use the Perl’s sprintf function withthe %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 Perl’s sprintf function, but this timewith %o conversion• print sprintf “%o”, 16; # Output: 20

Page 9: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 9/16

Copyright © Amstar Technologies 9

Data Types Scalars

Rounding NumbersTo 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 VariablesBesides numbers, scalar variables can hold strings like this

• $variable1=”Hello”;

We can use the Perl’s concatenation operator, which is a dot (.) to addtwo 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.’;

Page 10: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 10/16

Copyright © Amstar Technologies 10

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 Perltreat it as a literal rather than interpreting it.

We can use the escape characters in strings. Forexample, to place a double quote in our text, wecan use the \” escape character this way:

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

Page 11: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 11/16

Copyright © Amstar Technologies 11

Data Types Scalars

The escape characters and there meaningareEscape Means\’ Single quote

\” Double quote\t Tab\n Newline\u Make next character Uppercase\l Make next character Lowercase

\U Make all the following characters Uppercase\L Make all the following characters Lowercase\Q Add a backslash to all following no alphanumeric characters\E End of \L, \U, or \Q\r Return

\f Form feed\b Backspace\a Alarm(Bell)

Page 12: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 12/16

Copyright © Amstar Technologies 12

Data Types Scalars

Using String Interpolation When we enclose a string that includes variable name in

double quotes, Perl substitutes the value stored in thatvariable into the string.

$text=”Hello”;

print “Perl says: $text!\n”; # Output: Perl says:Hello!

This process is called interpolation.

However, if we use single quotes, Perl will not performinterpolation$text = “Hello”;print ‘Perl says: $text!\n’; #Output: ‘Perl says:$text!\n’ 

This means that we use single quotes when we don’t wantPerl to try to evaluate an expression.

Page 13: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 13/16

Copyright © Amstar Technologies 13

Data Types Scalars

If we want to interpolate a variable as part of another wordand not a word by itself then we use { and } to set off thename of the variable we want to interpolate as part of aword

$text=”un”;

print “Don’t be ${text}happy.”; # Output: Don’t be unhappy

Quotes and Quoting Single word text strings without quotes are called

barewords. But if we need to use more than one word, it’snot a bareword anymore, and just a simple assignmentwon’t work

$text=Hello;print $text; # Output: Hello

$text=Hello there!; # Error: No Goodprint $text; # Error: Doesn’t work

Page 14: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 14/16

Copyright © Amstar Technologies 14

Data Types Scalars

Besides skipping the quotes, we can also use Perl to addquotes automatically using the constructs shown bellow

Construct Resorts In Interpolated? Stands For

q// ‘ ‘ No Literalqq// “ “ Yes Literalqx// ` ` Yes Commandqw// ( ) No Word List

 // m// Yes Pattern Matchs/// s/// Yes Substitutiony/// y/// 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”.

Page 15: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 15/16

Copyright © Amstar Technologies 15

Data Types

In fact, we don’t need to use / and / to enclosethe string in such cases.

We can use nearly any character ( as long as weuse the same character at the beginning and endof the string )

print qq|I said, “Hello”.|; # Output: I said, “Hello”.

We can even use parentheses, which are usuallyused to enclose arguments passed to subroutinesprint qq(I said, “Hello”.); #Output: I said, “Hello”.

Page 16: Perl - m02 - Basics

7/23/2019 Perl - m02 - Basics

http://slidepdf.com/reader/full/perl-m02-basics 16/16

Copyright © Amstar Technologies 16