How can I set a Java-like "static variable" in Perl that can be accessed by other classes ?

SriSri

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

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,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can a static variable from other file accessed here on the C program below?

From Dev

How can I create a HashMap that dynamically calls other Java classes?

From Dev

How can I set individual log levels in different Java classes?

From Dev

Why can't i use static variable in java constructor?

From Dev

How can a static variable (static in module scope) be accessed from another module? In C

From Dev

How can a static variable (global variable) be an object in Java?

From Dev

How can I set characters like "{" in urls?

From Dev

Can instance variable be declared as static variable in java

From Dev

Can instance variable be declared as static variable in java

From Dev

How can I disable blocks of code in Perl like with #ifdef in C?

From Dev

How to set up a private mediawiki or wordpress site that can be accessed from my other laptop?

From Dev

How can I directly access methods from my library class in other classes? Java

From Dev

How can the current number of i be accessed in a for of loop?

From Dev

How can I center div, inside other div with bootstrap classes?

From Dev

How can I call other shell script like a subroutine?

From Dev

How can I set properties outside of the constructor in JS just like in Java?

From Dev

How can I set up Samba shares to only be accessed by certain users?

From Dev

How can I set up Samba shares to only be accessed by certain users?

From Dev

How do I set up a Synology NAS so it can be accessed by URL/name instead of by IP address

From Dev

Is there anything like "maxQueryTime"? and if yes then How can I set mongo maxQueryTime?

From Dev

How can I create a set-like array in bash?

From Dev

How can i set an arduino pin act like a jumper?

From Dev

how can i set view like alert and hide automatically in iOS

From Dev

How can I run external programs using Perl 6? (e.g. like "system" in Perl 5)

From Dev

How can i set this path in java?

From Dev

How can I add an object into a set in Java?

From Dev

How can I set default applications in Unity Launcher for other users?

From Dev

How can I set default applications in Unity Launcher for other users?

From Dev

How can I set variable in other namespace equal to variable in procedure?

Related Related

  1. 1

    How can a static variable from other file accessed here on the C program below?

  2. 2

    How can I create a HashMap that dynamically calls other Java classes?

  3. 3

    How can I set individual log levels in different Java classes?

  4. 4

    Why can't i use static variable in java constructor?

  5. 5

    How can a static variable (static in module scope) be accessed from another module? In C

  6. 6

    How can a static variable (global variable) be an object in Java?

  7. 7

    How can I set characters like "{" in urls?

  8. 8

    Can instance variable be declared as static variable in java

  9. 9

    Can instance variable be declared as static variable in java

  10. 10

    How can I disable blocks of code in Perl like with #ifdef in C?

  11. 11

    How to set up a private mediawiki or wordpress site that can be accessed from my other laptop?

  12. 12

    How can I directly access methods from my library class in other classes? Java

  13. 13

    How can the current number of i be accessed in a for of loop?

  14. 14

    How can I center div, inside other div with bootstrap classes?

  15. 15

    How can I call other shell script like a subroutine?

  16. 16

    How can I set properties outside of the constructor in JS just like in Java?

  17. 17

    How can I set up Samba shares to only be accessed by certain users?

  18. 18

    How can I set up Samba shares to only be accessed by certain users?

  19. 19

    How do I set up a Synology NAS so it can be accessed by URL/name instead of by IP address

  20. 20

    Is there anything like "maxQueryTime"? and if yes then How can I set mongo maxQueryTime?

  21. 21

    How can I create a set-like array in bash?

  22. 22

    How can i set an arduino pin act like a jumper?

  23. 23

    how can i set view like alert and hide automatically in iOS

  24. 24

    How can I run external programs using Perl 6? (e.g. like "system" in Perl 5)

  25. 25

    How can i set this path in java?

  26. 26

    How can I add an object into a set in Java?

  27. 27

    How can I set default applications in Unity Launcher for other users?

  28. 28

    How can I set default applications in Unity Launcher for other users?

  29. 29

    How can I set variable in other namespace equal to variable in procedure?

HotTag

Archive