1. Perl
  2. here

What can Perl do? - Comparison with Ruby, Python, PHP

"What can Perl do?" Many programming languages have appeared, and you may be wondering which programming language to choose. I've created content that answers what you can do with Perl.

Good at text processing using a regular expression such as search/replace

Perl is a scripting language that excels at text processing such as search and replace. Regular expressions are built into the language, making it easy to write search and replace programs.

# Replace all "banana" with "orange"
$message =~ s/banana/orange/g;

The processing speed of text processing is high. It depends on the processing content, but the experience is that a Linux server that can be rented at a low price can process 100,000 lines of text in about 1 second.

Perl a regular expression are widely known for their originality and usefulness, and can also be used as the "-P" option of the Linux grep command.

# You can use Perl a regular expression with the "-P" option of the Linux grep command
grep -P'(foo | bar) $'file

In addition to Perl, Ruby is another programming language that incorporates a regular expression.

Easy to write because you don't have to be aware of the type

In Perl, you don't have to be aware of the types of strings and numbers, so it's easy to write. Consider the case where command line argument gets a number as string to watch. In Perl, you can calculate as it is without being aware of the type.

# Command line arguments
my ($num1, $num2) = @ARGV;

# Calculate without being aware of the type
my $total = $num1 + $num2;

Easy to copy and paste

Perl is designed to make copy and paste easy to write. Easy copy and paste reduces the risk of accidentally editing the original data, and above all, it's easy.

String list operator

String list operator makes it easy to array can be.

For example, suppose you want the following text to be an array right away.

cat
dog
mouse

You can use the string list operator to quickly copy and paste into an array.

my @animals = qw(
cat
dog
mouse
);

Data section

You can copy and paste line by line using a feature called the data section.

For example, suppose you want to process the following text line by line:

<pre>
Hello World
Perl Book
Web Development

The data section allows you to copy and paste, line by line, right away.

while (my $line = <DATA>) {
  # Process line by line
}

__DATA__
Hello World
Perl Book
Web Development

Change the enclosing character

In Perl, use double quote operator and quote operator to describe a string. You can change the enclosing character or use the "m" notation to change the enclosing character in a regular expression. Therefore, you can copy and paste the original data without changing it.

