Checking what the file extension is in a perl script.

Posted on 2006-04-11
Last Modified: 2010-03-05
Hello everybody,

I am writing a perl script and within it I have a variable with a path and file name in it.  For example:

$MY_PATHNAME = /myprogs/abc.xml

However, when this perl script executes sometimes the (.xml) extension will be different.  Say something like, (acb.txt).

How can I write an "if" statement in perl that only compares the extension.....nothing preceding it (pathname and filename).

if ($MY_PATHNAME eq "*.xml")
  do somethine

BTW, the above won't work, but just wanted to give you an idea of what I am doing.

Question by:nothing8171
    LVL 84

    Accepted Solution

    if( $MY_PATHNAME =~ /\.xml$/ ){ do somethine }
    LVL 84

    Expert Comment

    if( substr($MY_PATHNAME,-4) eq ".xml" ){ do somethine }
    LVL 2

    Author Comment

    ozo, it works!!! Thanks!

    what does that syntax mean?

    /\ - I assume ignore everything before the period.
    LVL 84

    Expert Comment

    \.  matches a .
    xml matches xml
    $ matches the end of the string

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now