Skip to content

Instantly share code, notes, and snippets.

@draegtun
Created November 17, 2010 16:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draegtun/703620 to your computer and use it in GitHub Desktop.
Save draegtun/703620 to your computer and use it in GitHub Desktop.
Expose any Perl object over the web. An interesting little snippet (using Plack)
#!/usr/bin/env perl
use 5.012;
use warnings;
use autobox::Core;
my $expose = []; # expose an Array object (singleton)
my $app = sub {
my ($func, @attrs) = $_[0]->{PATH_INFO}->split('/')->tail;
return if $func eq 'favicon.ico';
[ 200, [], [ $expose->$func(@attrs) ] ],
};
# to run: plackup -p 9292 webapp.psgi
# http://localhost:9292/push/1 -> 1
# http://localhost:9292/push/2 -> 12
# http://localhost:9292/push/3 -> 123
# http://localhost:9292/flatten -> 123
# http://localhost:9292/pop -> 3
# http://localhost:9292/shift -> 1
##
# see: "Expose any Ruby object over the web. An interesting little snippet"
# http://news.ycombinator.com/item?id=1910120
# https://gist.github.com/675667
# also:
# https://gist.github.com/703651 (Continuity version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment