Perl-所有包含的脚本和模块中都可用全局变量吗? [英] Perl - Global variables available in all included scripts and modules?

查看:154
本文介绍了Perl-所有包含的脚本和模块中都可用全局变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以可以说我有一个main.pl脚本,在该脚本中,我需要声明一些变量(任何类型的变量常量或标准变量),并且这些变量需要在我将要包含的所有脚本和模块中使用.该main.pl脚本自动.

So lets say I have a main.pl script and in that script I need to declare some variables (any kind of variable constant or normal) and those variables need to be available through all scripts and modules that I'll include from that main.pl script automatically.

如果我在main.pl中有一个变量$myVar,并且在main.pl中有一个变量,我的意思是我需要script1.plscript2.plscript3.pm,并且需要访问那些脚本中的任何一个,就像您访问该特定脚本或模块中定义的任何var一样.

What I mean if I have a variable $myVar in main.pl and from main.pl I require script1.pl, script2.pl or script3.pm, and from anyone of those scripts I need to access $myVar as you would access any var defined in that specific script or module.

我已经在网上搜索过,但是我仅找到了一些示例,您可以在其中访问包含的脚本中的变量或从模块中提取变量.但这不是我想要的.

I've searched on the net, but I've only found examples where you can access variables from the scripts you include or extract variables from modules; but that's not what I want.

是否没有像PHP这样的关键字,您可以使用global $var1, $var2等在父脚本中使用变量?

Isn't there a keyword like in PHP where you would use global $var1, $var2 etc. to use variable from the parent script?

任何示例,文档或文章都是可以接受的-基本上任何可以帮助我完成的事情都会有所帮助.

Any example, documentation or article is acceptable - basically anything that could help me accomplish that would be helpful.

推荐答案

您可以使用our关键字声明全局变量:

You can declare global variables with the our keyword:

our $var = 42;

每个全局变量都有一个完全限定的名称,可用于从任何地方访问它.全名是程序包名称加上变量名称.如果此时尚未声明软件包,则您位于软件包main中,可以将其缩短为前导::.因此,上面的变量具有名称

Each global variable has a fully qualified name which can be used to access it from anywhere. The full name is the package name plus the variable name. If you haven't yet declared a package at that point, you are in package main, which can be shortened to a leading ::. So the above variable has the names

$var       # inside package main
$main::var # This is most obvious
$::var     # This may be a good compromise

如果我们使用了另一个软件包,则前缀会更改,例如

If we had used another package, the prefix would change, e.g.

package Foo;
our $bar = "baz";
# $Foo::bar from anywhere,
# or even $::Foo::bar or $main::Foo::bar

如果我们要使用不带前缀的变量,但在其他软件包下,则必须导出它.这通常是通过子类化Exporter来完成的,请参阅@Davids答案.但是,这只能提供来自use d包的变量,而不能提供相反的方式.例如

If we want to use a variable without the prefix, but under other packages, we have to export it. This is usually done by subclassing Exporter, see @Davids answer. However, this can only provide variables from packages that are being used, not the other way round. E.g.

Foo.pm:

package Foo;
use strict; use warnings;
use parent 'Exporter'; # imports and subclasses Exporter

our $var = 42;
our $not_exported = "don't look at me";

our @EXPORT = qw($var); # put stuff here you want to export
# put vars into @EXPORT_OK that will be exported on request

1;

script.pl:

#!/usr/bin/perl
# this is implicitly package main
use Foo; # imports $var

print "var = $var\n"; # access the variable without prefix
print "$Foo::not_exported\n"; # access non-exported var with full name

词法变量(用my声明)没有全局唯一的名称,并且不能在其静态范围之外访问.它们也不能与Exporter一起使用.

Lexical variables (declared with my) don't have globally unique names and can't be accessed outside their static scope. They also can't be used with Exporter.

这篇关于Perl-所有包含的脚本和模块中都可用全局变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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