Fetch File Extension of Perl Script

Discussion in 'Programming' started by onehundredandtwo, Aug 25, 2009.

  1. #1
    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? :confused:
    onehundredandtwo.
     
    onehundredandtwo, Aug 25, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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)
     
    kblessinggr, Aug 25, 2009 IP
  3. jumboa7

    jumboa7 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    jumboa7, Aug 26, 2009 IP
  4. onehundredandtwo

    onehundredandtwo Guest

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks, I just started using Perl, and I'm already starting to like it.

    Thanks heaps.:)
     
    onehundredandtwo, Aug 28, 2009 IP