File tests and split/join functions.

The split operator

Another operator that uses regular expressions is split, which breaks up a string according to a separator. This is useful for tab-separated data, or colon-separated, whitespace-separated, or anything-separated data. If you can specify the separator with a regular expression, you can use split:
@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

The join operator

The join function does the job completely the opposite to the function split. It takes two arguments - a scalar variable to use as a separator (not a regular expression just a simple variable) and an array, and it returns a string that contains the elements of the array separated by the given separator. For example, the following code
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

File tests

When we open a file for writing (using > sign), we will create a new file, wiping out any existing file with the same name. Perhaps you want to check if there isn't a file by that name. Perhaps you need to know how old the given file is. Perl has a complete set of tests you can use to find out information about files. For example, to check if there is a file named test.txt we need to use the -e file test:
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)
Note: In all Unix systems, a UID, or "user ID number", is an integer (actually an unsigned short on older systems) that identifies particular user. Every running process has at least two UID numbers associated with it, the real UID number, which identifies the user who launched the process, and the effective UID number, which is used to determine what resources the process can access. Normally these are the same, but if a program with a set-uid bit set is run, then while the real UID remains that of the user who ran it, the effective UID is that of the user who owns the file.

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);
  }
}