Perl
  Home arrow Perl arrow Page 3 - Perl 101 (part 6) - The Perl Toolbox
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Perl 101 (part 6) - The Perl Toolbox
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2000-08-30


    Table of Contents:
  • Perl 101 (part 6) - The Perl Toolbox
  • Expressing Yourself
  • Engry Young Men
  • Aardvark, Anyone?
  • Needles In Haystacks
  • Slice And Dice
  • Going Backwards
  • Math Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl 101 (part 6) - The Perl Toolbox - Engry Young Men
    ( Page 3 of 8 )

    In addition to simple matching, Perl also allows you to perform substitutions.

    Take a look at our next example, which prompts you to enter a line of text, and then replaces all occurrences of the letter "a" with the letter "e".


    #!/usr/bin/perl
    
    # get a line of input
    print "Gimme a line!\n";
    $line = ;
    chomp ($line);
    
    # substitute with the substitution operator
    $line =~ s/a/e/;
    
    # and print
    print $line;
    

    In this case, we've used Perl's substitution operator - it looks like this:
    s/search-pattern/replacement-pattern/
    

    In the example above, the line
    $line =~ s/a/e/;
    

    simply means "substitute a with e in the scalar variable $line".

    And here's the output:


    
    
    
    Gimme a line!
    angry young man
    
    engry young man
    

    Ummm...didn't work quite as advertised, did it? All it did was replace the first occurrence of the letter "a". How about adding the "g" operator, which does a global search-and-replace?


    #!/usr/bin/perl
    
    # get a line of input
    print "Gimme a line!\n";
    $line = ;
    chomp ($line);
    
    # substitute with the substitution operator
    $line =~ s/a/e/g;
    
    # and print
    print $line;
    

    And this time,
    
    
    angry young man
    
    

    is replaced with
    
    
    
    engry young men
    

    Much better! But what if your sentence contains an upper-case "a", also known as "A". Well, that's why Perl also has the case-insensitivity operator "i", which takes care of that last niggling problem:


    #!/usr/bin/perl
    
    # get a line of input
    print "Gimme a line!\n";
    $line = ;
    chomp ($line);
    
    # substitute with the substitution operator
    $line =~ s/a/e/gi;
    
    # and print
    print $line;
    

    And the output:
    
    
    Gimme a line!
    Angry young man
    Engry young men
    
    

    Of course, we're just scratching the tip of the regex iceberg here. Things get even more interesting when you start using patterns and metacharacters instead of actual words...

    This article copyright Melonfire 2000. All rights reserved.

     
     
    >>> More Perl Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek