Hi all, I'm creating a Perl-based site and I was just wondering if anybody knew how to find the current file's extension in perl? In PHP I just used $phpEx = substr(strrchr(__FILE__, '.'), 1); Code (markup): - but searching the internet didn't prove successful. Any ideas? onehundredandtwo.
I think PERL's biggest strength is in the use of regular expressions. an example: #!/usr/bin/perl while (<DATA>) { print "$1\n" if m/((\.tar)?\.[a-z]+)$/; } __DATA__ somefile.txt some.file.txt somefile..text somefile.tar.gz somefile Code (markup): The loop above would print the extension of each file name (the only special condition is that if its a tar.gz/tar.gz, etc it'll grab the tar part as part of the entire extension)
Hmm, if you want to get current file name, you should use variable $0 (similar to __FILE__ in php) Combine with kblessinggr code, it will look like this. $filename = $0; $filename =~ m/((\.tar)?\.[a-z]+)$/; print "$1\n"; HTH