By Alvin Alexander. Last updated: June 4, 2016
I was just modifying a Perl program so I could use a regular expression (regex) to search a Perl array for all less-than (<) and greater-than (>) symbols, and replace those with their HTML equivalents tags ("<", and ">", respectively).
Here's the source code I created to perform this Perl array search and replace operation (a Perl replace regex operation):
open(MY_FILE,$fileName); my @fileContents = (<MY_FILE>); close(MY_FILE); for (@fileContents) { s/>/>\;/; s/</<\;/; }
This Perl program does the following:
- Open a file, where the file is given by the variable
$fileName
. - Read the contents of the file into the Perl array variable named
@fileContents
. - Closes the file.
- Loops through each element in the
@fileContents
array, and does the desired Perl search and replace operations (using the regular expressions shown).