Perl:函数返回引用或复制? [英] Perl: function returns reference or copy?

查看:97
本文介绍了Perl:函数返回引用或复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个perl的新手。所以这个问题可能听起来很天真。



我有以下两个函数:

pre $ code>#此函数将返回数组的引用
sub getFruits1
{
my @fruits =('apple','orange','grape');
返回\ @ fruits;
}

但在以下情况下?

 #它如何返回? 
sub getFruits2
{
my @fruits =('apple','orange','grape');
返回@fruits;

getFruits2 返回该数组的一个引用和一个新副本将被创建?

解决方案

getFruits2 子程序返回一个列表,它可以像这样分配给一个新数组。

  my @newfruits = getFruits2(); 

是的,它会在数组中产生数据的副本


Im a newbie in perl. So the question might sound something naive.

I have two following functions

#This function will return the reference of the array
sub getFruits1
{
    my @fruits = ('apple', 'orange', 'grape');
    return \@fruits;
}

But in the following case?

#How it returns?
sub getFruits2
{
    my @fruits = ('apple', 'orange', 'grape');
    return @fruits;
}

Will getFruits2 return a reference and a new copy of that array will be created?

解决方案

The getFruits2 subroutine returns a list, which can be assigned to a new array like this

my @newfruits = getFruits2();

And yes, it will produce a copy of the data in the array

这篇关于Perl:函数返回引用或复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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