调用脚本并从被调用脚本传递值
我有一个脚本试图从另一个脚本调用,将正在调用的脚本中的信息传递给调用脚本。当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于 $hold 是如何声明的。
如果它是按词法声明的(使用“my $hold...”),那么您无法直接获取它 - 它只能在 Called_script.pl 的范围内访问。
如果它是动态范围的(本地 $hold 或我们的 $hold),那么您应该能够通过在其下声明的包作为前缀来获取它(因此,如果它在“package Foo;”中,您可以将其获取为 $ Foo::hold")。
也就是说...
您通常不想在脚本之间传递变量。在全局变量中存储状态可能会导致一些令人讨厌的调试会话。
作为第一步,您可能需要封装对 $ 的访问在子例程中保持,所以在 Called_script.pl 中你有类似的内容:
调用时将返回 $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:
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.
你已经开始走上正确的道路,但还有很长的路要走。您应该使用模块和
use
语句,而不是代码和require
语句。您应该尝试阅读perldoc perlmod
和perldoc perlmodlib
,但一般要点是:将脚本视为骨架,将函数视为充实骨骼。
这是一个简单的模块和使用它的脚本:
ExampleModule.pm:
example.pl:
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 therequire
statement. You should try readingperldoc perlmod
andperldoc perlmodlib
, but the general gist is: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:
example.pl: