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
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 

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
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 

Último

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
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
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 

Último (20)

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
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
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 

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;