That is, let's see what you have to do, download, where to write, how to run and everything.
How to install Perl
First step is obviously installing Perl.If you are using a Unix system such as Linux, it already has Perl installed by default.
Linux is by far the best operating system for programmers. It's not difficult or complex, nor does it have to be a genius or hacker to use Linux.
In fact, I find it even easier and more intuitive to use Linux.
If you want to take the programming profession seriously, I suggest you install some Linux distribution, such as Ubuntu, and start learning.
Remember, everyone knows how to use Windows or Mac, there's nothing much about it, even my grandmother uses it.
But knowing how to use Linux is a tremendous and interesting differentiator, it will automatically put you in front of so many programmers and IT professionals.
Open the command terminal and type: perl -v
And it will open the version of your Perl.
For those who use another platform, such as Windows, visit the official website of the language:
https://www.perl.org/
Then go to download and get started.
Choose your platform.
If it's Windows, you'll probably have two options: ActiveState Perl and Strawberry.
Choose the Strawberry version.
Download the executable, wait for the download, run as administrator and install Perl.
How to Program the First Perl Script
Open a text editor, it can be notepad.If you prefer, we even suggest using some Notepad ++ or Sublime , as they will make your code better formatted, organized, colorful and such, a beauty only.
Enter the simple command:
print "Hello, world \n";
Now save your file with the name: hello.pl
It is very important to have the extension pl, from Perl, ok?
Beware that sometimes notepad may want to save as: hello.pl.txt
Do not let it happen.
We suggest saving to folder: C:\perl
So your script address will be: C:\perl\hello.pl
How to run scripts in Perl
Open the command terminal, MS DOS or Linux shell.If you are on Windows, type:
cd C:\perl
On my Linux, I saved in:
/home/user/perl
Now that it is in the script folder/directory, give the command Perl interpret:
perl hello.pl
If not working test: perl -w hello.pl on Windows
The result is this:
And ready, the message will appear on the screen: Hello, world
If you did it, you're already officially a Perl programmer, after all, you've already made and run your first script.
The hash-bang line in Linux and Windows
If you are using a Unix system such as Linux or Mac, it may be interesting to make your Perl script executable.Come on, first make your hello.pl script executable:
chmod +x hello.pl
Now run it:
./hello.pl
And see that there will be an error.
This is because the terminal will try to read and interpret the content, and will use (usually) bash to interpret these commands.
But we don't want the shell to interpret this, but Perl.
For that, we use the very first line of Perl code, the hash-bang command, see how our code looks:
#!/usr/bin/perl print "Hello, world!\n";We added to the script the code: #! / Usr / bin / perl (called hash-bang)
Ready. The first line tells the shell interpreter that we want Perl to interpret the script!
In other plataform, like Windows, the shebang would be:
#!C:\perl\bin\perl.exe print "Hello, world!\n";Now try to run and see the message "Hello world!" beautiful on the command terminal screen.
Sublime Text IDE
Now that you have learned how to install, program, and run our scripts, let's learn a 'trick' that uses an IDE, or a cuter program to program.We will use Sublime Text. Search for it on Google and choose to use, download and install.
Open the Sublime Text program and go to:: Tools -> Build System -> New Build System
Type the following command in the file that will appear:
{
"cmd": ["perl", "-w", "$file"],
"file_regex": ".* at (.*) line ([0-9]*)",
"selector": "source.perl"
}
Save like: "Perl.sublime-build" in "~/.config/sublime-text-3/Packages/User" Sublime folder.
Now open a new file, save as hello.pl and do control + B to run the script.
The result will appear below.
See how cute and clean the program is, it will help you indent the code (give spacing and line breaks correctly), as well as color commands, strings, etc., and run the code directly there without having to give any commands.
A real hand on the wheel programming in Perl in the Sublime Text.
No comments:
Post a Comment