5.19. String functions

substr STRING, OFFSET, LENGTH - the content of a substring given its location

Example 5-14. examples/scalars/string_functions_substr.pl

#!/usr/bin/perl
use strict;
use warnings; 

my $s = "The black cat climbed the green tree";
my $z;
$z = substr $s, 4, 5;                         # $z = black
print "$z\n";
$z = substr $s, 4, -11;                       # $z = black cat climbed the 
print "$z\n";
$z = substr $s, 14;                           # $z = climbed the green tree
print "$z\n";
$z = substr $s, -4;                           # $z = tree
print "$z\n";
$z = substr $s, -4, 2;                        # $z = tr
print "$z\n";

$z = substr $s, 14, 7, "jumped from";         # $z = climbed
print "$z\n";
print "$s\n";                                 # $s = The black cat jumped from the green tree