How can I set a “static variable” like Java in Perl that other classes can access?

I have two classes: A and B, I need to set a static variable in class A (for example, static variables in Java) and access the variable from class B (using the name ClassName.variable in Java). Can I do something like this in Perl.

Thanks in advance

+1
source share
2 answers

I don’t know if Java really is, so I assume that what you mean by “static variable” is related to scoping? There are ways to control the scope in perl 'my' and 'our', but I think I'm right in saying that the hat packages / modules make the scope variable "private" in the file .pmin which they are declared (correct this and / or clarify further companions perlistas!).

Regarding how to “access” them, my copy of Perl Programming (2nd Edition) covers this in Chapter 2 under the “Ads with Area” section. But here is the informative (slightly edited) part of the first footnote from page 107:

, , . , - $Some::stuff, $stuff Some.

Exporter perlmonks node perl. perlmonks node - Scope Perl: - : -)

, (.. Java), ( ) "" Perl - Perl Cookbook - , .

,

0
tree . 
    ├── foo.pl
    └── lib
         ├── A.pm
         └── B.pm


cat lib/A.pm 
package A;
use strict;
use warnings;
our $foo = 7;
1;


cat lib/B.pm 
package B;
use strict;
use warnings;
use feature qw/ say /;
use A;
say $A::foo;
1;


cat foo.pl 
#!/usr/bin/env perl
use strict;
use warnings;
use B;


perl -Ilib foo.pl 
7
+1

Source: https://habr.com/ru/post/1545420/


All Articles