Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Defining global variable in a subroutine

by bihuboliya (Acolyte)
on Feb 22, 2012 at 13:20 UTC ( [id://955521]=perlquestion: print w/replies, xml ) Need Help??

bihuboliya has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am trying to declare and define some variables in a subroutine which should be available to the other subroutines in the same perl file. I am using 'use strict' for which I need to use some modifiers for the variables. 'my $foo' will limit the scope to the same subroutine. 'local $foo' will make $foo available to the subroutines called from the subroutine where $foo is declared and defined. Please let me know what do I need to use to make $foo avaiable to the other subroutines in the same perl file. Besides, is there any way that I declare and define all the variables in another plain/perl file and import them in my script? Thanks in advance...
  • Comment on Defining global variable in a subroutine

Replies are listed 'Best First'.
Re: Defining global variable in a subroutine
by Corion (Patriarch) on Feb 22, 2012 at 13:23 UTC

    See vars. The way to make a global variable known to Perl without prefixing its full name is:

    use strict; use vars qw($HELLO); $HELLO = 'Hello'; sub new_message { $HELLO = 'Nihao'; };
      No reason not to use a lexical.
      use strict; my $HELLO = 'Hello'; sub new_message { $HELLO = 'Nihao'; }
Re: Defining global variable in a subroutine
by choroba (Cardinal) on Feb 22, 2012 at 13:26 UTC
    You can not change the outer scope from within a sub. If you want to have a global variable, declare it in the topmost scope. For including a file, see do.

      choroba,

        "You can not change the outer scope from within a sub. If you want to have a global variable, declare it in the topmost scope."

      I believe you are referring to "use strict;", but if you run the following code with "# use strict;", you'll see that you can do what the OP wanted.

      my $in = result( 3 ); print "my \$in: $in \t \$out: \$out\n"; sub result { our $out = shift; return( 2 ** $out ); }

      This also will work in a forked environment, since the global 'our' variable is in the address space of the parent.

      However, I would use "use strict;" and predefine the global variables as you stated.

      Thank you

      "Well done is better than well said." - Benjamin Franklin

        This also will work in a forked environment, since the global 'our' variable is in the address space of the parent.

        Could you elaborate what you mean by that?

        Almost sounds like you are saying the child process could modify the variable in the parent process.  If so, I'd like to see a demo.

Re: Defining global variable in a subroutine
by JavaFan (Canon) on Feb 22, 2012 at 16:02 UTC
    You don't "modifiers" when using strict, and you don't need strict to use "modifiers".

    One way of doing what you want is to declare you shared variables as lexical variables in the most inner scope that is shared by your subroutines. In many cases, that's the file:

    #!/usr/bin/perl use 5.010; my $global; sub set_global {$global = shift} sub get_global {$global} sub say_global {say $global}
    If you want to import variables from a different file, the usual way of doing that is with the help of Exporter:
    package Blabla; our @ISA = qw[Exporter]; use Exporter(); our @EXPORT = qw[$global1 $global2 $global3];
    Then in any package that wants to use any of the variables, just do:
    use Blabla;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://955521]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found