Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

a lil help on file extension

by Anonymous Monk
on Jun 09, 2003 at 21:17 UTC ( [id://264466]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello perl friends I have a lil question for everyone i am doing this in my code  if ( $file_names !~ /\.html/ || $file_names !~ /\.htm/ ) it checks for the .html and .htm pattern in $file_names which contains the filename.extension , it works if the file has an extension of .html but dosen't work if the file has a .htm format can anyone please help me thank you

Replies are listed 'Best First'.
Re: a lil help on file extension
by Mr. Muskrat (Canon) on Jun 09, 2003 at 21:26 UTC

    Try this on for size: if ( $file_names !~ /\.html?/ ) {

    Oh and you might want to anchor that at the end like this: if ( $file_names !~ /\.html?$/ ) {

    By the way, using !~ means that you are checking that it doesn't match. If you want to look for matches use =~ instead.

Re: a lil help on file extension
by sauoq (Abbot) on Jun 09, 2003 at 21:28 UTC

    You are asking for a file that "does not match this pattern or that does not match this other pattern." The only reason it works for files with a ".html" extension is that both patterns match. Try changing your or to an and if you don't want files with either extension.

    Of course, you can make it easier on yourself by saying exactly what you mean...

    unless ( $file_names =~ /\.html?/ ) { ... }

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: a lil help on file extension
by CukiMnstr (Deacon) on Jun 10, 2003 at 02:46 UTC
    ...and you probably want to stick a /i modifier, in case the extension is in uppercase, or mixed case:
    unless ( $file_names =~ m/\.html?$/i ) { ... }

    hope this helps,

Re: a lil help on file extension
by CombatSquirrel (Hermit) on Jun 10, 2003 at 11:46 UTC
    What the others pointed out so far were correct solutions of the problem, but I suppose you would like to know what went wrong with your code.
    Assume that the file does not end in .html; then $file_names !~ /\.html$/ evaluates to true. The or statement is now definitely true, because one of its arguments is true, so that the if-block is executed.
    What you meant probably was  if ( $file_names !~ /\.html$/ and $file_names !~ /\.htm$/ ). Try it; it should work for you.
Re: a lil help on file extension
by tall_man (Parson) on Jun 10, 2003 at 15:35 UTC
    Another way to do it is with the File::Basename module. For example:
    use strict; use File::Basename; my $file_name = $ARGV[0]; my @suffixlist = ("\.html", "\.htm"); my ($name, $path, $suffix) = fileparse($file_name,@suffixlist); if ($suffix ne "") { print "file name $file_name matched\n"; }
    It's overkill for this simple example, but it could be useful if you want to break up and test pieces of the file name and path in a portable way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://264466]
Approved by Mr. Muskrat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found