SlideShare une entreprise Scribd logo
1  sur  22
Télécharger pour lire hors ligne
Practical Approach to PERL
           (Day2)

    Rakesh Mukundan
String Comparison

    We want to check if a string contains the pattern
    “blahblah”

    Consider the strings
       
           “I am so bored blahblah”
       
           “blahblahblah”
       
           “And so blahblah am I!”
       
           “Blahblah is so blahblah!! “
Regex: Perl's Way

    To check if a pattern exists in a string variable
       
           $MyString =~ /blahblah/;
       
           The expression will return 1 if a match is
           found else 0
       
           Use it inside an if condition
             
                if($MyString =~ /blahblah/){         }

    To check if a pattern is not present in a string
       
           $MyString !~ /blahblah/
Contd..

    Check if the line starts with a string : ^
       
           /^blahblah/
             
               Matches only to “blahblah is me”
             
               Don't match to “am so blahblah”

    Check if the line ends with a string : $
       
           /blahblah$/
             • Don't match “blahblah is me”
             • Only match “am so blahblah”
Contd..

    To match any charecter use dot(.)operator
      
          /b.t/
           
            will match to bit,bat,b1t,b0t etc

    To match one or more character: plus(+)
      
          /sho+t/
           
               will match shot,shoot,shooot,shooooooot
           
               But will not match to sht
Contd..

    To match zero or more characters: star(*)
     
         /sho*t/
          
            Will match to sht,shot,shoot,shooooot

    To match any charecter any number of times
     
         /b.*t/
          
            Will match bt,bot,bit,boot,boooot,baaaaaaat

    How to match an operator say plus(+) in string?
     
         Use escape char()
     
         /B+/ will match “B+”
Few more

    Matching a digit: d

    Matching a non digit : D

    Matching white space : s

    Matching any of the specified char square bracker[]:
       
           Eg:/[abc]cat/ will match to acat,bcat,ccat
       
           /[123]456/ will match 1456,2456,3456

    Fancy way: /[0-4]/ is same as /[01234]/
       
           /[a-d]/ is same as /[abcd]/
       
           /[0-2a-c]/ is same as /[012abc]/
Example Regex
An IP adress:192.168.1.1


          d
         d+
          .
         d+
          .
         d+
          .
         d+
  d+.d+.d+.d+
Date


 03/03/2012
     d+
      /
     d+
      /
     d+

d+/d+/d+
Extracting Matches

    Consider /alpha.+gamma/
       
           It matches string “xxxalphazzzzgamma”
       
           Suppose we want to extract the match
       
           Place the match in single bracket() matched
           value will be available in the variable $1
       
           if($MyString =~/alpha(.+)gamma/){
               
                   Print $1
       
           }
Extracting Date

    Extract date/month/year from the string
    “20/10/2012”

    if($MyString =~/(d+)/(d+)/(d+)/){
        
            $date= $1;
        
            $month=$2;
        
            $year=$3;

    }
FILE Opening

    open(FILEHANDLE, MODE, EXPR)
      
          open($FH,"<","trace.txt") or die $!;

    Modes:
      
          > Write/Create
      
          < Read
      
          >> Appened/Create
      
          +< Read/Write
      
          +> Read/Write/Create
      
          +>> Read/Append/Create
Reading a File

    To read a single line $MyLine = <$FH>

    To read the whole file $MyFile = <@FH>
       
           Not recommended as it will try to load the
           entire file into memory
       
           Instead use a loop

    Safer way to process a large file
       
           while($MyLine= <$FH>){
       
           #process a line
       
           }
File Closing

    Use close function along with file handler
       
           close($FH);
File Closing

    Use close function along with file handler
       
           close($FH);
Log Parser

    Open the log file and count the number of lines

    Count the number of packets

    Identify Unique IPs and number of occurances of
    each IP

    Identify the IPs exchanging ICMP traffic

    Identify missed pings if any
Functions(Sub Routines)

    Used for code re use and maintainability

    No need to declare subroutines, define and use

    Defining
       
           Sub MyFunction {

       
           #code to be executed
       
           }

    Calling a function

    MyFunction();
Passing values to sub

    Values passed to a sub routine will be available in a
    special array named @_

    sub MyFunction{
             
                 @argArray     = @_;
             
                 print Dumper @argArray;

    }

    MyFunction(‘arg1’,789);
Returning Values From sub

    Use return $variable;


    sub MyFunction{
        
            $num1 = shift(@_);
        
            $num2 = shift(@_);
        
            $sum = $num1 + $num2;
        
            return $sum;

    }
