-3

This is part of a school project. I cannot figure out what's the problem in my regexes. I have more that work but these are giving me a hard time. Apache doesn't tell you exactly where you went wrong.

First and Last name must be two simple names and output in Lastname, Firstname format

my $name = param('name');

if($name =~ {2}) {
    print "Name will be stored as $2, $1<br/><br/>";
} else {
    print "Bad name. Enter just two names, your first and last<br/><br/>";
}

Password must be in this order of regexes. Begin with a single upper case character, 2 digits, a single space, 2-3 lower case letters, one special character (not a letter or digit).

my $password = param('password');

if ($password =~ /[A-Z]+\d{2}+\s+[a-z]{2,3}+-]\^$/) {
print "Password $password was accepted<br/><br/>";
} else {
print "Bad password, $password was not accepted<br/><br/>";
}
Laura Lee
  • 1
  • 2
  • 1
    Check for `+` in your regex, what should they do? – mpapec Nov 10 '16 at 17:38
  • I realize this is a class assignment, but [password checkers like that are obsolete](http://stackoverflow.com/a/39710490/14660). – Schwern Nov 10 '16 at 17:46
  • @Schwern I have no control of the requirements :( `($password =~ /[A-Z]+\d{2}+\s+[a-z]{2,3}+-]\^$/)` The last part needs to be a special character, I think everything else looks good. – Laura Lee Nov 10 '16 at 18:03

2 Answers2

6

Apache doesn't tell you exactly where you went wrong.

First, find your Apache error log. It will contain the actual error. I can't tell you where it is, but I'd start with /var/log.

Second, debugging code through a web server just makes things more difficult. You're probably using CGI.pm which can accept arguments on the command line for debugging.

perl /path/to/your/program name='Michael Schwern'

Second, turn on strict and warnings. They will point out typos and silly mistakes like this one...

$ perl -w ~/tmp/test.plx name=foo
Odd number of elements in anonymous hash at /Users/schwern/tmp/test.plx line 5.
Bad name. Enter just two names, your first and last<br/><br/>

That's this.

$name =~ {2}

That says to make an anonymous hash with the key 2 and an undefined value. Then stringify it to something like HASH(0x7fca01805668) and then use that as a regex. In other words: nonsense.

What you're looking for is something like this that looks for two words separated by some spaces.

$name =~ m{^(\w+)\s+(\w+)$};

Read the Perl regex tutorial for more info.

Community
  • 1
  • 1
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Why it should not.? `use CGI::Carp('fatalsToBrowser')` – mkHun Nov 10 '16 at 17:53
  • 1
    @mkHun Yup, you can use that, too. You might also want `warningsToBrowser`. It's not my first choice because A) people forget and leave it on in production (I wish there was an environment variable) and B) getting the error message is only part of the reason it's good to be able to run your web code locally from the command line and C) apparently there's a problem with mod_perl 2. – Schwern Nov 10 '16 at 18:53
2

It's hard to tell what you're trying to do, and getting other people to do your homework for you won't teach you a thing

$name =~ {2}

isn't a regular expression at all: you're building an anonymous hash { 2 => undef } and using its stringified reference as a pattern. It will be something like HASH(0x71c328) so that isn't going to work

And this one is incomprehensible

$password =~ /[A-Z]+\d{2}+\s+[a-z]{2,3}+-]\^$/

It will match something like A99 aaa-]^, but I doubt if that is what you want. What is the plus sign + for after {2,3}?

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • `$name =~ {2}` for 2 inputs (first and last) `$password =~ /[A-Z]+\d{2}+\s+[a-z]{2,3}+\-]\^$/` would that be more like it? It needs to be more like `M12 abc@` – Laura Lee Nov 10 '16 at 17:42
  • 2
    @LauraLee: But that isn't a regex pattern. It's not even sensible Perl syntax. – Borodin Nov 10 '16 at 17:43