Updated April 12, 2023
Introduction to Perl unlink
The files in Perl can be deleted using a function called unlink() function which are specified as a List to the unlink() function where each item in the list is the path to the location of the file that is to be deleted using unlink() function or the file name can be directly can be specified by using $_ and the files deleted using unlink() function in Perl cannot be recovered at any cost, so it is recommended to be very careful before deleting the files using this function and this function deletes the specified files permanently and do not return anything.
Syntax to declare unlink() function in Perl is as follows:
unlink(List); or
unlink($filename);
where List specifies a list with each item specifying the path to the location of the file that is to be deleted and filename specifies the name of the file that is to be deleted.
Working of unlink() function in Perl
Working of unlink() function in Perl is as follows:
- The files in Perl can be deleted using a function called unlink() function which is specified as a List to the unlink() function.
- The List specified to the unlink() function is a list where each item in the list is the path to the location of the file that is to be deleted using unlink() function.
- The name of the file can be directly specified to the unlink() function by using $_.
- The files deleted using unlink() function in Perl cannot be recovered at any cost, so it is recommended to be very careful before deleting the files using this function.
- The unlink() function deletes the specified files permanently and do not return anything.
Examples of Perl unlink
Let us discuss examples of Perl unlink.
Example #1
Code:
use warnings;
use strict;
#The contents to be written to the file are stored in a string called strwrite
my $strwrite = <<END;
Welcome to Perl
END
#a temporary file is created to write the data into the file
my $filepath = "https://cdn.educba.com/tmp/tempfile.txt";
#open function is used to open the file using file handler to write the data to the file
open(FH, '>', $filepath) or die $!;
print FH $strwrite;
close(FH);
print "Data has been written to the file successfully\n";
my $filepath1 = "https://cdn.educba.com/tmp/tempfile.txt";
#open function is used to open the file using file handler to read the data from the file
open(FH1, '<', $filepath1) or die $!;
#displaying the contents of the file
print "The contents of the file are:\n";
while(<FH1>){
print $_;
}
close(FH1);
#using unlink function to permanently delete the file
unlink("https://cdn.educba.com/tmp/tempfile.txt");
print "The contents of the file after using unlink() function is:\n";
my $filepath2 = "https://cdn.educba.com/tmp/tempfile.txt";
#open function is used to open the file using file handler to read the data from the file
open(FH2, '<', $filepath2) or die $!;
#trying to display the contents of the file after using unlink() function
while(<FH2>){
print $_;
}
close(FH2);
The output of the above program is as shown in the snapshot below:
In the above program, we are storing the data to be written to the file in a string called strwrite. Then we are using open function along with file handler to write the data to the file. Once the data has been written to the file, we use the same open function along with file handler to read the contents of the file and display the data as the output on the screen. Then we use unlink() function to delete the file permanently. Then we again try to reread the file using open function along with file handler which fails because the file is permanently deleted by the unlink() function and the corresponding message is displayed as the output on the screen.
Example #2
Code:
use warnings;
use strict;
#The contents to be written to the file are stored in a string called strwrite
my $strwrite = <<END;
Welcome to Perl
END
#a temporary file is created to write the data into the file
my $filepath = "https://cdn.educba.com/tmp/tempfile.txt";
#open function is used to open the file using file handler to write the data to the file
open(FH, '>', $filepath) or die $!;
print FH $strwrite;
close(FH);
print "Data has been written to the file successfully\n";
my $filepath1 = "https://cdn.educba.com/tmp/tempfile.txt";
#open function is used to open the file using file handler to read the data from the file
open(FH1, '<', $filepath1) or die $!;
#displaying the contents of the file
print "The contents of the file are:\n";
while(<FH1>){
print $_;
}
close(FH1);
#using unlink function to permanently delete the file
unlink("https://cdn.educba.com/tmp/tempfile.txt");
print "The contents of the file after using unlink() function is:\n";
my $filepath2 = "https://cdn.educba.com/tmp/tempfile.txt";
#open function is used to open the file using file handler to read the data from the file
open(FH2, '<', $filepath2) or die $!;
#trying to display the contents of the file after using unlink() function
while(<FH2>){
print $_;
}
close(FH2);
The output of the above program is as shown in the snapshot below:
In the above program, we are storing the data to be written to the file in a string called strwrite. Then we are using open function along with file handler to write the data to the file. Once the data has been written to the file, we use the same open function along with file handler to read the contents of the file and display the data as the output on the screen. Then we use unlink() function to delete the file permanently. Then we again try to reread the file using open function along with file handler which fails because the file is permanently deleted by the unlink() function and the corresponding message is displayed as the output on the screen.
Conclusion
In this article, we have learnt the concept of unlink() function in Perl through definition, syntax, and working of unlink() function through programming examples and their outputs.
Recommended Articles
This is a guide to Perl unlink. Here we discuss the introduction, syntax, Working of unlink() function in Perl, and examples with code implementation. You may also have a look at the following articles to learn more –