Perler#2 has asked for the wisdom of the Perl Monks concerning the following question:

So ive been following the Beginning Perl (Curtis 'Ovid' Poe)tutorial found here, http://web.archive.org/web/20120624221651/http://ofps.oreilly.com/titles/9781118013847/the_interwebs.html, and ive run into a bit of a snag. whenever i try to run plackup on the command prompt it just gives me the error message, PSGI app should be a code reference: undef at C:/Dwimper/perlsite/lib/Plack/Middleware/Lint.pm line 13, i checked there and i think its failing because the app im trying to run does not equal a reference to some variable called 'CODE', but i cant understand why. here is a copy of the test app im trying to run

sub app { my $env = shift; return [ '200', [ 'Content-Type' => 'text/plain' ], [ "Hello World" ] ]; }

sorry if this is a stupid question or if there are any formatting errors its my first post

Replies are listed 'Best First'.
Re: plackup problem
by Your Mother (Archbishop) on Nov 10, 2013 at 00:51 UTC

    Just remove the sub name. Instead of returning a sub reference, you are declaring a sub. Since you aren’t using $env and the return is implicit, just as the the return of the sub is, you can even shorten it to this (again, some style room so do what you like or find most readable)–

    sub { [ 200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ] ]; }
Re: plackup problem
by three18ti (Monk) on Nov 09, 2013 at 22:47 UTC

    You're just declaring a subroutine, there's no $app variable, which is what plackup is looking for.

    Look at your example again:

    my $app = sub { return [ 200, [ 'Content-Type' => 'text/plain' ], ['Hello World'], ]; };

    Instead of declaring a subroutine, they are creating an anonymous sub and assigning it to the variable $app.

    To make your code work, try the following:

    my $app = \&app; sub app { my $env = shift; return [ '200', [ 'Content-Type' => 'text/plain' ], [ "Hello World" ] ]; }

    This version assigns a reference to the subroutine app to the variable $app, which is the code reference plackup is looking for.

      That works. But so does changing the $app line to this–

      my $snuffleupagus = \&app;

      From the docs–

      The last statement of app.psgi should be a code reference that is a PSGI application:

      The last statement isn't the sub declaration, it's the assignment. The variable name is meaningless. I personally prefer to just do an anonymous sub { … } as the last or only statement in this kind of test/one-off. But that’s only a style choice.