Perl日記

日々の知ったことのメモなどです。Perlは最近やってないです。

@にいれてない配列からはshiftとかpushとかはできません

ふわっふわしたままの配列の要素変更はできません><

#!/usr/local/bin/perl
use strict;
use warnings;
use feature qw/say/;

my $res = shift (1, 2, 3, 4, 5);
say $res;

Type of arg 1 to shift must be array (not list) at ./ary1.pl line 6, near "5)"
Execution of ./ary1.pl aborted due to compilation errors.


リストじゃなくて、配列じゃないとコンパイルエラーになります。


こんなのとか、

unshift (1, 2, 3, 4, 5), 6;

Type of arg 1 to unshift must be array (not constant item) at ./ary2.pl line 5, near "5)"
Execution of ./ary2.pl aborted due to compilation errors.


こんなのとか、

push ('elm1', 'elm2', 'elm3'), 'Last Element';

Type of arg 1 to push must be array (not constant item) at ./ary3.pl line 5, near "'elm3')"
Execution of ./ary3.pl aborted due to compilation errors.


もできません><


ただ、shiftに関しては、一度無名で包んでデリファンレンスというウェルノウンな技を使ってとることができます。

#!/usr/local/bin/perl
use strict;
use warnings;
use feature qw/say/;

my $res = shift @{[(1, 2, 3, 4, 5)]};
say $res; #=> 1