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!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if( substr($MY_PATHNAME,-4) eq ".xml" ){ do somethine }
ASKER
ozo, it works!!! Thanks!
what does that syntax mean?
=~
/\ - I assume ignore everything before the period.
$/
what does that syntax mean?
=~
/\ - I assume ignore everything before the period.
$/
\. matches a .
xml matches xml
$ matches the end of the string
xml matches xml
$ matches the end of the string