Tutorial by Examples

The array is one of Perl's basic variable types. It contains a list, which is an ordered sequence of zero or more scalars. The array is the variable holding (and providing access to) the list data, as is documented in perldata. You can assign a list to an array: my @foo = ( 4, 5, 6 ); You can u...
Lists can also be assigned to hash variables. When creating a list that will be assigned to a hash variable, it is recommended to use the fat comma => between keys and values to show their relationship: my %hash = ( foo => 42, bar => 43, baz => 44 ); The => is really only a specia...
As to pass list into a subroutine, you specify the subroutine's name and then supply the list to it: test_subroutine( 'item1', 'item2' ); test_subroutine 'item1', 'item2'; # same Internally Perl makes aliases to those arguments and put them into the array @_ which is available within the s...
You can, of course, return lists from subs: sub foo { my @list1 = ( 1, 2, 3 ); my @list2 = ( 4, 5 ); return ( @list1, @list2 ); } my @list = foo(); print @list; # 12345 But it is not the recommended way to do that unless you know what you are doing. While th...
The arrayref for @foo is \@foo. This is handy if you need to pass an array and other things to a subroutine. Passing @foo is like passing multiple scalars. But passing \@foo is a single scalar. Inside the subroutine: xyz(\@foo, 123); ... sub xyz { my ($arr, $etc) = @_; print $arr-&g...
In list context hash is flattened. my @bar = ( %hash, %hash ); The array @bar is initialized by list of two %hash hashes both %hash are flattened new list is created from flattened items @bar array is initialized by that list It is guaranteed that key-value pairs goes together. Keys are...

Page 1 of 1