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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |