Link to home
Start Free TrialLog in
Avatar of nothing8171
nothing8171

asked on

Checking what the file extension is in a perl script.

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.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if( substr($MY_PATHNAME,-4) eq ".xml" ){ do somethine }
Avatar of nothing8171
nothing8171

ASKER

ozo, it works!!! Thanks!

what does that syntax mean?

=~
/\ - I assume ignore everything before the period.
$/
\.  matches a .
xml matches xml
$ matches the end of the string