将变量传递给 Perl 子程序 [英] Passing variables to a Perl subroutine

查看:45
本文介绍了将变量传递给 Perl 子程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下变量传递给子程序 mySubroutine, $name, $age 然后是这个多维数组:

I would like to pass the following variables to subroutine mySubroutine, $name, $age and then this multidimensional array:

$name = "jennifer";
$age = 100;

$list[0][0] = "TEST NAME 2";
$list[0][1] = "TEST GROUP 2";
$[0][2] = 10;

$[1][0] = "TEST NAME 2";
$[1][1] = "TEST GROUP 2";
$[1][2] = 2;

子程序:

sub mySubroutine
{

}

我尝试了 $_[0]@_,但我似乎没有正确地将变量传递给子程序.

I have tried $_[0], and @_, but I don't seem to get the variables passed to the subroutine correctly.

推荐答案

有几种方法可以做到(就像 Perl 中的大多数事情一样).我个人是这样做的:

There are several ways to do it (like most things in Perl). I personally do it like this:

sub mySubroutine
{
    # Get passed arguments
    my ($name, $age, $refList) = @_;

    # Get the array from the reference
    my @list = @{$refList};
    # Good to go
}

# You need to pass @list as reference, so you 
# put \@list, which is the reference to the array
mySubroutine($name, $age, \@list);

这篇关于将变量传递给 Perl 子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