SlideShare uma empresa Scribd logo
1 de 22
Baixar para ler offline
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;

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Destaque

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
 

Destaque (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
 

Semelhante a 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
 

Semelhante a 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
 

Último

Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Último (20)

Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 

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;