Perl push() function

The Perl programming language has a built-in push() function. Using this function, you can add elements to the end of the Perl array.

Here is an example:

▶ Run

use Data::Dumper;

my @arr = (1, 2);

push @arr, undef, 'Hello', 8;

print Dumper \@arr;

We have created an array @arr with two elements. Then we are adding three more elements to the array using push() function. As you can see we are adding three completely different types of values to the array: undef, string 'Hello' and the number 8.

Here is the the output from this program:

$VAR1 = [


push(@arr, $value1, $value2);
push(@arr, @other_arr);
push(@arr, @other_arr, @and_the_other_arr);

Standard using push() is pass it is not at least two elements.

The first element must be an array (some versions of Perl the first element may be a reference to an array, but this use is not recommended, details in another section below)

The following arguments are usually scalars or arrays. In the array of the first argument will be added to all elements of the following arguments.

As argument you can specify the hash push(@arr, %hash). In the array @arr will be added all the pairs key-value of hash (there is the peculiarity that the order of the hash values is undefined and different runs of the same program can create the array elements in different sequences). But real life use of hash as a function argument push() rare.

If you do not specify any arguments, it will error:

Not enough arguments for push at line 3, near "push()"
Execution of aborted due to compilation errors.

If you specify one argument and this argument is an array push(@arr);, then errors will not be this does not change the array, but if the code has use warnings;, that is the warning:

Useless use of push with no values at line 8.

Return value

Function push() returns the number of elements that was in the array after perform this function.

my $number = push(@arr, $value);

This code is similar to this code:

push(@arr, $value);
my $number = scalar(@arr);

Return value push() need not very often. Very often used push(@arr, $value), and the return the value is ignored.

Use ARRAYREF as the first argument

Standard using push() — the first argument is the array into which the elements are added from all the other arguments.

But in the programming language Perl was an experiment. If the first argument is an array reference, the function push()itself did dereferencing this reference and added data to the array.

Here is the code.

▶ Run

use strict;
use warnings;
use Data::Dumper;

my $arr = [];

push $arr, 1;

warn Dumper $arr;

Perl to version 5.12 (inclusive) of this code throws the error:

Type of arg 1 to push must be array (not private variable) at line 9, near "1;"
Execution of aborted due to compilation errors.

Starting with Perl 5.14 and 5.18 to version (inclusive) is the code added the item in the ARRAYREF, and the result was:

$VAR1 = [

Perl 5.20 and 5.22 also added an item in the ARRAYREF, but also gave warning:

push on reference is experimental at line 9.
$VAR1 = [

And starting with version 5.24 of this code gives the error:

Experimental push on scalar is now forbidden at line 9, near "1;"
Execution of aborted due to compilation errors.

This feature of language use is not recommended. If you need to add the item in the ARRAYREF, it is best to write clearly push @{$arr}, 1; instead push $arr, 1;

Here's the code that works identically on all versions of Perl:

▶ Run

use strict;
use warnings;
use Data::Dumper;

my $arr = [];

push @{$arr}, 1;

warn Dumper $arr;

Official documentation

Here is the output of the command perldoc -f push:

       push ARRAY,LIST
       push EXPR,LIST
               Treats ARRAY as a stack by appending the values of LIST to the
               end of ARRAY.  The length of ARRAY increases by the length of
               LIST.  Has the same effect as

                   for $value (LIST) {
                       $ARRAY[++$#ARRAY] = $value;

               but is more efficient.  Returns the number of elements in the
               array following the completed "push".

               Starting with Perl 5.14, "push" can take a scalar EXPR, which
               must hold a reference to an unblessed array.  The argument will
               be dereferenced automatically.  This aspect of "push" is
               considered highly experimental.  The exact behaviour may change
               in a future version of Perl.

               To avoid confusing would-be users of your code who are running
               earlier versions of Perl with mysterious syntax errors, put
               this sort of thing at the top of your file to signal that your
               code will work only on Perls of a recent vintage:

                   use 5.014;  # so push/pop/etc work on scalars (experimental)

Related topics

Other articles