Perl Modules

    Modules are similar to libraries

    For code re usability

    Standard modules are available with perl installation
          Eg: Data::Dumper

    Non standard modules can be downloaded and
    installed
       
           CPAN : Comprehensive Perl Archive Network

    Typically will be in a .pm file
Creating a Module

    Similar to normal Perl code

    Start module package MyModule;
       
           The file should be named MyModule.pm

    End Module with 1;

    To use a modulein code use MyModule;

    To call a sub routine in module MyModule-
    >MyFuntion();
Strict Usage

    By default perl doesn't need any variable to be
    declared before use

    Simple spelling mistakes in variable names can lead
    to hours of code debugging!

    By using the strict method,perl will strictly ask you
    declare variable
     
         my $MyFirstVar;
     
         my @MyFirstArray;
     
         my %MyFirstHash;

Contenu connexe

Tendances

Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1Dave Cross
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 
Perl programming language
Perl programming languagePerl programming language
Perl programming languageElie Obeid
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsRoy Zimmer
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer. Haim Michael
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlsana mateen
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsNihar Ranjan Paital
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awkYogesh Sawant
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2Nihar Ranjan Paital
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1Nihar Ranjan Paital
 
Perl 101 - The Basics of Perl Programming
Perl  101 - The Basics of Perl ProgrammingPerl  101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl ProgrammingUtkarsh Sengar
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and PatternsHenry Osborne
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners PerlDave Cross
 

Tendances (20)

Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Hashes
HashesHashes
Hashes
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
Php
PhpPhp
Php
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awk
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
 
Perl 101 - The Basics of Perl Programming
Perl  101 - The Basics of Perl ProgrammingPerl  101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl Programming
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
 

En vedette

Achieving the Impossible with Perl
Achieving the Impossible with PerlAchieving the Impossible with Perl
Achieving the Impossible with PerlAdam Trickett
 
The Essential Perl Hacker's Toolkit
The Essential Perl Hacker's ToolkitThe Essential Perl Hacker's Toolkit
The Essential Perl Hacker's ToolkitStephen Scaffidi
 
Beginning Kindle Hackery
Beginning Kindle HackeryBeginning Kindle Hackery
Beginning Kindle HackeryJesse Vincent
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1Rakesh Mukundan
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning PerlDave Cross
 
Functional perl
Functional perlFunctional perl
Functional perlErrorific
 
Perl University: Getting Started with Perl
Perl University: Getting Started with PerlPerl University: Getting Started with Perl
Perl University: Getting Started with Perlbrian d foy
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlDave Cross
 

En vedette (13)

Achieving the Impossible with Perl
Achieving the Impossible with PerlAchieving the Impossible with Perl
Achieving the Impossible with Perl
 
The Essential Perl Hacker's Toolkit
The Essential Perl Hacker's ToolkitThe Essential Perl Hacker's Toolkit
The Essential Perl Hacker's Toolkit
 
Beginning Kindle Hackery
Beginning Kindle HackeryBeginning Kindle Hackery
Beginning Kindle Hackery
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Idiotic Perl
Idiotic PerlIdiotic Perl
Idiotic Perl
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
 
Functional perl
Functional perlFunctional perl
Functional perl
 
Perl University: Getting Started with Perl
Perl University: Getting Started with PerlPerl University: Getting Started with Perl
Perl University: Getting Started with Perl
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Perl Programming - 01 Basic Perl
Perl Programming - 01 Basic PerlPerl Programming - 01 Basic Perl
Perl Programming - 01 Basic Perl
 

Similaire à Practical approach to perl day2

Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2Dave Cross
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Andrea Telatin
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java ProgrammersMike Bowler
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kianphelios
 
Introduction to Modern Perl
Introduction to Modern PerlIntroduction to Modern Perl
Introduction to Modern PerlDave Cross
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Roy Zimmer
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docxajoy21
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell ScriptDr.Ravi
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1Dr.Ravi
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoobirbal
 

Similaire à Practical approach to perl day2 (20)

Cleancode
CleancodeCleancode
Cleancode
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
 
First steps in PERL
First steps in PERLFirst steps in PERL
First steps in PERL
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
Introduction to Modern Perl
Introduction to Modern PerlIntroduction to Modern Perl
Introduction to Modern Perl
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
Apache pig
Apache pigApache pig
Apache pig
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
ruby1_6up
ruby1_6upruby1_6up
ruby1_6up
 
ruby1_6up
ruby1_6upruby1_6up
ruby1_6up
 
Pig workshop
Pig workshopPig workshop
Pig workshop
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 

