Extract filename from full path using perl

By: Sam Chen Viewed: 153325 times  Printer Friendly Format    


This is a simple trick but comes in handy when you need to extract just the filename from the full path.

#!/usr/bin/perl -w
use strict;
my @temp;
my $string;
my $filename;
$string = "/var/spool/email2fax/1233123/Agoda-Booking-Sep-2012.tiff";
for ( reverse @{[ split( '', $string ) ]} )
{
   last if /\//;
   unshift @temp, $_;
}

$filename = join '', @temp;
print $filename;
exit;


Most Viewed Articles (in Perl )

Latest Articles (in Perl)

Comment on this tutorial