Perl Tutorial

Fundamentals

Input and Output

Control Flow

Arrays and Lists

Hash

Scalars

Strings

Object Oriented Programming in Perl

Subroutines

Regular Expressions

File Handling

Context Sensitivity

CGI Programming

Misc

String functions in Perl

In Perl, string manipulation is a fundamental aspect of the language, given its roots in text processing. Perl provides a rich set of functions for string operations. This tutorial provides an overview of the essential string functions in Perl.

1. String Length: length

Returns the length of a string.

my $str = "Hello, world!";
my $len = length($str);  # $len is 13

2. Concatenation: .

Combine two strings.

my $greeting = "Hello, " . "world!";  # "Hello, world!"

3. String Repetition: x

Repeats a string a given number of times.

my $repeated = "ha" x 3;  # "hahaha"

4. Substring Extraction: substr

Extracts part of a string.

my $extract = substr("Hello, world!", 0, 5);  # "Hello"

You can also use substr to modify parts of a string.

substr($str, 7, 5) = "Planet";  # $str is now "Hello, Planet!"

5. Case Conversion: lc, uc, lcfirst, and ucfirst

  • lc: Convert to lowercase.
  • uc: Convert to uppercase.
  • lcfirst: Convert the first character to lowercase.
  • ucfirst: Convert the first character to uppercase.
print lc("HELLO");       # "hello"
print uc("world");       # "WORLD"
print ucfirst("hello");  # "Hello"
print lcfirst("WORLD");  # "wORLD"

6. String Indexing: index and rindex

  • index: Find the position of a substring.
  • rindex: Find the last position of a substring.
my $position = index("apple pie", "pie");   # 6
my $last_pos = rindex("apple pie pie", "pie");  # 10

7. String Replace: Using the s/// operator

Replace parts of a string using regular expressions.

my $text = "Hello, world!";
$text =~ s/world/Perl/;  # $text is now "Hello, Perl!"

8. String Split and Join: split and join

  • split: Breaks a string into a list of substrings.
  • join: Combines a list of strings into a single string.
my @words = split(/ /, "Hello, world!");
my $sentence = join(" ", @words);

9. Quoting Strings: q and qq

  • q: Single quote a string.
  • qq: Double quote a string.
my $single = q(This is a 'single' quoted string);
my $double = qq(This is a "double" quoted string);

10. String Chomp and Chop: chomp and chop

  • chomp: Removes trailing newline or a specific trailing string.
  • chop: Removes the last character from a string.
my $input = "Hello\n";
chomp($input);  # $input is now "Hello"

my $word = "Hello";
chop($word);    # $word is now "Hell"

11. Formatting Strings: printf and sprintf

Format strings with specific layouts.

printf("I have %d apples.", 5);  # Prints "I have 5 apples."

my $formatted = sprintf("I have %d apples.", 5);  # "I have 5 apples."

12. Summary

Perl's rich set of string functions makes it a powerful language for text processing tasks. By mastering these functions, you can efficiently manipulate, format, and analyze textual data in your Perl programs.

  1. Manipulating strings in Perl:

    • Description: Basic string manipulation operations.
    • Code Example:
      my $string = "Hello, Perl!";
      
      # Manipulating strings
      my $uppercase = uc($string);
      my $lowercase = lc($string);
      
  2. String concatenation in Perl:

    • Description: Concatenating strings using the concatenation operator (.).
    • Code Example:
      my $first_name = "John";
      my $last_name = "Doe";
      
      my $full_name = $first_name . " " . $last_name;
      
  3. Substring extraction in Perl:

    • Description: Extracting substrings using substr.
    • Code Example:
      my $sentence = "Perl is powerful";
      
      my $substring = substr($sentence, 5, 2);  # Extracts "is"
      
  4. String length function in Perl:

    • Description: Determining the length of a string using length.
    • Code Example:
      my $word = "Perl";
      
      my $length = length($word);
      
  5. String comparison in Perl:

    • Description: Comparing strings using eq, ne, lt, le, gt, ge.
    • Code Example:
      my $str1 = "Perl";
      my $str2 = "Python";
      
      if ($str1 eq $str2) {
          print "Strings are equal.\n";
      } else {
          print "Strings are not equal.\n";
      }
      
  6. Searching and replacing in strings in Perl:

    • Description: Searching and replacing using index and s///.
    • Code Example:
      my $sentence = "Perl is fun and Perl is powerful";
      
      # Searching for a substring
      my $position = index($sentence, "Perl");
      
      # Replacing a substring
      $sentence =~ s/Perl/Python/;
      
  7. String splitting and joining in Perl:

    • Description: Splitting and joining strings using split and join.
    • Code Example:
      my $csv_data = "John,Doe,30";
      
      # Splitting a CSV string
      my @fields = split(',', $csv_data);
      
      # Joining array elements into a string
      my $joined_data = join('-', @fields);
      
  8. Regular expressions for string manipulation in Perl:

    • Description: Using regular expressions for advanced string manipulation.
    • Code Example:
      my $text = "The quick brown fox jumps over the lazy dog";
      
      # Matching words starting with 'b'
      my @matches = $text =~ /\b[bB]\w+/g;
      
  9. Case conversion functions in Perl strings:

    • Description: Converting string case using ucfirst, lcfirst, uc, lc.
    • Code Example:
      my $name = "alice";
      
      # Capitalizing the first letter
      my $capitalized = ucfirst($name);
      
      # Converting to uppercase
      my $uppercase_name = uc($name);