I have like this situation.
my %ha = ()
my @ar = ('1','2')
my $st = 't'
f(%ha,@ar,$st);
sub f
{
my (%h, @a,$s) = @_;
OR
my %h = shift;
my @a shift;
my $s = shift;
}
both do not work. What can I do?
You cannot pass complicated data structures as arguments - they get unpacked into lists of values, and your subroutine can't tell where the boundaries are.
What you can do is pass references instead:
my %hash = ()
my @arr = ('123','456')
my $str = 'test'
sub func
{
my ( $hashref, $arrayref, $str ) = @_;
my %copy_of_hash = %$hashref;
my @copy_of_array = @$arrayref;
## or you can do it by following the reference to modify the hash without copying it:
$hashref->{'key'} = "value";
}
func ( \%hash, \@arr, $str );
Nit: One can pass references. Passing by reference means something else (and Perl always passes by reference).