Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: learning by example; please explain what this code is doing?

by tangent (Vicar)
on Mar 30, 2016 at 19:05 UTC ( #1159162=note: print w/replies, xml ) Need Help??

in reply to learning by example; please explain what this code is doing?

This code is handling and routing CGI requests based on the path (the bit that comes after the script name in a URL). It seems well thought out and covers all possible error cases. However, it is written in a style which is alien to me, and I think most monks here would reccommend against following this style.

Let's say we call the script like this:
The request method is GET, and the path is "/=". You get the path by calling CGI method $q->path_info

The script will call a series of subroutines until it finds a match for the request method and path. In the eval{} block you see this series of calls. One of them is:
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:');
This could be written as:
my $regex = qr{^/=$}; my $coderef = sub { print $q->header('text/html'); # etc. }; GET( $regex, $coderef );
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:
$code->(); exit;
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-]+)$}
GET qr{^/=/model/book/id/([\d-]+)$} => sub { my $id = $1; # etc.
If we were to call the script with this URL
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.

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

    Hello Tangent

    Many thanks for that. I wish I'd checked back sooner to read your post. As it happens I have spent my evening working out to the same as what you have described (well nearly).

    When you say 'alien style', what do you mean and could you give me an example please (unless the 'could be written as' was an example) of a non-alien approach ?

    To someone who has not experienced Perl, some of the 'folded' code (or is that better worded as more concise, efficient, performant, proficient - I do not know) can make it difficult to read/understand. Therefore I like the alternative you presented in the 'could be written as' piece - that to me is much more presentable if nothing else.

    I have coded in the past (not in Perl), but not to any advanced levels, so is the approach of [crudely dropping out?] 'exit-ing' from a program in a sub routine acceptable ?

    Lastly, for what it is as such, are there 'better' coding approaches to achieve what is being done, or is it fairly decent technique (accepting there is no security etc)?

    Thank you for taking time to respond; thank you to all - I have moved on a little and the references given to me earlier are proving enlightening, if a little challenging also - particularly one that led to a piece that touched on lexical code referencing (lambda style) coupled with OO style in relation to anonymous subs! - well I think that was what it was about :-)

    Perl is *very* flexible! That would seem fair to say.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1159162]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-05-17 18:49 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (68 votes). Check out past polls.