1

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

Thanks in advance

G. Cito
  • 6,210
  • 3
  • 29
  • 42
SriSri
  • 95
  • 1
  • 13
  • 5
    Use `our` to declare the variable as package one (i.e., `package A; our $foo = 1`, then access it via package name resolution (`print $A::foo;`) – raina77ow Apr 22 '14 at 12:47
  • 1
    Package can also be made exportable, if you so desire. – ikegami Apr 22 '14 at 12:57
  • 2
    That said, it's usually better to use an accessor. – ikegami Apr 22 '14 at 12:57
  • 1
    I have package Script::Test ...In this package i initialized "our XYZ= ABC" and from other package i called it as $Script::Test::XYZ" ..this is not working ...can you correct me where i am wrong .. Should i add "use Script::Test" ? – SriSri Apr 22 '14 at 13:11
  • 2
    @SriSri: Yes. It's like adding `import` in Java. – choroba Apr 22 '14 at 13:25
  • 1
    I think the problem is accessibility of packages , I have something like this : lib\script\test , Test.pm is one class ....... lib\TestA\TestB\Nav.pm .. Nav.pm is the second file ... but as long as Lib is accessible , i should be able to call it as $lib::Script::Test::XYZ ..am i right ..Still not working ..am trying .. – SriSri Apr 22 '14 at 14:18
  • 1
    Got It ....My mistake ..i was assigning an array and looking for output ..assigned proper value ..am able to access the variable ..Thankyou all .. – SriSri Apr 22 '14 at 14:38
  • 2
    @SriSri: If you have resolved the problem then please accept one of the answers, or write your own (and accept it) if none of them helped – Borodin Apr 22 '14 at 15:12

2 Answers2

1
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
davewood
  • 196
  • 8
0

I don't know Java really so I'm guessing that what you mean by "static variable" is something to do with scoping? In perl 'my' and 'our' are the ways you can control scope, but I believe I am correct in saying hat packages/modules make variable's scope "private" to the .pm file they are declared in (correct this and/or elaborate further fellow perlistas!).

As for how to "access" them hmm my copy of Programming Perl (2nd Edition) covers this in chapter 2 in the section on Scoped Declarations. But here's a pithy (slightly edited) part of the first footnote from page 107:

Packages are used by libraries, modules, and classes to store their own private data so it doesn't conflict with data in your main program. If you see someone write $Some::stuff they're using the $stuff scalar variable from the package Some.

The Exporter documentation and this perlmonks node about global variables might help you get clearer in your thinking about variables and scope in perl. The classic perlmonks node - Variable Scoping in Perl: the basics - is a frequently consulted reference :-)

If you already know how to program (i.e. in Java) sometimes another good reference (only slightly dated) for "how to" do things in Perl is The Perl Cookbook - excerpts of which you can find online.

Cheers,

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
G. Cito
  • 6,210
  • 3
  • 29
  • 42
  • 1
    Wonderful information , I am good @ java programming & needed to add Perl code for some issues , Very helpful info added by you ..Thankyou. – SriSri Apr 23 '14 at 15:56
  • If you have time watch an hour long talk about namespaces (and other topics as well) see Bruce Gray's talk at **YAPC::NA 2012** - [The why of my()](https://www.youtube.com/watch?v=SA4mUs3Ro98). There's a good story in there about how a pretty expert `perl` programmer wrapped his head around `perl` and namespaces. – G. Cito May 02 '14 at 16:25