Perl makes a clear distinction between values ​​and references

Perl is a programming language that makes a clear distinction between values ​​and references, which is rare in dynamic programming languages.

If you're learning C in the basics of software, it's relatively easy to distinguish between values ​​and references.

The values ​​and pointers in C are the values ​​and references in Perl. References are also known as references.

First of all, I will write what I want you to remember as a premise.

Dynamic arrays and associative arrays

Perl has built-in dynamic arrays and associative arrays in the language. In the Perl language, dynamic arrays are simply called arrays, and associative arrays are called hashes.

# Array (dynamic array)
my @nums = (1, 2, 3);

#Hash (associative array)
my%person = (age => 12, name =>'kimoto');

In Perl, keep in mind that variables to which arrays and hashes can be assigned are represented by the special symbols "@" and "%".

Remember that "@" is similar to the acronym a for Array. Remember that the "%" seems to correspond to a key and a value.

Variables to which numbers or strings are assigned are represented by "$".

# Numbers and strings
my $num = 4;
my $name ='kimoto';

Variable correspondence

I will briefly describe the correspondence of variables.

Numbers and strings $num, $name
Array (dynamic array) @nums
Hash (associative array) %person

Perl's default assignment is a copy of the value

Perl is based on the grammar of C language, and is considered to be easy for those who have learned C language to learn.

The default behavior of assignments in C is copying values. Perl has the same specifications.

Copy of numbers

Let's copy the numbers.

my $num1 = 1;

# Copy of numbers
my $num2 = $num2;

The value 1 is copied. Are you surprised? You are not surprised. This is a familiar operation for those who know the language specifications of C language.

Copy of string

Let's copy the string.

my $name1 ='kimoto';

#Copy of string
my $name2 = $name1;

So what will happen? In fact, the string is copied. Even if you change $name1, the value of $name2 does not change.

It is a Perl language specification that a copy is made when a string is assigned. You can safely pass a string.

The internal implementation is copy-on-write. Performance is very good and there are no conflict issues on a single thread.

Copy of array

Let's copy the array.

my @nums1 = (1, 2, 3);

# Copy of array
my @nums2 = @nums1;

What will happen now? The array itself is copied. Even if you change the value of the element of @num1, the element of @nums2 does not change.

It is a Perl language specification that a copy is made when an array is assigned.

Hash copy

Let's copy the hash.

my%person1 = (age => 12, name =>'kimoto');

# Hash copy
my%person2 =%person1;

What will happen now? The hash itself is copied. Even if you change the value of the element of%person1, the element of%person2 does not change.

It is Perl's language specification that a copy is made when a hash is assigned.

The default behavior for Perl assignments is all copy

The default behavior for Perl assignments is all copy.

This means that you can copy the basic data structure using only the language specification features, without using the copy function.

Copying values ​​is a very common feature. Perl is a language specification that allows you to copy values.

Perl's default

First, keep in mind that Perl makes a clear distinction between values ​​and references.

The value is a copy of the value itself, and in the reference, the address is copied.

Perl has a data structure called a reference

Perl can make references just like pointers in C. For reference, it is better to have the image of an arrow.

# Numerical reference
my $num = 1;
my $num_ref = \ $num;

# String reference
my $name ='kimoto';
my $name_ref = \ $name;

# Array reference
my @nums = (1, 2, 3);
my $nums_ref = \ @nums;

# Hash reference
my%person = (age => 1, name =>'kimoto');
my $person_ref = \%person;

A reference is actually an address value, and it's just a number, so variables start with a "$", just like storing a number.

Numbers and strings $num, $name References to numbers and strings $num_ref, $name_ref
Array @nums Array Reference $nums_ref
hash %person hash reference $person_ref

The reference holds the address of the data, so if you copy the reference, it only copies the address value, not the value itself.

# Array reference
my @nums1 = (1, 2, 3);
my $nums_ref1 = \ @nums;

my $nums_ref2 = $nums_ref1;

If you change the element of @nums1, the array pointed to by $nums_ref2 is the same array, so it looks like it has changed.

Syntack sugar for making array references and hash references directly

To read the Perl source code, keep in mind that there is a syntax sugar to make array references and hash references directly.


# Array reference
my @nums = (1, 2, 3);
my $nums_ref = \ @nums;

# The above can be written as follows
my $nums_ref = [1, 2, 3];

# Hash reference
my%person = (age => 1, name =>'kimoto');
my $person_ref = \%person;

# The above can be written as follows
my $perlson_ref = {age => 1, name =>'kimoto'};

The advantage of language is that you can distinguish between values ​​and references

Perl is misunderstood because Perl's clear distinction between values ​​and references has been criticized simply as confusing.

It's been done to overstate the bad things about Perl, but the distinction between values ​​and references has a clear advantage.

Strings are copied by default, so it's safe

The fact that the string is copied with the default behavior means that it will not be rewritten at the destination passed by the function.

Since the string is copied, it does not happen that the string is rewritten and causes unintended behavior deep inside the function.

You will realize this gratitude as your project grows.

Perl strings can be rewritten

Perl strings can be rewritten by default.

When a replacement is performed with a regular expression, etc., a new character string is not created, but if sufficient capacity is secured, that character string can be used.

This is a huge benefit to string processing performance. Perl is also designed with the language itself to optimize string processing performance.

No need to write copy function

Copying strings is a very common feature, but you don't have to write a copy function like strcopy in your program.

You may need to copy the array from time to time, but you can copy it without writing a function with the language specification function.

This means that Perl programs can be written very short and smart.

Understand the basics of software

In the world below C, for example, in assembler and machine language, the data in memory is composed of one dimension.

This is made to look as if it were multidimensional, using the concept of address reference.

Understanding the concept of references is essential to understanding the low levels of software.

Perl is a programming language that distinguishes between values ​​and references, not just a language that has references only.

Beginning with Perl, which is easy to write, you can learn the basic concept of software called references.

Associated Information