In perl what does -f mean in an if statement? - perl

my $Path = $_[0];
return "" if(not $Path or not -f $Path);
I am going through a perl file and not able to understand what the -f option means
What does not -f mean ?

The -f operator tests whether its operand is a regular file, rather than a directory, a symbolic link, or another special file.
All operators starting with a - are called file test operators and are usually found in shell-scripting languages as well. They are unary operators (taking just one operand), like ! or ~ are unary operators.

-r readable
-w writable
-x executable
-o owned by user
-R readable by this user or group
-W writable by user or group
-X executable by this user or group
-O owned by this user
-e File or directory name exists
-z File exists and has zero size
-s exists and has nonzero size (the value is the size in bytes)
-f plain file
-d directory
-l symbolic link
-S socket

Related

What is the use `-d` in Perl script? [duplicate]

This question already has answers here:
Using the -d test operator in perl
(3 answers)
Closed 8 years ago.
What does the -d in the following piece of code:
foreach my $filename (#files) {
my $filepath = $dir.$filename;
next if -d $filepath;
function1();
}
This is a short form for
if (-d $filepath) {
next;
}
Where -d $filepath is a test if $filepath is a directory.
See http://perldoc.perl.org/functions/-X.html for a full list of file tests.
-d tests if $filepath is a directory.
All such file tests are documented at perldoc -X:
-X FILEHANDLE
-X EXPR
-X DIRHANDLE
-X
A file test, where X is one of the letters listed below. This unary operator takes one argument, either a filename, a filehandle, or a dirhandle, and tests the associated file to see if something is true about it. If the argument is omitted, tests $_, except for -t, which tests STDIN. Unless otherwise documented, it returns 1 for true and '' for false. If the file doesn't exist or can't be examined, it returns undef and sets $! (errno). Despite the funny names, precedence is the same as any other named unary operator. The operator may be any of:
...
-f File is a plain file.
-d File is a directory.
...
It checks for the directory...
A short example to check that
$somedir = "c:/windows";
if (-d $somedir) {
print "$somedir exists";
} else {
print "$somedir does not exist!";
}
Also check the docs for other such cases
-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.
Essentially, next if -d $filepath; means "if this file is a directory, run the next iteration of the loop", which effectively skips the call of function1 for that file. In short, it is a way of applying function1 only to files which are NOT directories.

tilde (~) directories in Perl

I found a slight misbehaviour in my Perl script when I create and check for the existence of directories with a tilde sign, which doesn't happen if I use a full /home/user path. When I run this script for the first time, it creates the new directory. When I run it the second time, it doesn't recognise the existence of the directory, and tries to create it a second time:
#!/usr/bin/perl
use strict;
my $outdir = '~/test';
my $cmd = "mkdir $outdir";
unless (-d $outdir) {
0 == system($cmd) or die "Error creating outdir $outdir\n $?";
}
1;
[~] $ rm test/ -rf
[~] $ perl dir.pl
[~] $ perl dir.pl
mkdir: cannot create directory `/home/avilella/test': File exists
Error creating outdir ~/test
256 at dir.pl line 7.
How can I reliably deal with directories that use the tilde ~ sign in Perl?
The tilde is interpreted by the shell to mean your home directory.
Hence Perl's -d operator sees something different (a file/directory called ~) to your shell invocation 'mkdir ~/whatever' (which expands ~ to mean /home/user).
I would try to use exclusively Perl functions to perform your operations. You'll avoid spawning new processes and your file access will be performed in a consistent fashion.
Note Perl's mkdir built-in function. Note also the File::Glob module which does perform expansion of the ~ character (perhaps useful if you have users entering directory names manually)
You can use the %ENV home directory, which is the values imported from the shell:
my $home = $ENV{HOME};
You should also know that mkdir is a Perl built-in function:
mkdir "$home/test" or die "Cannot create test: $!";
~ is interpreted by the shell that is invoked by the system function. It's the shell that replaces ~ by the user's home directory. As far as Perl or the kernel is concerned, ~ means a file or directory with a one-character name, like any other character. So the test done by -d fails, because there's no directory called ~.
If you'd used Perl's built-in mkdir function rather than calling an external command via a shell script, you would have had an error at that point because the directory ~ doesn't exist.
The user's home directory is almost always available in the environment variable HOME. If you like, you can fall back to querying the user database if HOME is not present, but that's an abnormal situation. Do use the HOME environment variable if it is present, because it is sometimes useful to change it to run a program with different configuration files, and the environment variable is always available in practice whereas the user database could be unavailable due to network trouble in some configurations (e.g. NIS or LDAP).
my $home_directory = $ENV{HOME};
if (!defined $home_directory) {$home_directory = getpwuid($<);}
my $outdir = "$home_directory/test";
unless (-d $outdir) {
mkdir $outdir or die "Error creating $outdir: $!\n"
}
Your script can't create directories which exist. That's the error you presented us:
[~] $ rm test/ -rf
[~] $ perl dir.pl
[~] $ perl dir.pl
mkdir: cannot create directory `/home/avilella/test': File exists
Error creating outdir ~/test
256 at dir.pl line 7.
The problem is the line of your delete:
[~] $ rm test/ -rf
is wrong. Like most commands, the right syntax would be:
[~] $ <command> <options> <parameters>
so it would be:
[~] $ rm -rf test/

perl -pe to manipulate filenames

