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

查看:18
本文介绍了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.plmain.pl 中有一个变量 $myVar 我需要 script1.plscript2.plscript3.pm,以及我需要像您一样从这些脚本中的任何一个访问 $myVar访问在该特定脚本或模块中定义的任何变量.

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 答案.然而,这只能提供来自正在used 的包的变量,而不是相反.例如

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
"; # access the variable without prefix
print "$Foo::not_exported
"; # 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天全站免登陆