# Double quote operator
my $name = qq (aaa "bbb);

# Quart operator
my $name = q (aaa'bbb);

# Change the enclosing character using m in regular expression pattern matching
if ($name =~ m | http: // |) {
  # ...
}

# Change the enclosing character by replacing the regular expression
$name =~ s | http: // ||;

Regular expression escaped by\Q

If you use the regular expression "\Q" escape, you can copy and paste it as a simple string without being aware of the regular expression characters.

my $re = "abc [] ...";

if ($name =~ /\Q $re/) {
  # ...
}

Perl One Liner

Perl's "-e" option allows you to run a Perl program like a one-line command. This is customarily called the Perl one-liner. Linux server management -Effective in UNIX server management.

# Find files ending in .html and replace all foo in the file with bar
find * | grep -P'\ .html $'| xargs perl -pi -e's/foo/bar/g';

Within the Perl one-liner, you can use all the features of Perl. Also, on Linux/UNIX, Perl is widely installed, so you can run it portablely with the same description.

For the Perl one-liner, please also refer to the following.

In addition to Perl, Ruby is another programming language that is easy to use with One Liner.

What you can do with Perl functions

The following articles explain what you can do with Perl functions.

You can do the same with Python, Ruby, and PHP functions.

What you can do with Perl modules

Here's a quick introduction to what you can do with Perl modules. What is called a library in other programming languages is called a module in Perl. Perl has modules that come standard with it and modules that you can install for free from a site called CPAN.

For what you can do with Perl modules, please refer to the module thorough explanation below.

You can do the same with Python, Ruby, and PHP libraries.

UNIX/Linux server management

Perl has historically been a good match for UNIX/Linux servers. Perl one-liner can be used naturally, and it is highly relevant to commands that can be used on UNIX/Linux. Perl is installed on most UNIX/Linux servers.

Website creation

Perl is good at text processing, so it is also good at outputting HTML, which is a text format. When creating a Perl website, I feel that it can be very powerful when combined with Git.

Click here if you want to learn HTML/CSS for creating websites. It also explains how to create a website using a tool called Giblog.

Web application development

Perl is good at outputting dynamic HTML components in HTML, email body, and Ajax communication, so it is good at web application development.

PHP is a well-known and strong field in the field of outputting dynamic HTML components in HTML/email body/Ajax communication in web application development, but it is also a specialty field of Perl/Ruby.

Perl's strengths

Here's a brief description of Perl's strengths.

Linux/UNIX middleware infrastructure

Perl is "git" "openssl" "Debian/Ubuntu Linux command line tools" "mysql, PostgreSQL peripheral tools" are supported and used as middleware on Unix/Linux.

High level backward compatibility

Perl is backwards compatible at a very high level, making it suitable for long-term, stable business applications.

Run immediately without compiling

You don't have to compile when you run the program. The created program is a perl command and can be executed immediately.

# Run Perl program
perl test.pl

This is a great advantage for programs that need to be rewritten many times.

This is a feature of dynamic programming languages, including Python, Ruby, PHP, and more.

Answer to Perl's anxiety

You may be worried about Perl when you're doing a web search for Perl. I think there is a problem with the expression of the article, but Imagine the anxiety that you generally feel and describe it.

Is Perl maintainable and readable?

You may have seen Perl on other websites with low maintainability and readability, and Python with high maintainability and readability.

In reality, no empirical research on readability has been done, and users of each programming language are in a situation where they claim readability very subjectively.

The Perl seminar describes it as follows.

When you write Perl in what is considered a modern way of writing, Perl's readability is relatively good, balancing writeability and readability.

Programming languages have their advantages and disadvantages when it comes to readability.

For example, Perl needs to be able to read and write data structures called reference to represent multidimensional data structures of arrays and hashes. In Python and Ruby, everything is treated as a reference, so there is a sense of unity.

Perl scopes, on the other hand, are very well implemented to represent the structure of programming. If you use scopes well, you can improve Perl's readability.

Is Perl not object - oriented?

As of 2021, Perl has the ability to be object-oriented, but for various reasons it hasn't introduced a concise class syntax.

In Perl, writing a class is generally written as follows.

package Point;

# Accessor
sub x {shift->{x}}
sub y {shift->{y}}

# Constructor
sub new {
  my $class = shift;
  
  my $self = {
    @_;
  };
  
  return bless $self, ref $class || $class;
}

package main;

my $point = Point->new(x => 1, y => 2);
my $x = $point->x;
my $y = $point->y;

It feels fresh, so I'd like to expect it in the near future so that I can write it easily.

Class syntax exists in Ruby, Python, and PHP.

Is Perl unpopular?

I'm not familiar with the general public, but I know people who like Perl with good motives.

Is Perl lagging behind in the field of AI/machine learning?

For deep learning, if you need it in practice, on Amazon AWS, one of the public clouds, Perl's AI library AI: : MXNet is officially supported.

Try Perl programming

Let's try one thing you can do with Perl. Let's run Perl's example code using the data section. A Perl program that converts "," comma-separated data to "|" -separated data.

Paste the code below at the link and click the "Save for 30 days & run" button.

use strict;
use warnings;

# Convert comma-separated data to "|" delimited
while (my $line = <DATA>) {
    chomp $line;
    my @items = split(/,/, $line);
    my $new_line = join('|', @items);
    print "$new_line\n";
}

__DATA__
1, Web Development, 2000
2, Perl Tutorial, 1500

Try the program

The input is the data written under "__DATA__".

1, Web Development, 2000
2, Perl Tutorial, 1500

When you run Perl, the output will look like this:

1 | Web Development | 2000
2 | Perl Tutorial | 1500

To conclude

It's been a long time, but I hope you'll learn Perl, enjoy Perl, and make your work easier with Perl as a publisher.

Related Informatrion