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

THis doesnt work, it says the error even if its right.
@edittypes = qw(.htm .html .txt);
sub edit { $file = $q->param('select'); foreach $line (@edittypes) { if ($file !~ /$line/) { &inerror("You can only edit @edittypes"); } } }

Replies are listed 'Best First'.
Re: Not equal, regular expressions
by RMGir (Prior) on Jun 25, 2002 at 15:43 UTC
    Right.

    Assume the parameter is "mike.txt". Then the regex will match for .txt, but fail for .htm and .html, so the error will get printed twice.

    If the parameter doesn't match any of those patterns, the error gets printed 3 times.

    You're also not checking to make sure the extension is at the end of the filename, so that would match "miketxt.exe" as well, since /.txt/ is "any character, followed by 'txt'".

    Please check out this discouraging webpage to get some other ideas of ways your script could possibly be abused...
    --
    Mike

Re: Not equal, regular expressions
by kvale (Monsignor) on Jun 25, 2002 at 15:46 UTC
    One problem might be that the `.' is a metacharacter in the regex. Try quoting it:
    if ($file !~ /\Q$line\E$/) {
    and see what happens.

    -Mark
Re: Not equal, regular expressions
by frankus (Priest) on Jun 25, 2002 at 16:06 UTC
    @edittypes = qw( htm html txt);
    $regex = join('|', map{"\\.$_" }@edittypes);
    
    $_ = $q->param('select');
    inerror("You can only edit @edittypes") unless /$regex/i;
    
    Using & will disable any proto you've got on a function.

    --

    Brother Frankus.

    ¤

Re: Not equal, regular expressions
by flounder99 (Friar) on Jun 25, 2002 at 16:30 UTC
    How about this?
    #assuming: #use strict; #$q is initialized #sub inerror is defined my @edittypes = qw(.htm .html .txt); my $regex = join "|", map {'\Q' . $_ . '\E'} @edittypes; my $file = $q->param('select'); unless $file =~ /(?:$regex)$/ &inerror("You can only edit @edittypes" +);

    --

    flounder

(tye)Re: Not equal, regular expressions
by tye (Sage) on Jun 25, 2002 at 16:48 UTC

    Works for me:     edit( "chtmatxt.html" ); gives me no error. Perhaps you meant something else? ;-)

            - tye (but my friends call me "Tye")