Hello

I came across some code and I am grateful please, for some explanation on what the code is doing (for want of a better phrase). I am not understanding how it 'works', but it does. It is a cgi script. Opinions on the quality of it is not as important to me as is the desire to understand the principle of it.

The original is here: http://www.onlamp.com/2008/02/19/library; some code extract is below.

My initial ignorant questions are I suppose:

Generally an overview of what is 'going on' would help me a great deal, as I am struggling (apologies) to understand it.

Thank you. Habs.

extract code:

.... 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; } sub POST($$) { my ($path, $code) = @_; return unless $q->request_method eq 'POST'; return unless $q->path_info =~ $path; $code->(); exit; } sub PUT($$) { my ($path, $code) = @_; return unless $q->request_method eq 'PUT'; return unless $q->path_info =~ $path; $code->(); exit; } sub DELETE($$) { my ($path, $code) = @_; return unless $q->request_method eq 'DELETE'; return unless $q->path_info =~ $path; $code->(); exit; } ..... eval { .... 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:'); ...... }; .... GET qr{^/=/model/book/id/([\d-]+)$} => sub { my $id = $1; # Look up the resource file my $filename = get_local_path($id); if (-f $filename) { # Open and slurp up the file and output the resource ..... }; .... DELETE qr{^/=/model/book/id/([\d-]+)$} => sub { my $id = $1; # Make sure the book actually exists my $resource_path = get_local_path($id); unless (-f $resource_path) { barf 404, 'Where is What?', 'Nothing here to delete.'; } .... }; }; if ($@) { # Handle barfing if (ref $@ and reftype $@ eq 'HASH') { my $ERROR = $@; print $q->header( -status => $ERROR->{status}, -type => 'text/ +html' ); print $q->h1( $ERROR->{title} ); print $q->p( $ERROR->{message} ) if $ERROR->{message}; } # Handle anything else else { my $ERROR = $@; print $q->header( -status => 500, -type => 'text/html' ); print $q->title('Server Error'); print $q->p( $ERROR ); } exit; } # Nothing handles this, throw back a standard 404 print $q->header(-status => 404, -type => 'text/html'); print $q->h1('Resource Not Found');

In reply to 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.