Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by Corion (Patriarch)
on Mar 30, 2016 at 12:46 UTC ( [id://1159111]=note: print w/replies, xml ) Need Help??


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

You can reduce your script to a minimal example for learning:

use strict; use CGI '3.30', (); my $q = CGI->new; sub GET($$) { my ($path, $code) = @_; return unless $q->request_method eq 'GET' or $q->request_method eq + 'HEAD'; return unless $q->path_info =~ $path; $code->(); exit; } eval { GET qr{^/=$} => sub { print $q->header('text/html'); print $q->h1('REST API Documentation'); }; GET qr{^/=/model/book/id/([\d-]+)$} => sub { my $id = $1; # Look up the resource file my $filename = get_local_path($id); if (-f $filename) { .. }; }; }; if( $@ ) { print "There was an error."; };

Each call to GET gets passed a regular expression and a piece of code to call. The GET routine will then check whether the request matches that regular expression and if it does, call the callback to produce the result.

All of this is not related to the CGI module at all, except for where the request comes from.

To familiarize yourself with the code, I would start with (a copy of) the program and rip out as much of CGI as possible and run it from the command line. Add print statements to see where the code goes through and what it looks at.

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 13:05 UTC
    Hello Corion

    Thank you for that; very helpful and good point on slimming down the code.

    I am struggling to see what gets passed and from where, for those routines to get called; I will do as you say and get it to run from the command line and see if that helps me further appreciate.

    Regards

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found