Monday 18 November 2019

Perl Unlink - what is?

Perl Unlink:
perl unlink is used to remove the files. based on our requirements we can delete more than single file using unlink function.
by storing array or list elements  as filename we can achieve group of filenames deletion.


Cons:
1. Unlink will not notify which files could not remove. but it can be manageable using perl warning functions.
2. Unlink will not work on many os.
3. Super user permission necessary for Unlinking a directory although it is not recommended for usual coding.



Unlink Can be achieved by using following Methods


Method 1:[List of elements]

foreach ('a', 'b', 'c','d')
 {
      unlink $_ or die "Cannot unlink $file_to_remove: $!";
}

Method 2:[array of files]


@Files=('a', 'b', 'c','d');

foreach (@Files)
 {
      unlink $_ or die "Could not unlink $file_to_remove: $!";
 }

Method 3:


while () 
{
 unlink $_ or die "Cannot unlink $file_to_remove: $!";
}

Method 4:


$file = 'file.txt';

unlink $file_to_remove or die "Cannot unlink $file_to_remove: $!";

Method 5:


opendir H, '/home/kava' or die "Cannot open dir: $!\n";
foreach (grep(/FileRegex/i, readdir D))
 {
      unlink $_ or die "Cannot unlink $file_to_remove: $!";
 }

Method 6:


unlink glob "*.file"or die "Cannot unlink $file_to_remove: $!";

Method 7:


By using File::Find module.


kaavannan perl blogspot

Labels: ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home