Perl 中的全局变量、子程序变量问题

发布于 2024-09-04 08:21:50 字数 268 浏览 12 评论 0原文

如何将子程序变量值转移到另一个子程序变量中,我可以使用全局变量吗?

sub foo(){

my $myvar = "Hello";
} 

sub foo1(){
my $myvar1 = $myvar;   # how can I get the "Hello" from $myvar.
}

我尝试使用包和全局变量,但失败了。

Package Bar;
our $bar;

谢谢。

How can I transfer the subroutine variable value into another subroutine variable, Can I use global variable.

sub foo(){

my $myvar = "Hello";
} 

sub foo1(){
my $myvar1 = $myvar;   # how can I get the "Hello" from $myvar.
}

I tried to use package and global variable, but failed.

Package Bar;
our $bar;

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ˇ宁静的妩媚 2024-09-11 08:21:50

您可以在包含 2 个函数的作用域中声明变量:

{ my $myvar

  sub foo{
    $myvar = "Hello";
  } 

  sub foo1{
    my $myvar1 = $myvar;   
  }
}

但这并不是很优雅,并且可能很难维护,因为在 foo1 中不清楚 $ 的值在哪里myvar 已设置。将变量作为参数传递可能更好。

sub foo {
    my $myvar = "Hello";
    return $myvar;
}

sub foo1 {
  my( $myvar)= @_;
  my $myvar1 = $myvar;
}

# calling code
my $myvar= foo();
foo1( $myvar);

请注意,所有 3 个 $myvar 都是不同范围内的不同变量。

附带说明一下,使用原型 (sub foo()) 可能不是一个好主意,除非您真的知道它们在做什么,但情况可能并非如此(请参阅 原型的问题用于原型的讨论)

You could declare the variable in a scope that includes the 2 functions:

{ my $myvar

  sub foo{
    $myvar = "Hello";
  } 

  sub foo1{
    my $myvar1 = $myvar;   
  }
}

That is not really elegant though, and can be hard to maintain, as it is not clear in foo1 where the value of $myvar was set. It is probably better to pass the variable as an argument.

sub foo {
    my $myvar = "Hello";
    return $myvar;
}

sub foo1 {
  my( $myvar)= @_;
  my $myvar1 = $myvar;
}

# calling code
my $myvar= foo();
foo1( $myvar);

Note that all 3 $myvar are different variables, in different scopes.

As a side note, using prototypes (sub foo()) is probably not a good idea, unless you really know what they are doing, which is likely not to be the case ( see The problem with prototypes for a discussion on prototypes)

放血 2024-09-11 08:21:50

如何将子程序变量值转移到另一个子程序变量中,我可以使用全局变量吗?

是的,您可以:

my $myvar;
sub foo(){
    $myvar = "Hello";
} 

sub foo1(){
    my $myvar1 = $myvar;   # how can I get the "Hello" from $myvar.
}

即使使用“use strict;”,这也有效。和“使用警告;”。

我尝试使用包和全局变量,但失败了。

包变量适用于您想要导出到包外部的变量,而不是您想要在同一包中的两个子例程之间共享的变量。

How can I transfer the subroutine variable value into another subroutine variable, Can I use global variables?

Yes, you can:

my $myvar;
sub foo(){
    $myvar = "Hello";
} 

sub foo1(){
    my $myvar1 = $myvar;   # how can I get the "Hello" from $myvar.
}

This works even with "use strict;" and "use warnings;".

I tried to use package and global variable, but failed.

Package variables are for variables you want to export outside your package, not for variables you want to share between two subroutines in the same package.

孤单情人 2024-09-11 08:21:50

只是不要使用my

#!/usr/bin/perl

sub foo() {
  $myvar = "Hello\n";
}

sub foo1() {
  $myvar1 = $myvar;
    print $myvar1;
}

print "here we go!\n";
foo();
foo1();

但是,我不推荐这种编程方式。

Just don't use my:

#!/usr/bin/perl

sub foo() {
  $myvar = "Hello\n";
}

sub foo1() {
  $myvar1 = $myvar;
    print $myvar1;
}

print "here we go!\n";
foo();
foo1();

However, I don't recommend this way of programming.

难如初 2024-09-11 08:21:50

你有几种方法。

最简单的就是不使用 my 声明变量。但这要求您避免use strict;,因此不建议这样做。

您可以在脚本顶部的函数外部声明变量。然后这个变量将可用于下面的所有函数。这是 scope 的结果:在一组大括号之外声明的变量是通常可在任何后续花括号内使用。

您可以使用 use vars qw/$myvar/; 编译指示来声明变量。这本质上使您的变量在以下代码中可用。

You have a few approaches.

The simplest is not to declare the variable with my. But this requires you to avoid use strict; and not recommended as a result.

You could declare your variable outside the functions at the top of your script. This variable would then be available to all functions below. This is a consequence of scope: variables declares outside a set of curly braces are generally available inside any subsequent curly braces.

You could declare your variable using the use vars qw/$myvar/; pragma. This inherently makes your variable available throughout the following code.

幽梦紫曦~ 2024-09-11 08:21:50

以下代码可以演示您所描述的解决方案:

#!/usr/bin/perl
use strict;
my $var = "hello";
sub foo {
    local *var;
    print "$var world\n";
    $var = "hi";
}

sub bar {
    local *var;
    print "$var world\n";
    $var = "hey";
}

foo();
bar();
print "$var world\n";

结果应该是:

hello world
hi world
hey world

The following code may demonstrate a solution to what you describe:

#!/usr/bin/perl
use strict;
my $var = "hello";
sub foo {
    local *var;
    print "$var world\n";
    $var = "hi";
}

sub bar {
    local *var;
    print "$var world\n";
    $var = "hey";
}

foo();
bar();
print "$var world\n";

The result should be:

hello world
hi world
hey world
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文