perl function unlink

The unlink function in Perl is used to delete a file or list of files specified by the list of filenames provided to it. The function returns the number of files successfully deleted.

Here's an example of using unlink function to delete a file named "file.txt":

refer to‮figi:‬tidea.com
unlink("file.txt") or die "Unable to delete file: $!";

In the above example, unlink function is called with the filename "file.txt" as its argument. The or die syntax is used to catch any errors that might occur during the file deletion process. If an error occurs, the die function is called with an error message that includes the value of the special variable $!, which holds the system error message associated with the error.

Here's another example that demonstrates how to use unlink to delete multiple files:

my @files = ("file1.txt", "file2.txt", "file3.txt");

my $num_deleted = unlink(@files);

print "$num_deleted files deleted\n";

In this example, an array @files is created with the filenames to be deleted. The unlink function is called with the array as its argument, and the number of files deleted is stored in the scalar variable $num_deleted. Finally, the number of files deleted is printed to the console.