Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

Perl Dancer #3 – Hello World


On this video first we verify what occurred to the set up after which if it was profitable then we’re going to write our first Dancer-based net utility.

Then I can go to the Command Immediate or the Energy Shell if I used to be on Home windows, or if I used to be on Linux or Mac I might go to the terminal and sort within the following:

perl -MDancer2 -e1
Enter fullscreen mode

Exit fullscreen mode

This may run perl. *-MDancer2 will make it attempt to load the Dancer2 module into reminiscence and -e1 simply tells perl to execute this system
consisting of the quantity 1 which is a non-operation. It mainly tells perl to do nothing.
The objective right here is to see if perl can discover and cargo the Dancer2 module.

If there isn’t a output to the display which means all the things is ok. Not like within the earlier case when this command failed.

This, not having any output, implies that Dancer2 is put in.

If, nevertheless, we strive

perl -MDancer3 -e1
Enter fullscreen mode

Exit fullscreen mode

We’ll get one thing like:

Cannot find Dancer3.pm in @INC (chances are you'll want to put in the Dancer3 module) (@INC incorporates:
/dwelling/gabor/perl5/lib/perl5/5.34.0/x86_64-linux-gnu-thread-multi
/dwelling/gabor/perl5/lib/perl5/5.34.0
/dwelling/gabor/perl5/lib/perl5/x86_64-linux-gnu-thread-multi
/dwelling/gabor/perl5/lib/perl5
/and so on/perl
/usr/native/lib/x86_64-linux-gnu/perl/5.34.0
/usr/native/share/perl/5.34.0
/usr/lib/x86_64-linux-gnu/perl5/5.34
/usr/share/perl5
/usr/lib/x86_64-linux-gnu/perl-base
/usr/lib/x86_64-linux-gnu/perl/5.34
/usr/share/perl/5.34
/usr/native/lib/site_perl).
BEGIN failed--compilation aborted.
Enter fullscreen mode

Exit fullscreen mode

This failed clearly as a result of Dancer3 isn’t put in as a result of it doesn’t even exist.

It instructed me that it will possibly’t find Dancer3 and that I ought to set up it.

After all perl doesn’t know that my downside right here isn’t the shortage of set up, however that I made a mistake (on function) within the title of the module.



Course slides

The opposite factor that we left on the finish of the earlier video was the unzipping of the course materials.
If you happen to change to the opposite window – the file explorer – the place you began the unzip, you may see a folder (listing) known as slides-main.

Throughout the slides-main one can find a folder for every coaching course I’ve.

The Dancer2 course materials is inside the perl course and thus within the perl folder.

Throughout the perl folder you may discover the instance recordsdata from the slides by following the trail which you can see above every instance on the slides.
For instance here.

Then I switched to the Command Immediate/Terminal and typed in cd (change listing) after which the trail the place I wished to change to.

I can go to the file-explorer, click on on the “deal with bar” after which it’s going to present me the total path to the listing I’m viewing.
I can copy that by urgent Ctrl-C then I can paste it to the terminal window my clicking on the best button of the mouse.

hen I can press the House key to leap to the start of the road and sort in cd.

cd c:courseslides-mainperlexamplesdancerhello_world
Enter fullscreen mode

Exit fullscreen mode

By urgent ENTER I will bounce to the chosen folder.

If now I kind in

dir
Enter fullscreen mode

Exit fullscreen mode

I can see the recordsdata which can be in that listing.

... app.psgi
... check.out
... check.t
Enter fullscreen mode

Exit fullscreen mode

Switching to the file-explorer I can see the identical.

Among the many 3 recordsdata within the listing: app.psgi is the file that we’ll really want now. That is the place our “utility” is.
check.t is the check file that’s checking that our code works correctly.

Then you definately’ll discover all types of additional recordsdata with .out extension that you just will not have in an actual utility.
I solely have them to retailer the output of varied instructions so I can embody it within the slides.
You do not have to fret about these recordsdata.



Whats up World

The content material of the app.psgi file may be seen right here:

package deal App;
use Dancer2;

get '/' => sub {
    return 'Whats up World!';
};

App->to_app;
Enter fullscreen mode

Exit fullscreen mode

What it’s essential to do at this level is kind within the Command Immediate:

plackup
Enter fullscreen mode

Exit fullscreen mode

It would print one thing like this on the display:

HTTP::Server::PSGI: Accepting connections at http://0:5000/
Enter fullscreen mode

