my %hash;
my $input2 = "message";
#calling subroutine check_something
$self->check_something (%hash, message => $input2);
sub check_something {
my $self = shift;
my %p = @_;
my $message = $p{message};
my %hash = @_;
# do some action with the %hash and $input2;
}
Run Code Online (Sandbox Code Playgroud)
我构建一个哈希(%哈希),并有另一个我想传递给子程序的变量.但是,在子程序中,我正在做的方式"我的%hash = @_"也获得了$ input2的值.我该怎么做才能避免这种情况?
@_是一个数组,因此设置变量.如果你想要解决单个部分,你可以解决$ _ [0]; 通过ref传递哈希:
$self->check_something (\%hash, {message => $input2});
sub check_something {
my ($self, $ref, $message) = @_;
my %p = %{$ref};
# can reference the value of $input2 through $$message{'message'} or casting $message as a hash my %m = %{$message}; $m{'message'};
# do some action with the %hash and $input2;
}
Run Code Online (Sandbox Code Playgroud)