4.7.1 Translating Path Name Formats
4.7.1 Translating Path Name Formats
split takes two arguments: a pattern and a string. It breaks up the string into one or more substrings based on the pattern. The call to split returns all the substrings in the original sequence.
Suppose we have a file name with path in the Unix style. Assume we want to convert it into the Microsoft Windows style. In the Unix style, directory and file names are separated by /. Therefore, a valid name is something like the following.
/users/server/faculty/kalita/public_html/index.html
The same name in the Microsoft Windows style looks like
\users\server\faculty\kalita\public_html\index.html
There are many ways to do this conversion. One way is given below. Note that this is not the most efficient way to do so.
Program 4.23
#!/usr/bin/perl
print join ("\\", split (m@/@, $ARGV[0])), "\n";
Assuming the script is stored in the file convert.pl, a call to this script is something like
%convert.pl /users/server/faculty/kalita/public_html/index.html
It prints the name in the Microsoft Windows style.
\users\server\faculty\kalita\public_html\index.html First, split takes the argument given to it and produces a list whose elements are "users" "server" "faculty" "kalita" "public_html" "index.html"
Next, the join operator takes a string of length zero or more (not a pattern) and uses this string as the glue to put the list (the second argument) together. In this case, putting the list back together with \ as the glue or the separator gives us the file name in the Microsoft style. The backslash needs to be escaped using another backslash inside a string.
