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!
LVL 1
nothing8171Asked:
Who is Participating?
 
ozoCommented:
if( $MY_PATHNAME =~ /\.xml$/ ){ do somethine }
0
 
ozoCommented:
if( substr($MY_PATHNAME,-4) eq ".xml" ){ do somethine }
0
 
nothing8171Author Commented:
ozo, it works!!! Thanks!

what does that syntax mean?

=~
/\ - I assume ignore everything before the period.
$/
0
 
ozoCommented:
\.  matches a .
xml matches xml
$ matches the end of the string
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.