调用脚本并从被调用脚本传递值

发布于 2024-09-16 04:54:56 字数 356 浏览 2 评论 0原文

我有一个脚本试图从另一个脚本调用,将正在调用的脚本中的信息传递给调用脚本。当我使用 do 或 require 时,它​​会运行但不会传递该值。

前任。

我调用的脚本底部有以下行

script.pl

print " $hold IS VALUE\n";

这会打印出hold的值。

然后,我使用以下命令启动调用脚本:

calling_script.pl

require 'acc_option.pl'; print "HOLD PASSED IS $hold\n";

但变量保持不打印。

调用这个脚本的最佳方法是什么,而不是将所有内容都放在一页上?

I have one script i'm trying to call from another script, passing the information from the script that is being called to the calling script. When i use do or require it runs through but doesnt pass the value.

ex.

I have the following line at the bottom of the script that i am calling

called script.pl

print " $hold IS VALUE\n";

which prints me the value of hold.

I then start the calling script with:

calling_script.pl

require 'acc_option.pl';
print "HOLD PASSED IS $hold\n";

but the variable hold doesnt print.

Whats the best way to call this script instead of putting everything on one long ass page?

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

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

发布评论

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

评论(2

眼藏柔 2024-09-23 04:54:56

这取决于 $hold 是如何声明的。

如果它是按词法声明的(使用“my $hold...”),那么您无法直接获取它 - 它只能在 Called_script.pl 的范围内访问。

如果它是动态范围的(本地 $hold 或我们的 $hold),那么您应该能够通过在其下声明的包作为前缀来获取它(因此,如果它在“package Foo;”中,您可以将其获取为 $ Foo::hold")。

也就是说...

您通常不想在脚本之间传递变量。在全局变量中存储状态可能会导致一些令人讨厌的调试会话。

作为第一步,您可能需要封装对 $ 的访问在子例程中保持,所以在 Called_script.pl 中你有类似的内容:

sub is_on_hold { return $hold };

调用时将返回 $hold (我假设 $hold 是某种布尔状态指示器。如果它没有以适当的意图命名你的子例程揭示方式:-)

如果您更详细地描述如何尝试使用 $hold,人们可能能够就更好地完成任务的方式提供一些更具体的建议。

It depends on how $hold was declared.

If it was lexically declared (with "my $hold...") then you can't get at it directly - it's only accessible within the scope of called_script.pl.

If it's dynamically scoped (local $hold, or our $hold) then you should be able to get at it by prefixing it with the package it was declared under (so if it's in "package Foo;" you can get at it as $Foo::hold").

That said...

You generally don't want to mess around passing variables between scripts. Storing state in global variables can make for some nasty debugging sessions.

As a first step you might want to encapsulate accessing $hold inside a subroutine so in called_script.pl you have something like:

sub is_on_hold { return $hold };

which will return $hold when called (I'm assuming here that $hold is some kind of boolean state indicator. If it isn't name your subroutine in an appropriately intention revealing way :-)

If you describe how you're trying to use $hold in a bit more detail folk might be able to give some more specific advice on a better way of doing your task.

海未深 2024-09-23 04:54:56

你已经开始走上正确的道路,但还有很长的路要走。您应该使用模块和 use 语句,而不是代码和 require 语句。您应该尝试阅读 perldoc perlmodperldoc perlmodlib,但一般要点是:

  1. 将流程分解为函数
  2. ,根据功能对这些函数进行分组
  3. 将函数组放入模块中
  4. 编写一个使用模块并调用函数的脚本

将脚本视为骨架,将函数视为充实骨骼。

这是一个简单的模块和使用它的脚本:

ExampleModule.pm:

package ExampleModule;

use strict;
use warnings;

use base 'Exporter';

our @EXPORT_OK  = qw/do_first_thing do_second_thing do_third_thing/;

sub do_first_thing {
    my ($thing) = @_;

    return $thing + 1;
}

sub do_second_thing {
    my ($thing) = @_;

    return $thing + 1;
}

sub do_third_thing {
    my ($thing) = @_;

    return $thing + 1;
}

1;

example.pl:

#!/usr/bin/perl

use strict;
use warnings;

use ExampleModule qw/do_first_thing do_second_thing do_third_thing/;

my $thing = 0;

$thing = do_first_thing($thing);
$thing = do_second_thing($thing);
$thing = do_third_thing($thing);

print "$thing\n";

You have started down the right path, but are still a ways off. You should be using modules and the use statment, not code and the require statement. You should try reading perldoc perlmod and perldoc perlmodlib, but the general gist is:

  1. decompose your process into functions
  2. group those functions by what they do
  3. put the groups of functions into modules
  4. write a script that uses the modules and calls the functions

Think of the script as a skeleton and the functions as fleshing out the skeleton.

Here is a simple module and a script that uses it:

ExampleModule.pm:

package ExampleModule;

use strict;
use warnings;

use base 'Exporter';

our @EXPORT_OK  = qw/do_first_thing do_second_thing do_third_thing/;

sub do_first_thing {
    my ($thing) = @_;

    return $thing + 1;
}

sub do_second_thing {
    my ($thing) = @_;

    return $thing + 1;
}

sub do_third_thing {
    my ($thing) = @_;

    return $thing + 1;
}

1;

example.pl:

#!/usr/bin/perl

use strict;
use warnings;

use ExampleModule qw/do_first_thing do_second_thing do_third_thing/;

my $thing = 0;

$thing = do_first_thing($thing);
$thing = do_second_thing($thing);
$thing = do_third_thing($thing);

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