Exit fullscreen mode

This tells us that the server is operating and accepting connection on port 5000.

If you’re utilizing Home windows, you may in all probability additionally see a pop-up of the Home windows Defender Firewall saying that it has blocked
some options of this app.

This occurs when you’ve got the Home windows Firewall in your pc that may restrict the entry to your pc.

It has two checkboxes, one to allow entry on Personal networks, the opposite to allow entry on Public networks

For some purpose on my pc the Public networks was checked. I favor to solely enable entry on Personal networks so I checked that field
and unchecked the field of the Public networks.

Then I clicked Enable entry. It took a few seconds to make the pop-up disappear.
It might need been sluggish as a result of I used to be recording the video on a Home windows operating in a Digital Field whereas the video-recording was operating.
So my pc was already busy.

Then I can open my browser to the URL that was listed on the terminal window: http://0:5000/. (you need to be capable of simply click on on this hyperlink)

If that doesn’t work you may also attempt to kind in http://127.0.0.1:5000/ which goes to “localhost” which is your pc.

Lastly you possibly can strive http://localhost:5000/.

Within the video you possibly can see that I attempted all of those utilizing the browser that got here with Home windows (I believe it was Edge).



Set up Firefox or Chrome

So as a result of I couldn’t persuade Edge to work I made a decision to obtain Firefox. Which is Open Supply so I prefer it.

Whereas I used to be ready for Firefox to be put in I attempted Edge once more and this time it labored.

Both I did one thing incorrectly earlier or Edge simply obtained scared when it noticed I’m downloading Firefox and began to work.



Lastly it really works!

So we are able to see within the browser Whats up World1. It is not a lot of an internet utility, nevertheless it works and it’s ours!

Within the terminal/Command Immediate window you possibly can see the entry log. Apparently additionally the earlier makes an attempt confirmed up though we didn’t get a response within the browser.



The code

Let’s go over the code now.

package deal App;
Enter fullscreen mode

Exit fullscreen mode

That is simply the declaration of the Perl package deal. The title App is not actually necessary right here. You possibly can put there just about something.
So long as on the finish of the code you utilize the identical title.

use Dancer2;
Enter fullscreen mode

Exit fullscreen mode

This masses the Dancer2 module. (That is what the -MDaner2 did on the command line.)

get '/' => sub {
    return 'Whats up World!';
};
Enter fullscreen mode

Exit fullscreen mode

That is known as a route.

The best way Dancer works is that we have to map URL pathes to nameless capabilities. (You could possibly additionally use named capabilities, however they’re often nameless.)

The “path” is the half that comes within the URL after the title of site. So in case of this URL: “https://code-maven.com/slides/dancer/hello-world-with-dancer
The half “/slides/dancer/hello-world-with-dancer” is the trail.

In case of “http://localhost:5000/” the trail is simply “https://style-tricks.com/” although once I typed it within the browser hid it.

I may additionally kind in some longer path, for instance I attempted “http://localhost:5000/hello” and I obtained a Error 404 - Not Discovered message.
That is as a result of the trail /hi there has not been outlined on this utility.

After the get key phrase is the trail we’re mapping.

After the fat-arrow => you possibly can see the nameless subroutine. No matter it returns is distributed again to the browser.

It could actually return some HTML to make a pleasant web page, however in our case we solely returned the plain textual content “Whats up World!” and the browser displayed it.

The code with the fat-arrow would possibly look a bit unusual, particularly if you recognize Perl, however it’s mainly only a key-value pair
the place the trail is the important thing and the nameless perform is the worth.

Simply remember the semi-colon on the finish of that block! This is among the instances when after the closing of curly braces you need to put a semi-colon
as a result of it is a assertion and never only a block.

The identical as this code written barely in a different way would possibly assist you perceive what’s actually occurring right here.

get('/', sub {
    return 'Whats up World!';
});
Enter fullscreen mode

Exit fullscreen mode

We imported the get perform from the Dancer2 module. This perform will get two parameters. The primary one is the trail and the second is the perform to be executed when somebody requests that path.



Cease the applying

To cease the applying it’s essential to change to the terminal/command immediate once more and press Ctrl-C. Home windows will ask you for affirmation. Kind Y.



Testing

Then stays the query, how do we all know that this really works. With out manually beginning the entire thing after which visiting the site?

That is what we’ll see within the subsequent video.

Add a Comment

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?