Use perl in linux

Use perl in linux

Perl Tutorials — Herong’s Tutorial Examples — v6.01, by Dr. Herong Yang

∟ Running Perl Scripts on Linux Systems

This section provides a tutorial example on how to run Perl scripts on Linux systems. To make a Perl script file executable, you need to add ‘#!/usr/bin/perl’ to the beginning of the script.

There are many ways to run Perl scripts on Linux:

1. Run the «perl» command with the Perl script included in the command line. For example, enter the following command line in a shell window:

/home/herong$ perl -e "print 'Hello world!';" Hello world!

Note that about command will not work in some shell windows. For example, in a «bash» shell window, you will get the following error, because the «!» is a reserved character to access an event in the command history.

/home/herong$ perl -e "print 'Hello world!';" -bash: !': event not found

To avoid the problem, you can run the above command a «sh» command window:

/home/herong$ sh $ perl -e "print 'Hello world!';" Hello world!$

Or you can protect «!» using escape sequence:

/home/herong$ perl -e "print \"Hello world\!\n\";" Hello world!

Or you can use single quotes (‘. ‘) to protect the entire Perl script from the shell:

/home/herong$ perl -e 'print "Hello world!\n";' Hello world!

Here is another example of running a Perl script in a single command line:

/home/herong$ perl -e "for (\$i=0; \$i" 0 1 2 3 4 5 6 7 8 9

Note that dollar sign ($) used for Perl scalar variables are also shell reserved characters, we need to use escape sequences to protect them.

Also note that double quote («) is used to put the entire script code as one command line parameter. Any double quote inside the program needs to be protected as (\»).

Читайте также:  Настроить локальную сеть linux windows

Or you can use single quotes (‘. ‘) to protect the entire Perl script from the shell:

/home/herong$ perl -e 'for ($i=0; $i' 0 1 2 3 4 5 6 7 8 9

Including Perl script in the command line is quick and easy. But you can only run scripts that are small enough to fit into one command line.

2. Run the «perl» command with the Perl script supplied from the standard input stream. For example, enter «perl» in a command window. Then enter the script code followed by Control-D, which is the End Of File (EOF) indicator:

/home/herong$ perl $s=0; for ($i=0; $i print "$s\n"; ^D 45

Obviously, you can enter a much longer script in this way. But the program is not save permanently.

3. Run the «perl» command with the Perl script supplied in a file. For example, enter the following Perl script in a file called hello.prg:

Then enter the following command in a command window:

/home/herong$ perl hello.prg Hello world!

4. Run Perl script files as commands. You can do this, only if you insert a special line at the beginning of your script file: #!/usr/bin/perl, and assign execution permission to the script file. This special line represents the Perl installation location on the file system.

For example, enter the following script in a file called hello.pl:

#!/usr/bin/perl print "Hello world!\n";

Then assign execution permission and enter the script file name to run it:

/home/herong$ chmod a+x hello.pl /home/herong$ ./hello.pl Hello world!

It works! And this is the best way to run Perl scripts on Linux systems.

Источник

How to Run Perl scripts in Linux Command Line

Learn to learn Perl scripts in Linux command line. Also learn a few tricks about running Perl commands without saving them in files.

Читайте также:  Calculate linux use флаги

Call them ancient but Perl scripts are still a great way of scripting automation tasks for various sysadmin tasks.

If you are new to it and wondering how to run Perl scripts in the Linux terminal, just use this command:

This will work even if the script file doesn’t have the execute permission.

You may also run it like a bash script:

But for that to work, the file must have the execute permission and the script must begin with the hashbang:

As long as the syntax is right and Perl installed on your system, you should be able to run Perl scripts easily.

How do you know if Perl is installed on your system? Use this command and if you see the version details, you should be good.

Check installed verison of Perl

If your system throws an error like «Perl command not found,» use your system’s package manager to install it first.

There are other ways of using Perl in Linux. You can write a Perl script directly in the terminal and run it immediately, without even saving it.

Let’s see things in a bit more detail.

Executing Perl scripts from the files

This is the recommended way to create and execute Perl scripts. You have the scripts saved and you can use them in an IDE like Eclipse if the script is complicated or part of a larger Perl project.

Perl file does not need to have any extension, but a «.pl» extension is good practice.

The recently created file will be empty, I’ll be using Nano editor to modify the empty file.

My sample script looks like this:

#!/usr/bin/perl print "Enter your name: "; $name=; print "Hello, $ . you will soon be a Perl addict!"; print "\n";

Modifying Perl script in nano text editor

Save and exit from the Nano editor using the Ctrl+X key.

Читайте также:  Run it as root kali linux

No need to give execute permission to the script. Use it like this:

Execute Perl script from file

You can also give the script execute permission and run it like this:

That’s easy, right? But if your aim is to just check a Perl syntax, you don’t need to create and save scripts.

Execute a single line of Perl command in Linux

This is a quick method for quickly checking Perl syntaxes.

To run your Perl command, you just have to follow the given syntax:

You just have to pay attention to the quotes in bash shell.

perl -e 'print "Hello world!\n";'

Execute Perl script with single quotes to avoid issues with bash

It works with a single line of Perl code. You can pass multiple lines with multiple -e options. However, there is a better way of writing multiple lines of Perl code without saving them in a file.

Executing Perl commands without saving them in a file

This method is a bit similar to what you saw above, but it is less complicated.

Start the Perl prompt with the given command:

Then you write your desired script. Once you’re done writing, you must press CTRL + D to indicate the prompt End of File (EOF). The Perl script will be executed immediately as you press Ctrl+D.

For instance, I want to run the following script:

print "Enter your name: "; $name=; print "Hello, $ . you will soon be a Perl addict!"; print "\n";

This simple script is supposed to take input from the user (name in this case) and will return something like this:

Execute Perl script in Perl prompt

As you can see, running Perl scripts in Linux is not much different than running a bash shell script. Perl is a powerful scripting language and though Python has gained popularity, seasoned sysadmins still use it for complex scripting. Enjoy.

Источник

Оцените статью
Adblock
detector