Perl 101 - The Basics of Perl Programming

Click here to load reader

  • date post

    11-May-2015
  • Category

    Education

  • view

    9.957
  • download

    2

Embed Size (px)

Transcript of Perl 101 - The Basics of Perl Programming

  • 1.Perl -101

2. Objectives

  • To introduce Perl Scripting Language
  • To introduce the concept of regular expressions and pattern matching
  • To introduce various file operations in Perl

Linux User Group at San Jose State University 3. Agenda

  • Part 1 (1:30PM to 2:45PM)
  • Introduction to Perl
  • Types of Variables
  • I/O Functions
  • Operators
  • String and formatting strings
  • Selectional, iterational and miscellaneous control statements
  • Data Structures
  • Arrays and various operations on arrays
  • Hashes and various operations on hashes
  • Functions:
  • Subroutines
  • Command line arguments

Linux User Group at San Jose State UniversityPart 2 (2:50PM to 3:45PM) Regular Expressions: Regular Expressions Meta characters Character classes Pattern matching File IO: Reading, Writing text files Various file I/O modesMisc:Installing modules Questions and Answers 4. What is PERL and where PERL is being used?

  • PERL is scripting language
  • PERL is mostly used for extracting information from text files and generating various reports based on that
  • PERL is also used for developing web applications using CGI standards
  • It is also used for developing scripts of automated testing, system administration etc.
  • It is one of the most popular scripting language

Linux User Group at San Jose State University 5. Perl Introduction

  • PERL Practical Extraction and Report Language
    • Developed by Larry Wall in 1987
    • Originally designed for reading text files and preparing reports based on that information
    • Combines features of C ,sed, awk and sh
    • Gives unlimited data size
    • Recursion of unlimited depth
    • Sophisticated pattern matching techniques
    • Used for web programming
    • Widely used for system administration
    • Wide range of third party modules

Linux User Group at San Jose State University 6. The PERL Interpreter

  • Perl Interpreter
    • Converts the scripts in to a parse tree and executes immediately
    • Internally Perl maintains a byte code for execution
    • Known asinterpreter/ compiler

Linux User Group at San Jose State University 7. PERL - Variables

  • Scalar Variables
    • Simple variables containing an element, either a number or a string
    • Defined by $ symbol
  • Array
    • List of scalar variables
    • Defined by @ symbol
  • Hashes
    • Similar to arrays
    • Each item is identified by a key value
    • Defined by % symbol

Linux User Group at San Jose State University 8. PERL Scalar Variables

    • Defined by $ Symbol
    • Stores either a number or a String
    • No declaration required, creation is done when a variable is referred first in the program,knownas auto vivification
    • Strings and Numbers are interchangeable
    • Variable names are case sensitive
    • Variable name can have alphabets, numbers and underscore (_)
      • Should not start with a number
      • Variables starting with_ is special

Linux User Group at San Jose State University 9. PERL - Lists

    • Collection of scalar data, Arrays and Hashes
    • Represents a logical group of items
    • Scalar data inside ( and ) separated using , represents a list
    • A set of operators works on list called as list operator
      • E.g. print
      • print (1,2,3) displays 123

Linux User Group at San Jose State University 10. PERL Display Functions

  • print
    • Unformatted output statement which can print a string, Number or a List
    • Default outputs to STDOUT, can be redirected to STDERR or a File
    • Returns true if successful else false [1 represents true]
      • print List
      • print FILEHANDLEList
  • printf
    • Formatted output statement
    • By defaults outputs to STDOUT. Can be redirected to STDERR or a File
    • printf Format, List
    • printf FILEHANDLE, Format, List
  • warn
    • Outputs a message on the STDERR stream (unbuffered)
    • Used to trace error when STDOUT is redirected
    • Program is neither terminated nor an exception is thrown
      • warn Message

Linux User Group at San Jose State University 11. PERL - Operators

  • Assignment Operator
  • Auto increment and decrement operators
  • Additive operators
  • Multiplicative operators
  • Exponentiation Operator
  • Relational Operators
  • Logical Operators
  • Range Operator
  • Binding Operator
  • Arrow operator
  • File I/O Operator

Linux User Group at San Jose State University 12. PERL Operators contd

  • Assignment Operator (=)
  • Auto increment and decrement operator (++,--)
  • Additive Operator (+, -, .)
      • Addition & Subtraction operator
      • Concatenation Operator (.)
  • Multiplicative Operators
    • Multiplication & Division
    • Repetition Operator (x)
    • Exponential Operator

Linux User Group at San Jose State University 13. PERL operators contd

  • Relational Operators
    • Operating on Numeric data
      • =, ==, !=,
    • Operating on String data
      • lt less thanE.g.($Myname lt $YourName)
      • gt greater than
      • le less than or equal to
      • ge greater than or equal to
      • eq equal to
      • ne not equal to
      • cmp compare (returns -1, 0 , 1)
    • Logical Operator
      • AND-&&or and
      • OR-||or or
      • NOT-not

Linux User Group at San Jose State University 14. PERL Operators contd

  • Range Operator (..)
      • Returns a list of sequence of values
        • print (1..50);
        • print (a..z);
  • Bind Operator (=~, !~)
      • Used for binding scalar expressions to a pattern match
  • Arrow Operator (->)
      • Used to retrieve elements from a reference
  • File Operator ()
      • Used for File I/O Operations
      • represents
  • Note: Bind, Arrow and File operators will be explained later

Linux User Group at San Jose State University 15. PERL Standard Input

  • Input operator
    • or
    • Read input from the user including from the user
    • chomp operator can be used for removing the character
    • By default input is stored in a default variable $_
  • chomp
    • Usually used to discard the character at the end of input string