Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
In Perl, complex data structures, such as arrays of arrays, hashes of arrays, arrays of hashes, and even more intricate combinations, can be created using references. Passing these complex parameters to subroutines requires understanding of references and dereferencing.
Let's dive into how to pass and handle complex parameters in subroutines:
Remember that a reference is essentially a memory address of a variable:
\$scalar
\@array
\%hash
To pass an array of arrays to a subroutine:
sub print_aoa { my ($aoa_ref) = @_; for my $inner_array_ref (@$aoa_ref) { for my $item (@$inner_array_ref) { print "$item "; } print "\n"; } } my @aoa = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] ); print_aoa(\@aoa);
sub print_hoa { my ($hoa_ref) = @_; for my $key (keys %$hoa_ref) { print "$key: @{ $hoa_ref->{$key} }\n"; } } my %hoa = ( fruits => ["apple", "banana", "cherry"], animals => ["cat", "dog", "elephant"] ); print_hoa(\%hoa);
sub print_aoh { my ($aoh_ref) = @_; for my $hash_ref (@$aoh_ref) { for my $key (keys %$hash_ref) { print "$key: $hash_ref->{$key}, "; } print "\n"; } } my @aoh = ( {name => "Alice", age => 25}, {name => "Bob", age => 30}, {name => "Charlie", age => 35} ); print_aoh(\@aoh);
Perl provides syntactic sugar for dereferencing:
@$array_ref
or @{$array_ref}
%$hash_ref
or %{$hash_ref}
It's possible to mix these structures for more complex data:
my %complex = ( numbers => [1, 2, 3], user => { name => "Alice", age => 25, pets => ["cat", "dog"] } ); sub print_complex { my ($complex_ref) = @_; print "Numbers: @{ $complex_ref->{numbers} }\n"; print "Name: $complex_ref->{user}->{name}\n"; print "Pets: @{ $complex_ref->{user}->{pets} }\n"; } print_complex(\%complex);
Understanding references and dereferencing is crucial when dealing with complex data structures in Perl. By grasping this concept, you can efficiently manipulate and access nested data structures, making your scripts much more versatile.
sub process_array { my @input_array = @_; # Receive array as argument foreach my $element (@input_array) { print "Element: $element\n"; } } my @my_array = (1, 2, 3, 4, 5); process_array(@my_array);
sub complex_subroutine { my ($scalar, @array, %hash) = @_; # Receive scalar, array, and hash # Process parameters as needed } complex_subroutine("scalar_value", (1, 2, 3), key1 => "value1", key2 => "value2");
sub process_hash { my %input_hash = @_; # Receive hash as argument foreach my $key (keys %input_hash) { print "$key: $input_hash{$key}\n"; } } my %my_hash = (name => "John", age => 25, city => "New York"); process_hash(%my_hash);
sub process_complex { my $complex_ref = shift; # Receive reference to a complex data structure # Dereference and process as needed } my $complex_data = { array => [1, 2, 3], hash => { key => "value" } }; process_complex($complex_data);
sub process_multiple_arrays { my ($array1_ref, $array2_ref) = @_; # Receive references to arrays # Dereference and process arrays as needed } my @array1 = (1, 2, 3); my @array2 = (4, 5, 6); process_multiple_arrays(\@array1, \@array2);
sub process_array_of_hashes { my $array_of_hashes_ref = shift; # Receive reference to array of hashes foreach my $hash_ref (@$array_of_hashes_ref) { # Dereference and process each hash } } my @array_of_hashes = ( { key1 => "value1" }, { key2 => "value2" }, { key3 => "value3" } ); process_array_of_hashes(\@array_of_hashes);
sub process_hash_of_arrays { my $hash_of_arrays_ref = shift; # Receive reference to hash of arrays foreach my $key (keys %$hash_of_arrays_ref) { # Dereference and process each array } } my %hash_of_arrays = ( key1 => [1, 2, 3], key2 => [4, 5, 6], key3 => [7, 8, 9] ); process_hash_of_arrays(\%hash_of_arrays);
sub variable_parameters { my @parameters = @_; # Receive variable number of parameters foreach my $param (@parameters) { # Process each parameter as needed } } variable_parameters("param1", "param2", "param3");