The code will look different depending on the webserver, fortunately there's Plack (an abstraction layer between webservers and applications) and it has middlewares which are software components that are executed before the application, one of them Plack::Middleware::Auth::Basic allows you to quickly implement an authorization layer.
Plack can be overwhelming at the beginning because it's radically different to everything else (in the Perl world), but definitely something worth any minute spend on it as it's the new black