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.


In reply to Re: learning by example; please explain what this code is doing? by Corion
in thread learning by example; please explain what this code is doing? by Habs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.