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

Alright... I have a search form and I need the text box where the user types in a 'keyword' to default to a value of "any character"... this way if the user doesn't type anything in the text box it will still search for any character... Here is a piece of the code I am currently using...
<input type="text" name="searchstring" size="18"> if ($desc =~ /$form{'searchstring'}/i) {
The $desc variable is where the code will search the data... I'm thinking I need something like this
$form{'searchstring'}/i = [a-zA-Z] else $desc =~ /$form{'searchstring'}/i
But that doesn't seem to work... any suggestions???

________SysAdm

Replies are listed 'Best First'.
Re: Giving text box a hidden value
by arturo (Vicar) on Mar 13, 2001 at 21:40 UTC

    How about:

    $form{searchstring} = param('searchstring') || 'any character';
    ?

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Giving text box a hidden value
by fpi (Monk) on Mar 13, 2001 at 22:08 UTC
    How about something like this?
    unless ($form{'searchstring'}) { $form{'searchstring'}= '[a-zA-Z]'; } if ($desc =~ /$form{'searchstring'}/i) { ... }

    It's unclear from your code, but I assume you know that this has to be done in the post-processing of the form, and not at the same time you are asking the form....

    Added:
    You could always do something like this when you are asking the form: <input type="text" name="searchstring" size="18" VALUE="any character"> Of course, "any character" will appear in the search box every time, and the program will default to searching for "any character" unless you add something like this:
    if ($form{'searchstring'} eq "any character") { $form{'searchstring'}= '[a-zA-Z]'; }
Re: Giving text box a hidden value
by McD (Chaplain) on Mar 14, 2001 at 00:00 UTC
    Related tidbit - be careful of using a regular expression provided by a user, it could blow up on you.

    Consider using an eval as a safety:

    eval { $desc =~ /$form{'searchstring'}/i } die "Invalid search pattern: $@" if $@;
    Peace,
    -McD