简体   繁体   English

如何在Perl中将变量从一个函数传递到另一个函数

[英]How do I pass in a variable from one function into another in perl

I am initializing a variable within one function and would like to pass this variable into another function. 我正在一个函数内初始化一个变量,并希望将此变量传递给另一个函数。 This variable holds a char value. 该变量保存一个char值。 I have tried passing in the referencing and dereferencing, declaring the variables outside of the function, and using local. 我尝试传递引用和解引用,在函数外部声明变量,并使用local。

I've also looked in perlmonks, perl by example, googled and looked through this site for a solution but to no avail. 我也查看过perlmonks,例如perl,用google搜索了整个网站,寻找解决方案,但无济于事。 I'm just starting out with perl programming so any help will be appreciated! 我只是从Perl编程开始,所以我们将不胜感激!

Sounds to me like you need to read through some documentation, not just google around. 在我看来,您需要阅读一些文档,而不仅仅是google。 I would suggest http://www.perl.org/books/beginning-perl/ . 我建议http://www.perl.org/books/beginning-perl/

use strict;
use warnings;

sub foo {
    my $char = 'A';

    bar($char);
}

sub bar {
    my ($bar_char) = @_;

    print "bar got char $bar_char\n";
}

foo();

If you pass a parameter by reference (see below), it can be modified by the first function and you can then pass it to another function: 如果通过引用传递参数(请参见下文),则可以通过第一个函数对其进行修改,然后可以将其传递给另一个函数:

    #!/usr/bin/perl

sub f {
  $c = shift;
  $$c='m';
}

$c='a';
f(\$c);
print $c;

This will print 'm' 这将打印“ m”

Is there a reason who your first function cannot return this variable? 您的第一个函数无法返回此变量的原因吗?

my $config_variable = function1( $param1 );
function2 ( $config_variable, $param2 );

You can also pass more than one variable back too: 您还可以传递多个变量:

my ( $config_variable, $value ) = function1( $param1 );
my $value2  = function2( $param1, $config_variable );

This would be the best way. 这将是最好的方法。 However, you can use globally defined variables and they can be used from function to function: 但是,您可以使用全局定义的变量,并且可以在函数之间使用它们:

#! /usr/bin/env perl
#
use strict;
use warnings;

my $value;

func1();
func2();

sub func1 {
    $value = "foo";
}

sub func2 {
    print "Value = $value\n";
}

Note that I declared $value outside of both functions, so it's global in the entire file - even in the subroutines. 请注意,我在两个函数之外都声明了$value ,因此它在整个文件中都是全局的 ,即使在子例程中也是如此。 Now, func1 can set it, and func1 can print it. 现在, func1可以设置它,而func1可以打印它。

The technical term for this is: A terrible, awful, evil idea and you should never, ever 1 think of doing it. 这样做的技术术语是:一个可怕的,可怕的,邪恶的想法,你应该永远,永远想到1做的。

This is because a particular variable you think is set to one value suddenly and mysteriously changes values without any reason. 这是因为您认为某个特定变量突然设置为一个值,并且在没有任何原因的情况下神秘地更改了值。 Do this for one variable is bad enough, but if you use this as a crutch, you'll end up with dozens of variables that are impossible to track through your program. 对一个变量执行此操作已足够糟糕,但是如果将其用作拐杖,最终将导致数十个无法在程序中跟踪的变量。

If you find yourself doing this quite a bit, you may need to rethink your code logic. 如果您发现自己做了很多事情,则可能需要重新考虑代码逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM