in reply to learning by example; please explain what this code is doing?
The request method is GET, and the path is "/=". You get the path by calling CGI method $q->path_infohttp://localhost/script.cgi/=
This could be written as:GET qr{^/=$} => sub { print $q->header('text/html'); print $q->h1('REST API Documentation'); print $q->p('Here is a list of what you can do:');
So it sends off the $regex and the $coderef to the GET() subroutine, which then checks if the request method is 'GET' and if the path matches the regular expression. In our case the path "/=" does match the regular expression {^/=$}, so it then calls the subroutine referenced in $coderef and then exits:my $regex = qr{^/=$}; my $coderef = sub { print $q->header('text/html'); # etc. }; GET( $regex, $coderef );
If the regex didn't match it would go on to the next test in the series, which would try to match the path to the regex {^/=/model/book/id/([\d-]+)$}$code->(); exit;
If we were to call the script with this URLGET qr{^/=/model/book/id/([\d-]+)$} => sub { my $id = $1; # etc.
then this regex would match, the code ref will be called and $id will contain the value "10566". If it didn't match the script will continue to test all the possible GET, POST, PUT and DELETE methods in the eval block until it finds a match. If it doesn't find one it returns a 404 Not Found response.http://localhost/script.cgi/=/model/book/id/10566
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: learning by example; please explain what this code is doing?
by Habs (Acolyte) on Mar 30, 2016 at 20:51 UTC |