--- Konstantine <[EMAIL PROTECTED]> wrote:

> Greetings
> 
> This is my first stab at Perl. Please be gentle :-)
> 
> I have the following snippet which seems to do what I am trying to
> do,
> namely, check the url field value in a POST request, do a server
> redirect (as opposed to browser redirect) if the value is in
> safe_destinations array, redirect to start.htm if not.
> 
> #!/usr/bin/perl -wT
> #
> use strict;
> use CGI qw(:standard);
> my @safe_destinations = ('a.htm', 'b.htm', 'c.htm');
> my $destination_url = '/start.htm';
> if(param()) {
>       foreach (@safe_destinations) {
>               if (param('url') eq $_) {
>                       $destination_url = '/' . param('url');
>                       last;
>               }
>       }       
> }
> print redirect($destination_url);
> 
> I have two questions
> Is there a function to check if an item is in an array, instead of
> looping through the array? Can you see a problem with the snippet?
> Many thanks for your time.

Typically this is done by putting the array elements in a hash.  Here's
 how one might write this:

  my %safe_destination 
    = map { $_ => 1 } ('a.htm', 'b.htm', 'c.htm');
 
  my $destination_url = '/start.htm';
  if ( my $url = param('url') ) {
      if ( exists $safe_destination{$url} ) {
          $destination_url = "/$url";
      }
  }

Since you're new to Perl, the map statement will look weird.  You might
find it clearer to do this (and this will create the same data
structure as the map statement):

  my %safe_destination = (
    'a.htm' => 1,
    'b.htm' => 1,
    'c.htm' => 1,
  );

In this case, the values are irrelevant.  We're just using a hash as a
lookup table.

Cheers,
Ovid

-- 
If this message is a response to a question on a mailing list, please send 
follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to