Dernier

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Dernier (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Practical approach to perl day2

  • 1. Practical Approach to PERL (Day2) Rakesh Mukundan
  • 2. String Comparison  We want to check if a string contains the pattern “blahblah”  Consider the strings  “I am so bored blahblah”  “blahblahblah”  “And so blahblah am I!”  “Blahblah is so blahblah!! “
  • 3. Regex: Perl's Way  To check if a pattern exists in a string variable  $MyString =~ /blahblah/;  The expression will return 1 if a match is found else 0  Use it inside an if condition  if($MyString =~ /blahblah/){ }  To check if a pattern is not present in a string  $MyString !~ /blahblah/
  • 4. Contd..  Check if the line starts with a string : ^  /^blahblah/  Matches only to “blahblah is me”  Don't match to “am so blahblah”  Check if the line ends with a string : $  /blahblah$/ • Don't match “blahblah is me” • Only match “am so blahblah”
  • 5. Contd..  To match any charecter use dot(.)operator  /b.t/  will match to bit,bat,b1t,b0t etc  To match one or more character: plus(+)  /sho+t/  will match shot,shoot,shooot,shooooooot  But will not match to sht
  • 6. Contd..  To match zero or more characters: star(*)  /sho*t/  Will match to sht,shot,shoot,shooooot  To match any charecter any number of times  /b.*t/  Will match bt,bot,bit,boot,boooot,baaaaaaat  How to match an operator say plus(+) in string?  Use escape char()  /B+/ will match “B+”
  • 7. Few more  Matching a digit: d  Matching a non digit : D  Matching white space : s  Matching any of the specified char square bracker[]:  Eg:/[abc]cat/ will match to acat,bcat,ccat  /[123]456/ will match 1456,2456,3456  Fancy way: /[0-4]/ is same as /[01234]/  /[a-d]/ is same as /[abcd]/  /[0-2a-c]/ is same as /[012abc]/
  • 8. Example Regex An IP adress:192.168.1.1 d d+ . d+ . d+ . d+ d+.d+.d+.d+
  • 9. Date 03/03/2012 d+ / d+ / d+ d+/d+/d+
  • 10. Extracting Matches  Consider /alpha.+gamma/  It matches string “xxxalphazzzzgamma”  Suppose we want to extract the match  Place the match in single bracket() matched value will be available in the variable $1  if($MyString =~/alpha(.+)gamma/){  Print $1  }
  • 11. Extracting Date  Extract date/month/year from the string “20/10/2012”  if($MyString =~/(d+)/(d+)/(d+)/){  $date= $1;  $month=$2;  $year=$3;  }
  • 12. FILE Opening  open(FILEHANDLE, MODE, EXPR)  open($FH,"<","trace.txt") or die $!;  Modes:  > Write/Create  < Read  >> Appened/Create  +< Read/Write  +> Read/Write/Create  +>> Read/Append/Create
  • 13. Reading a File  To read a single line $MyLine = <$FH>  To read the whole file $MyFile = <@FH>  Not recommended as it will try to load the entire file into memory  Instead use a loop  Safer way to process a large file  while($MyLine= <$FH>){  #process a line  }
  • 14. File Closing  Use close function along with file handler  close($FH);
  • 15. File Closing  Use close function along with file handler  close($FH);
  • 16. Log Parser  Open the log file and count the number of lines  Count the number of packets  Identify Unique IPs and number of occurances of each IP  Identify the IPs exchanging ICMP traffic  Identify missed pings if any
  • 17. Functions(Sub Routines)  Used for code re use and maintainability  No need to declare subroutines, define and use  Defining  Sub MyFunction {  #code to be executed  }  Calling a function  MyFunction();
  • 18. Passing values to sub  Values passed to a sub routine will be available in a special array named @_  sub MyFunction{  @argArray = @_;  print Dumper @argArray;  }  MyFunction(‘arg1’,789);
  • 19. Returning Values From sub  Use return $variable;  sub MyFunction{  $num1 = shift(@_);  $num2 = shift(@_);  $sum = $num1 + $num2;  return $sum;  }
  • 20. Perl Modules  Modules are similar to libraries  For code re usability  Standard modules are available with perl installation  Eg: Data::Dumper  Non standard modules can be downloaded and installed  CPAN : Comprehensive Perl Archive Network  Typically will be in a .pm file
  • 21. Creating a Module  Similar to normal Perl code  Start module package MyModule;  The file should be named MyModule.pm  End Module with 1;  To use a modulein code use MyModule;  To call a sub routine in module MyModule- >MyFuntion();
  • 22. Strict Usage  By default perl doesn't need any variable to be declared before use  Simple spelling mistakes in variable names can lead to hours of code debugging!  By using the strict method,perl will strictly ask you declare variable  my $MyFirstVar;  my @MyFirstArray;  my %MyFirstHash;