I was trying to do some quick filename cleanup at the shell (zsh, if it matters). Renaming files. (I'm using cp instead of mv just to be safe)
foreach f (\#*.ogg)
cp $f `echo $f | perl -pe 's/\#\d+ (.+)$/"\1"/'`
end
Now, I know there are tools to do stuff like this, but for personal interest I'm wondering how I can do it this way. Right now, I get an error:
cp: target `When.ogg"' is not a directory
Where 'When.ogg' is the last part of the filename. I've tried adding quotes (see above) and escaping the spaces, but nonetheless this is what I get.
Is there a reason I can't use the output of s perl pmr=;omrt as the final argument to another command line tool?
It looks like you have a space in the file names being processed, so each of your cp command lines evaluates to something like
cp \#nnnn When.Ogg When.ogg
When the cp command sees more than two arguments, the last one must be a target directory name for all the files to be copied to - hence the error message. Because your source filename ($f) contains a space it is being treated as two arguments - cp sees three args, rather than the two you intend.
If you put double quotes around the first $f that should prevent the two 'halves' of the name from being treated as separate file names:
cp "$f" `echo ...
This is what you need in bash, hope it's good for zsh too.
cp "$f" "`echo $f | perl -pe 's/\#\d+ (.+)$/\1/'`"
If the filename contains spaces, you also have quote the second argument of cp.
I often use
dir /b ... | perl -nle"$o=$_; s/.../.../; $n=$_; rename $o,$n if !-e $n"
The -l chomps the input.
The -e check is to avoid accidentally renaming all the files to one name. I've done that a couple of times.
In bash (and I'm guessing zsh), that would be
foreach f (...)
echo "$f" | perl -nle'$o=$_; s/.../.../; $n=$_; rename $o,$n if !-e $n'
end
or
find -name '...' -maxdepth 1 \
| perl -nle'$o=$_; s/.../.../; $n=$_; rename $o,$n if !-e $n'
or
find -name '...' -maxdepth 1 -exec \
perl -e'for (#ARGV) {
$o=$_; s/.../.../; $n=$_;
rename $o,$n if !-e $n;
}' {} +
The last supports file names with newlines in them.

How to search and replace in text files only?

I have a directory containing a bunch of files, some text some binary, with no consistent naming. I want to search and replace a string in text files only. So I went with:
perl -i -pne 's#/some/text/to/replace#/replacement/text#' *
Remove the -i option and you will see that binary files get caught. How do I modify this one-liner to skip binary files?
ack -n --text --sort -f . | xargs perl -i -pne 's…'
Abusing ack goes much quicker than writing your own solution with -T.
Well, this is all based on what your definition of a text file is. Perl 5 has the -T filetest operator that will tell you if a filename or filehandle is a text file (using Perl 5's definition):
perl -i -pne 'BEGIN{#ARGV=grep-T,#ARGV}s#regex#replacement#' *
The BEGIN block will filter out any files that don't pass the -T test, so they won't even be read (except for their first block because that is what -T uses to determine if they are text).
From perldoc -f -X
The -T and -B switches work as follows. The first block or so of the file is examined for odd characters such as strange control codes or characters with the high bit set. If too many strange characters (>30%) are found, it's a -B file; otherwise it's a -T file. Also, any file containing a zero byte in the first block is considered a binary file. If -T or -B is used on a filehandle, the current IO buffer is examined rather than the first block. Both -T and -B return true on an empty file, or a file at EOF when testing a filehandle. Because you have to read a file to do the -T test, on most occasions you want to use a -f against the file first, as in next unless -f $file && -T $file .

How can I check if a file exists in Perl?

I have a relative path
$base_path = "input/myMock.TGZ";
myMock.TGZ is the file name located in input folder.
The filename can change. But the path is always stored in $base_path.
I need to check if the file exists in $base_path.
Test whether something exists at given path using the -e file-test operator.
print "$base_path exists!\n" if -e $base_path;
However, this test is probably broader than you intend. The code above will generate output if a plain file exists at that path, but it will also fire for a directory, a named pipe, a symlink, or a more exotic possibility. See the documentation for details.
Given the extension of .TGZ in your question, it seems that you expect a plain file rather than the alternatives. The -f file-test operator asks whether a path leads to a plain file.
print "$base_path is a plain file!\n" if -f $base_path;
The perlfunc documentation covers the long list of Perl's file-test operators that covers many situations you will encounter in practice.
-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 (false if symlinks aren’t supported by the file system).
-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 or UTF-8 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)
You might want a variant of exists ... perldoc -f "-f"
-X FILEHANDLE
-X EXPR
-X DIRHANDLE
-X A file test, where X is one of the letters listed below. This unary operator takes one argument,
either a filename, a filehandle, or a dirhandle, and tests the associated file to see if something is
true about it. If the argument is omitted, tests $_, except for "-t", which tests STDIN. Unless
otherwise documented, it returns 1 for true and '' for false, or the undefined value if the file
doesn’t exist. Despite the funny names, precedence is the same as any other named unary operator.
The operator may be any of:
-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.
if (-e $base_path)
{
# code
}
-e is the 'existence' operator in Perl.
You can check permissions and other attributes using the code on this page.
Use:
if (-f $filePath)
{
# code
}
-e returns true even if the file is a directory. -f will only return true if it's an actual file
You can use: if(-e $base_path)
if(-e $base_path)
{
print "Something";
}
would do the trick.
#!/usr/bin/perl -w
$fileToLocate = '/whatever/path/for/file/you/are/searching/MyFile.txt';
if (-e $fileToLocate) {
print "File is present";
}
Use the below code. Here -f checks if it's a file or not:
print "File $base_path is exists!\n" if -f $base_path;