Skip to content

Instantly share code, notes, and snippets.

@gideondsouza
Created September 9, 2013 14:57
Show Gist options
  • Save gideondsouza/6496750 to your computer and use it in GitHub Desktop.
Save gideondsouza/6496750 to your computer and use it in GitHub Desktop.
using LWP::Protocol::PSGI with dancer2
use LWP::UserAgent;
use LWP::Protocol::PSGI;
# can be Mojolicious, Catalyst ... any PSGI application
my $psgi_app = do {
use Dancer;#ATTENTION: changing this to Dancer2 makes this sample NOT WORK.
setting apphandler => 'PSGI';
get '/search' => sub {
return 'googling ' . params->{q};
};
dance;
};
# Register the $psgi_app to handle all LWP requests
LWP::Protocol::PSGI->register($psgi_app);
# can hijack any code or module that uses LWP::UserAgent underneath, with no changes
my $ua = LWP::UserAgent->new;
my $res = $ua->get("http://www.google.com/search?q=bar");
print $res->content; # "googling bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment