1. Perl
  2. FAQ

FAQ - Answering Frequently Asked Questions in Perl

I would like to write about (probably) frequently asked questions in Perl.

What does the shift function with no arguments mean?

In Perl source code, you'll often see shift functions with no arguments. The meaning of the shift function with no arguments is as follows.

  1. In the subroutine, fetch the first argument of the argument of the subroutine
  2. Elsewhere, it fetches the first argument of the command line argument
sub foo {
  # Same meaning as "my $x = shift @_;"
  my $x = shift;
}

# Same meaning as "my $y = shift @ARGV;"
my $y = shift;

Is it possible to handle Japanese in Perl?

Perl is a language that is almost fully Unicode-enabled. It also supports many encodings, so you can work with characters in a variety of environments. Please refer to the following article as a reference for dealing with Japanese in Perl.

Jcode.pl and JCode.pm are no longer the recommended methods, so learn about the modern standard methods that use Encode.pm.

Does Perl have exception handling?

Yes. I have. Perl uses the syntax eval block. If an exception occurs, the content of the exception will be assigned to the variable $@, so check it.

# Exception handling
eval {
  ...
}

# What to do if an exception occurs
if ($@) {
  ...
}

There is no need to use a special module, and exception handling is provided as a default function.

I want to manipulate a string using a function

Perl provides a variety of string functions. Please read the item "String operation" in List of frequently used functions/modules.

What is the scope of variable in Perl?

The article Understanding Perl Scope describes Perl Scope.

Is there a standard way to handle dates

For Perl 5.10 or later, a module called Time::Piece is attached as standard. Please read Explanation of Time::Piece module.

Can Perl perform automated tests?

Yes. You can perform an automatic test. Please refer to Automatic test (Perl reverse lookup dictionary) for automatic test.

Is CGI the only way to create a web application in Perl?

Nowadays, various methods are possible. In addition to using CGI, there are methods such as executing with mod_perl on Apache, executing using the FastCGI protocol, and executing using a protocol called PSGI.

If you want to write a web application easily, it is better to use the module Mojolicious::Lite in the framework called Mojolicious rather than using the CGI module.

Web framework Mojolicious

Is Perl a programming language for CGI?

No, Perl is a generic programming language. So many things can be done in Perl. CGI is a protocol for running programs on a web server and is not directly related to Perl.

Is PHP easier than Perl to develop web applications?

Developing a web application with PHP is much easier than developing with Perl's CGI.pm. For this reason, many people seem to find PHP easier to develop web applications.

However, if you use a web framework such as Mojolicious, you can obtain even higher development efficiency than web application development using plain PHP. In addition, you can get various advantages such as extremely simple description, no need to build a test environment, reliable application development by automatic test, easy installation, general-purpose application development that can be executed in various environments. I can do it.

It's important to realize that the correct point of view when comparing is not "PHP vs Perl + CGI.pm", but "PHP vs Perl + web framework".

Is PHP faster than Perl?

This is also often written in books, but it is a complete misunderstanding. When comparing the language PHP and the language Perl, the processing speed does not change much.

It is often said that PHP is faster than CGI, but it is a misunderstanding about CGI. CGI is a protocol and has nothing to do with Perl. In other words, there are PHP that uses CGI, C language that uses CGI, and Perl that uses CGI.

PHP written in CGI is as slow as Perl written in CGI. PHP running on mod_php on Apache is as fast as Perl running on mod_perl on Apache. Also, Perl running on the PSGI protocol is as fast as PHP running on mod_php on Apache.

So speed comparison has little meaning in choosing a language, PHP or Perl.

Please tell us about the relationship between Perl 6 and Perl 5.

Perl6 was developed in the past as a successor to Perl5, but now Perl6 is a different programming language than Perl5. Perl6 was officially released in December 2016. The current Perl, Perl 5, is promised to continue development and support in the future.

See the explanation below for details.

Why does Perl tend to be negatively evaluated when looking at the net?

There are many possible causes, but most people speak of the image of the language, not the usefulness of the language. For example, it is a vague image such as "new" or "old". I think people who like new languages tend to think of Perl as old. Also, the bitter experience of being forced to maintain unmaintainable Perl scripts in the Perl 4 era may have led to a bad image.

It's been 10 years since Perl 6 was born, so many people may have an obsolete image of Perl 5. The trend in the world tends to give Perl a classic image, but it's also due to the Perl community, and probably to the non-Perl language community.

What are the good features of Perl compared to other scripting languages?

One is that it is available on many platforms and is often installed by default. For example, even if you don't have permission to install a programming language, you're likely to be able to use Perl.

It also maintains a very high level of backward compatibility, so your scripts can be run in other environments without any changes. Perl doesn't have much of a worry about which version to write. If you write it with Perl version 5.8 in mind, you should be able to run it in most environments. In this regard, Perl is much more caring and user-friendly than other scripting languages.

If you're an infrastructure engineer, it's no exaggeration to say that Perl is one of the first languages you need to know. You can rest assured that your scripts will continue to run in the future. Also, if you are a web engineer, you will probably study one of PHP, Ruby, and Perl.

Also, the processing speed of text processing is fast. If you have a general text processing environment, you can probably do text processing more than twice as fast as Ruby. This means that a process that takes an hour can be completed in 30 minutes. Perl is probably the best language given the descriptiveness and performance of programming in text processing.

It also saves memory and compiles faster than other scripting languages. It's also important to keep in mind that Perl is one of the languages that allows you to write scripts neatly, unlike what others have imagined. If Ruby or Python is called the best in terms of descriptiveness, Perl is a little less than it is, but it's pretty clean enough to be called better.

One of the great attractions is that there are a lot of modules called CPAN that can be used for free. If you can't do something with Perl itself, you can probably solve it by looking at the modules on CPAN.

Also very web applicationHaving an environment that is easy to create will also be a point to use. There is a very easy-to-use web framework called Mojolicious.

So I think Perl is a good language for business use as well.

Need to learn Perl4 (Perl version 4)

There are many scripts on the Internet that can be run with Perl4, but now it's safe to say that you don't have to write Perl with Perl4 in mind. Perl5 has a penetration rate of almost 100%, so it's a good idea to write in Perl5 notation, which is maintainable, readable, and flexible. Perl5 has been improved and updated in terms of readability and maintainability.

I'm not familiar with Sigil "$@%".

You'll get used to it after using Perl for a while. The advantage of sigil is that you can visually see that it is a variable. Compared to other languages, I'm not used to Perl, so I think it's hard to read, but as I get used to it, I can visually understand that it's a variable, so it's easier to read. Also, since function names and variable names never collide, you don't have to think about what reserved words are when programming, so you can program without stress.

Is it true that you can't read the Perl source code after a week?

No. As with any language, it's hard to read if you write it in an easy-to-understand way, and it's hard to read if you write it incomprehensible. Modern Perl is called Perl 5, and it's much easier to read and maintain than the old Perl. You can describe the process as easily as other scripting languages. I think it's true that reading source code written a long time ago is difficult to read, but it's not the Perl language itself. It's just that a lot of code was written before the knowledge to write good source code was accumulated.

For example, Perl's Mojolicious and Ruby's Ruby on Rails. Which one do you find easier to read?

Is it true that Perl is old?

It is true if it means that the origin is old. Perl is a language born in 1987. But being old does not mean that it is unusable or inconvenient. Perl is used in many fields as a modern programming language, the community is active, and the language is updated regularly. Modules for the latest technology are actively released to CPAN by the community. There are also many companies using Perl in the area of the Web that needs to handle high volumes of access.

Are Python and Ruby an evolution of Perl?

It would be more accurate to refer to it than to evolve it. The three languages are common, and the areas of programming that the three languages can do are very similar. It's better to say that each has its own unique side as a programming language. It is important not to just swallow the information, but to actually check it.

If you're negative about Perl and recommend Python or Ruby, ask why. If you can only tell me "new and clean. It's great.", The person is likely to know less about the language.

Unlike C and Java, there is no type, so it's too free to get used to.

Scripting languages such as Perl, Ruby, and Python allow you to assign objects of any type to variable (a scalar variable in Perl).

# Perl
my $url = URL->new;

# Java
URL url = new URL ();

For those who have learned static languages, this part alone may seem awkward.

However, the lack of types has the obvious advantage over static languages. The first is that you don't have to think about method overloading. C ++ and Java have a feature called method overloading. This is a function that allows you to define a method with the same name when the method argument types are different. For example, a method that takes a floating-point type as an argument and a method that takes an integer as an argument must be written separately.

# Java
int sum (double num1, double num2) {...}
double sum (double num, double num2) {...}

On the other hand, only one dynamically typed language is required. This greatly improves maintainability in the sense that it eliminates duplication.

sub sum {}

Second, you don't have to think about template classes like C ++. You can accept any type, so you don't have to say anything in the method or class definition depending on the type.

Third, you don't have to think about interfaces (in Java). Designing Java classes involves the very difficult task of thinking about a common interface. Interface design will be indispensable when trying to increase versatility. In the case of a dynamic language, any type of object can be assigned to a variable, and the method is called at runtime, so there is no need to worry about this.

So instead of looking at the negative side of having no types, why not look at the positive side of dynamic languages, where you can write concise, consistent, and concise programming. You will find that you can actually write programming easily.

Can I write a large program in Perl?

Yes. Perl is sometimes thought to be good for writing small programs, but that's not the case. For example, if you have the image of making a large program in Java and a small program in Perl, you are misunderstanding the language Perl.

You'll find that writing in Perl allows you to write large-scale applications with significantly less code than writing in a static language. You'll also notice that there's nothing that seems difficult to write a large application.

For example, considering that the site Amazon is written in Perl as an example, I think that the misunderstanding that Perl is not suitable for large-scale programming can be solved.

Can I do object - oriented programming in Perl?

It's possible to do object-oriented programming in Perl, and it has enough features to do object-oriented programming. However, the procedure for creating a class is slightly different from other object-oriented languages. Because of this, the first time you do object-oriented programming, you will find that the learning cost is somewhat higher than in other languages. It's also true that Perl's object-oriented writing is more time-consuming than other scripting languages. Modules can be as light as other languages, but Perl's core doesn't include standard object-oriented modules, so you'll probably want to use some modules from CPAN.

For example, using Object::Simple, you can write as follows.

package Point;
use Object::Simple -base;

has x => 0;
has y => 0;

1;

Base class inheritance.

package Point3D;
use Object::Simple 'Point';

has z => 0;

1;

It is possible to write classes as simple as other languages.

Perl standard module (core module) cannot be used on CentOS

When I tried Centos 6 with Sakura VPS, I noticed that Perl 5.10.1 installed by default is not a genuine Perl. I noticed that I was trying to run a Mojolicious application, but some of the standard modules are missing. what the hell.

The core module (standard module) can be used by installing the following packages.

yum -y install perl-core

Please try the same method for CetnOS 7 or later.

Related Informatrion