Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if a file exists in Perl?
In this tutorial, we will take a couple of examples and demonstrate how you can if a file exists or not, with the help of Perl. Let's suppose we have a simple text file called "sample.txt" with the following data ?
This is a sample txt file that contains some content inside it. TutorialsPoint is simply amazing!
We will use a Perl code to check whether this file exists or not.
Example 1
The most basic approach to check whether a file exists or not is to use the "-e" flag and then pass the name of the file. Consider the code shown below.
<div class="code-mirror language-perl" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">use</span> warnings<span class="token punctuation">;</span>
<span class="token keyword">use</span> strict<span class="token punctuation">;</span>
<span class="token keyword">my</span> <span class="token variable">$filename</span> <span class="token operator">=</span> <span class="token string">'sample.txt'</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">-e</span> <span class="token variable">$filename</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">print</span> <span class="token string">"the file exists\n"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token keyword">print</span> <span class="token string">"the file does not exist!\n"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</div>
Save the program code as "sample.pl" and then run the code using the following command ?
perl sample.pl
Example 2
We can also add more specific cases to check if a file contains any data, if it exists. In the following example, we will check both the cases, with minimum level of code. The "-s" flag in the code helps us in this regard.
<div class="code-mirror language-perl" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">use</span> warnings<span class="token punctuation">;</span>
<span class="token keyword">use</span> strict<span class="token punctuation">;</span>
<span class="token keyword">my</span> <span class="token variable">$filename</span> <span class="token operator">=</span> <span class="token string">'sample.txt'</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">-e</span> <span class="token variable">$filename</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">print</span> <span class="token string">"the file exists\n"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token keyword">print</span> <span class="token string">"the file does not exist!\n"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">-s</span> <span class="token variable">$filename</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">print</span> <span class="token string">"the file exists and contains some data inside it\n"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token keyword">print</span> <span class="token string">"the file exists but doesn't contain any data inside it\n"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</div>
Again, save the file as "sample.pl" and run it using the following command
perl sample.pl
Conclusion
In addition to "-e" and "-s", there are other flags such as "-r" that checks if a file is readable, "-x" that checks if a file is executable, "-d" that checks if a file is a directory, etc. In this tutorial, we used two simple examples to show how you can use a Perl code to check if a file exists or not.
