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

Ok I gotta know how to do this I have these error statements

if(!$q->param('name') || !$q->param('site') || !$q->param('siteid') || + !$q->param('email') || !$q->param('pass') || !$q->param('repass') || + !$q->param('des')) { &error("You Forgot some required fields"); } elsif($q->param('email') !~ /^\S+@\S+\.\S+/g) { &error("Your email is in a invalid format"); } elsif($q->param('pass') ne $q->param('repass')) { &error("Your passwords don't match"); } else { }

In one of the elsif loops I need it too see if I siteid is taken, which is basically a directory, But how can I do that.
elsif($q->param('siteid') eq @directory) { }

How am I going to check this.
updated by boo_radley : title change

Replies are listed 'Best First'.
Re: How to check CGI params in if/elsif
by cLive ;-) (Prior) on Jun 11, 2002 at 09:28 UTC
    Err, just my .02, but I'd make it a little more readable, and instead of checking the e-mail regexp, get the user to enter their address twice, like the password. If they want to fake it they will, and double entry should pick up on typos (unless they're lazy like me and cut & paste :)

    Note - I've assumed here that &error returns and 'email2' is the second email field.

    my @required = qw(name site siteid email email2 pass repass); my $req_err; for (@required) { next if $q->param($_); $req_err++; last; } if ($req_err) { &error("You forgot some required fields"); } elsif ($q->param('email') ne $q->param('email2')) { &error("The two e-mail addresses you entered don't match"); } elsif($q->param('pass') ne $q->param('repass')) { &error("Your passwords don't match"); } else { # do whatever... }
    As for site ID, if it's in a particular directory, first check it fits required format, then test - eg, if siteid is meant to be an integer:
    $q->param('siteid') =~ /^\s*(\d+)\s*$/ my $site_id = $1 if ($site_id) { if (-d '/path/to/sites/'.$site_id) { &error("Directory already exists"); } else { # directory doesn't exist, so do whatever... } } else { &error("Invalid Site ID"); }
    If &error doesn't return, you can simplify further:
    my @required = qw(name site siteid email email2 pass repass); for (@required) { next if $q->param($_); &error("You forgot some required fields"); } $q->param('email') eq $q->param('email2') or &error("The two e-mail addresses you entered don't match"); $q->param('pass') eq $q->param('repass') or &error("Your passwords don't match"); my ($site_id) = $q->param('siteid') =~ /^\s*(\d+)\s*$/; $site_id or &error("Invalid Site ID"); -d '/path/to/sites/'.$site_id or &error("Directory already exists"); # directory doesn't exist, so do whatever...
    .02

    cLive ;-)

    --
    seek(JOB,$$LA,0);

      my @required = qw(name site siteid email email2 pass repass); my ($site_id) = $q->param('siteid'); my $req_err; for (@required) { next if $q->param($_); $req_err++; last; } if ($req_err) { &error("You forgot some required fields"); } elsif ($q->param('email') ne $q->param('email2')) { &error("The two e-mail addresses you entered don't match"); } elsif($q->param('pass') ne $q->param('repass')) { &error("Your passwords don't match"); } elsif (-d "/var/www/virtual/webewebin.com/home/$site_id") { &error("Directory already exists"); } else { }

      That worked just fine, if I would of done this
      $q->param('siteid') =~ /^\s*(\d+)\s*$/;

      Then it says everysite id exists, if I would of just did
      my ($site_id) = $q->param('siteid');

      It would work fine
Re: Help
by talexb (Chancellor) on Jun 11, 2002 at 04:08 UTC
    I have no idea how you're going to check for siteid.

    Where are the directories you want to check against? Are you checking for them locally, or are you looking to see if there's a match with one of the elements of your array @directory? You can do the first check with -e and the second using a foreach to loop through each of the elements and test for equality.

    I also notice that your E-Mail test would fail with any domain that has more than two parts .. better would be

    /^\S+@\S+(\.\S+){1,}/
    Suffice to say that it's impossible to write a regexp that parses E-Mail addresses.

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

      > I also notice that your E-Mail test would fail with any domain that has more than two parts ..
      Wrong, it would match because there is no anchor at the and of the regex. But the test is really bad anyway ...

      > Suffice to say that it's impossible to write a regexp that parses E-Mail addresses.
      See Jeffrey Friedl's regex that checks if an email address conforms to rfc822 published in Mastering Regular Expressions. IIRC that regex is about 6k characters long, it is also included in the module Email::Valid which I would recommend for checking email addresses.

      cheers,
      snowcrash
          > Suffice to say that it's impossible to write a regexp that parses E-Mail addresses.
          See Jeffrey Friedl's regex that checks if an email address conforms to rfc822 published in Mastering Regular Expressions. IIRC that regex is about 6k characters long, it is also included in the module Email::Valid which I would recommend for checking email addresses.
        Right, well of course, what I was implying was that it's impossible to write a *short* regexp that will accurately parse for a valid E-Mail address. Anything's impossible with enough code and horsepower to run it.

        I still maintain that in the context (code size, inferred experience level) of the original message, such a regexp is impossible.

        --t. alex

        "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

Re: How to check CGI params in if/elsif
by DamnDirtyApe (Curate) on Jun 11, 2002 at 05:58 UTC
    stat may be your friend here.
    # Untested elsif ( !stat( $q->param( 'pass' ) ) ) { # do something }
    Hope that helps.
    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: How to check CGI params in if/elsif
by alien_life_form (Pilgrim) on Jun 11, 2002 at 07:57 UTC
    Greetings,

    If you mean you have a vector of domain names stuffed in a directory, you can (lazily) do something like:

    my $siteid=$q->param('siteid'); if(!$siteid) { #complain } elsif(grep {$siteid eq $_} @directory) { # taken } else { mkdir 0755 $siteid or warn("Foo: $!") push @directory, $siteid; # or whatever }
    Where I cached $siteid to avoid a function call per comparison in the grep block.

    Cheers,
    alf


    You can't have everything: where would you put it?