Alternative Delimiter Example in Perl
For a moment, assume that you are a junior webmaster for a UNIX-based Web server.
The boss gives you a list of files on the development server that need to be moved over to the live webserver. The file list looks like this.
index.html
/about/index.html
/about/company.html
/about/company1.html
/products/thingamagigs.html
/about/company2.html
/about/company3.html
/about/ceo.html
/about/mike.html
/products/index.html
/products/wigets.html
/products/thingamabobs.html
/about/joe.html
/art/mike.gif
/art/arrow_left.gif
/art/ceo.gif
and so on ....
But the senior webmaster growls at you when you tell him you are going to make modifications because he has lots of processes running in the background,
so he wants a list of the affected directories.
So you write a script to handle it.
Do you see the problem here?
The directory names are bracketed by /, which we normally use as a delimiter.
But we can use other things, as long as we identify whether or not we are matching or substituting.
Look at my program to read a file for the names, then put the directory names in a second file
Perl 6
#!/user/bin/perl
open(CHANGELIST,"flist") || die "could not open the flist: $!\n";
open(DLIST,">dlist") || die "could not open the dlist: $!\n";
while(<CHANGELIST>) {
s#/(\w+)/#($1)#;
print DLIST "$1 \n";
}
close(CHANGELIST);
close(DLIST);
Notice the key substitution:
s#/(\w+)/#($1)#;
We identify we are substituting and we enclose the search pattern and substitute pattern with #
as a delimiter.
Remember, that the #
character is normally used to indicate a comment in Perl, but we are using it as an alternate delimiter in this example. Also, since we have a $_ because we used while(<CHANGELIST>), it is assumed to be the string upon which the substitution is being applied.