@array_variable = split(/separator/, string);This functions splits the string passed as the second argument according to the regular expression that sets the separator. The result of the function is an array of strings. For example,
my $str = "daemon:x::2:daemon:/sbin:";
my @fields = split(/:/, $str);
foreach (@fields){
print "field: $_\n";
}
In the example above we didn't actually use a regular expression because the separator was very simple.
If we are talking about words in a sentence for example, then these words can be separated with one
or two or more spaces, or a tab symbols, or a new line symbols. In this case we can use the power of
regular expressions:
$sentence = "This is a simple sentence,\nwhich continues on the second line\n and on\tthe third too.";
foreach $word ( split(/\s+/, $sentence) ){
print "word: $word\n";
}
If the regular expression for the separator is empty, then the function split returns an array of characters from the line under operation. Thus, the following code
my @chars = split(//, "This is my string"); print "@chars\n";returns:
T h i s i s m y s t r i n g
my @chars = split(//, "This is my string");
print join("_", @chars) . "\n";
returns
T_h_i_s_ _i_s_ _m_y_ _s_t_r_i_n_g
my $fname = "test.txt";
if( -e $fname ){
print "File '$fname' exists\n";
}
Here is the list of all file tests. Please note that some of these tests return true/false values and some
of them return numeric values:
| File test | Meaning |
|---|---|
| -r | File is readable by effective uid/gid |
| -w | File is writable by effective uid/gid. |
| -x | File is executable by effective uid/gid. |
| -o | File is owned by effective uid. |
| -R | File is readable by real uid/gid. |
| -W | File is writable by real uid/gid. |
| -X | File is executable by real uid/gid. |
| -O | File is owned by real uid. |
| -e | File exists. |
| -z | File has zero size (is empty). |
| -s | File has nonzero size (returns size in bytes). |
| -f | File is a plain file. |
| -d | File is a directory. |
| -l | File is a symbolic link. |
| -p | File is a named pipe (FIFO), or Filehandle is a pipe. |
| -S | File is a socket. |
| -b | File is a block special file. |
| -c | File is a character special file. |
| -t | Filehandle is opened to a tty |
| -u | File has setuid bit set. |
| -g | File has setgid bit set. |
| -k | File has sticky bit set. |
| -T | File is an ASCII text file (heuristic guess). |
| -B | File is a "binary" file (opposite of -T). |
| -M | Script start time minus file modification time, in days. |
| -A | Same for access time. |
| -C | Same for inode change time (Unix, may differ for other platforms) |
Here is a short example that takes a file name as an argument and prints some information about the file:
printf("%12s %8s %12s %12s\n", "File name", "Size", "Modified", "Accessed");
for my $fname ( @ARGV ){
if( -e $fname ){
printf("%12s %8d %12d %12d\n", $fname, -s $fname, -M $fname, -A $fname);
}
}
The same example can be rewritten shorter if we decide to use the default variable $_:
printf("%12s %8s %12s %12s\n", "File name", "Size", "Modified", "Accessed");
for ( @ARGV ){
if( -e ){
printf("%12s %8d %12d %12d\n", $_, -s, -M, -A);
}
}