• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

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!
0
nothing8171
Asked:
nothing8171
  • 3
1 Solution
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now