I have the following Perl script.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @fruits = qw(apple banana orange pear);
print Dumper \@fruits;
foreach my $fruit (@fruits) {
$fruit =~ s|apple|peach|;
}
print Dumper \@fruits;
The following is returned.
$VAR1 = [
'apple',
'banana',
'orange',
'pear'
];
$VAR1 = [
'peach',
'banana',
'orange',
'pear'
];
I do not understand why the following line has changed apple to peach in the @fruits array, as I thought this line would only apply to the $fruit variable, not the @fruits array.
$fruit =~ s|apple|peach|;