WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Advanced · question 56 of 100

Explain how to use Perl’s ’Mojolicious’ or ’Dancer’ web frameworks to develop web applications.?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

Mojolciious and Dancer are two popular Perl web frameworks that can be used for developing web applications.

Mojolicious is a full-stack web framework, while Dancer is a micro web framework.

To use Mojolicious or Dancer, you first need to install them, which can be done using the following commands:

For Mojolicious:

$ sudo cpan Mojolicious

For Dancer:

$ sudo cpan Dancer

After installation, you can create a new web application using the command line tool provided by the frameworks. For example, to create a new Mojolicious app, you can run:

$ mojo generate app MyApp

This will create a new directory called "MyApp" that contains the basic structure of a Mojolicious app.

For Dancer, you can use the ‘dancer‘ command to create a new app:

$ dancer -a MyApp

This will create a new directory called "MyApp" that contains the basic structure of a Dancer app.

Once you have created your app, you can start developing it by editing the source code files provided by the frameworks. Both Mojolicious and Dancer use a similar routing system, where you can define routes for handling requests to different URLs.

For example, in Mojolicious, you can define a route like this:

use Mojolicious::Lite;

get '/' => sub {
    my $c = shift;
    $c->render(text => 'Hello, world!');
};

app->start;

This code defines a route for the root URL ("/") that responds with the text "Hello, world!".

In Dancer, you can define a similar route like this:

use Dancer;

get '/' => sub {
    'Hello, world!';
};

dance;

This code defines a route for the root URL ("/") that also responds with the text "Hello, world!".

Both frameworks also provide support for templates, which can be used to generate dynamic HTML content. Mojolicious uses a built-in templating system called "Mojo::Template", while Dancer supports various templating engines such as Template Toolkit and Text::Xslate.

Here is an example of using Mojo::Template in Mojolicious:

get '/hello/:name' => sub {
    my $c = shift;
    $c->stash(name => $c->param('name'));
    $c->render(template => 'hello');
};

__DATA__

@@ hello.html.ep
<!DOCTYPE html>
<html>
<head><title>Hello <%= $name %>!</title></head>
<body>
    <h1>Hello <%= $name %>!</h1>
</body>
</html>

This code defines a new route that takes a URL parameter called "name" and uses it to render a template that says "Hello [name]!".

In Dancer, you can use a similar template like this:

get '/hello/:name' => sub {
    template 'hello', { name => param('name') };
};

template 'hello' => sub {
    my $vars = shift;
    "<!DOCTYPE html>n" .
    "<html>n" .
    "<head><title>Hello $vars->{name}!</title></head>n" .
    "<body>n" .
    "    <h1>Hello $vars->{name}!</h1>n" .
    "</body>n" .
    "</html>n"
};

This code defines a new route that takes a URL parameter called "name" and uses it to render a template that says "Hello [name]!".

In summary, Mojolicious and Dancer are two popular Perl web frameworks that can be used to develop web applications. Both frameworks provide a simple and easy-to-use routing system and support for templates for generating dynamic HTML content.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics