Perl reference functionality is similar to C pointer functionality

Perl has the ability to get a reference to a value called a reference, which is similar to the functionality of a pointer in C.

Perl Reference

You can get a Perl value reference with "\".

my $num = 1;
my $num_ref = \ $num;

C pointer

In C language, you can get the address of a value with "&" and assign it to a pointer type variable.

int32_t num = 1;
int32_t * num_ptr = & amp; num;

Comparison of Perl reference and C pointer

Perl and C do the same thing, except that the symbol is "\" or "&". In the case of Perl, it says to get the reference, but in reality it just gets the value of the address. You can do address operations in C, but think of Perl's reference as an address value in C that you can't do.

In C language, it is assigned to a pointer variable, but in Perl, the type is dynamic, so it can be assigned to a variable as it is.

The concept of pointers learned in C language can be used as it is in Perl

You can use the concept of C language pointers that you learned hard in a university software class in Perl as they are.

Also, if you started programming from Perl, you can use Perl to understand the functions that correspond to pointers in C language.

Perl, like C, is a language that clearly distinguishes between values ​​and references.

One opinion is that it's easier for users to hide the distinction between values ​​and references, but it's possible to distinguish between values ​​and references because it's a very basic idea of ​​software data. That is also a merit.

In fields such as assembler and machine language, which are lower level than C language, it is required as basic knowledge as an engineer to be able to clearly distinguish between values ​​and references (addresses).

How about taking this difference as the individuality of the language?

Other similarities between Perl and C

In this article, I wrote that Perl references are similar to C pointers, but also Perl if statements, for statements, while statements, increments / decrements, operator priorities, scopes, and reference concepts. , Similar to C language grammar.

Associated Information