Perl Newbies

  1. Perl
  2. Functions
  3. here

unlink function - Delete a file

unlink function removes a file. The return value is the count of deleted files.

unlink $file;

unlink function programming example

The following is a unlink function programming example.

use strict;
use warnings;
use Fcntl;

# Delete a file

# Create a preparation file.
my $file = "sample_20080520_$$.txt";

sysopen(my $fh, $file, O_CREAT | O_WRONLY | O_EXCL)
  or die "Can't create $file : $!";
close( $fh );
print "Preparation: Created '$file'\n\n";

print "1: Delete a file\n";
if (unlink $file) {
  print "Deleted '$file'\n";
else {
  print "'Can't delete $file'\n";

(See also)sysopen functionFcntl