[26127] in Perl-Users-Digest
Perl-Users Digest, Issue: 8320 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Aug 13 18:05:19 2005
Date: Sat, 13 Aug 2005 15:05:04 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 13 Aug 2005 Volume: 10 Number: 8320
Today's topics:
How do I use global variables safely? <graham@letsgouk.com>
Re: How do I use global variables safely? <klaus_gb@yahoo.com>
Re: How do I use global variables safely? <noreply@gunnar.cc>
Re: How do I use global variables safely? <matthew.garrish@sympatico.ca>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 13 Aug 2005 12:34:34 +0100
From: "Graham" <graham@letsgouk.com>
Subject: How do I use global variables safely?
Message-Id: <42fddbea.0@entanet>
I want to be able to allow a variable to be set in one subroutine, and use
its value in another subroutine, but I obviously can't do this if I use the
'my' or 'local' functions on the variable in each subroutine. You might say
just dispense with 'my' or 'local' and let the variable be global. Well I
was doing that and had no problem until I used ActiveState's 'Perl App' to
create a standalone executable. This doesn't seem to like global variables
and is virtually telling me I must 'use strict', and I would if I could, but
I don't know how to transfer a vairiable's value (I never did fully
understand lexical scope) Any ideas anyone?
------------------------------
Date: Sat, 13 Aug 2005 14:20:36 +0100
From: "Klaus" <klaus_gb@yahoo.com>
Subject: Re: How do I use global variables safely?
Message-Id: <42fde3d3$0$27362$636a15ce@news.free.fr>
"Graham" <graham@letsgouk.com> wrote in message news:42fddbea.0@entanet...
> I want to be able to allow a variable to be set in one subroutine, and use
> its value in another subroutine, but I obviously can't do this if I use
the
> 'my' or 'local' functions on the variable in each subroutine.
correct.
> You might say
> just dispense with 'my' or 'local' and let the variable be global.
That's one solution
> Well I
> was doing that and had no problem until I used ActiveState's 'Perl App' to
> create a standalone executable. This doesn't seem to like global variables
> and is virtually telling me I must 'use strict',
I strongly support 'use strict' in any Perl program wich is longer than 2
lines of code.
> and I would if I could, but
> I don't know how to transfer a vairiable's value (I never did fully
> understand lexical scope) Any ideas anyone?
Idea 1:
Alwayse use strict and warnings: just let the variable be global (i.e. a
package-variable) and use the correct package (that would be "$::") to acces
the variable.
==============================
use strict;
use warnings;
set_glob();
use_glob();
sub set_glob { $::glob = "xxx" }
sub use_glob { print "glob is '$::glob'\n"; }
==============================
Idea 2:
Alwayse use strict and warnings: just let the variable be global (i.e. a
package-variable) and employ "our" to make the very same global variable
accessible in each function.
==============================
use strict;
use warnings;
set_glob();
use_glob();
sub set_glob { our $glob = "xxx" }
sub use_glob { our $glob; print "glob is '$glob'\n" }
==============================
Idea 3:
Alwayse use strict and warnings: just let the variable be a lexical scope
variable (i.e. introduced by "my") outside the definition of both functions.
This lexical scope variable is then automatically accessible in each
function.
==============================
use strict;
use warnings;
my $glob;
set_glob();
use_glob();
sub set_glob { $glob = "xxx" }
sub use_glob { print "glob is '$glob'\n" }
==============================
--
Klaus
------------------------------
Date: Sat, 13 Aug 2005 17:11:34 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How do I use global variables safely?
Message-Id: <3m6gtbF11p66tU1@individual.net>
Klaus wrote:
> "Graham" wrote:
>> I want to be able to allow a variable to be set in one subroutine, and use
>> its value in another subroutine,
<snip>
>> I don't know how to transfer a vairiable's value (I never did fully
>> understand lexical scope)
>
> Idea 1:
<three ways to use global variables snipped>
Better yet, idea 4:
Learn how to pass values between subroutines.
==============================
use strict;
use warnings;
my $var = set_var();
use_var($var);
sub set_var {
my $var = 'xxx';
return $var;
}
sub use_var {
my $var = shift;
print "var is '$var'\n";
}
==============================
See "perldoc perlsub".
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sat, 13 Aug 2005 11:01:55 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: How do I use global variables safely?
Message-Id: <HXnLe.22161$6d4.1539427@news20.bellglobal.com>
"Klaus" <klaus_gb@yahoo.com> wrote in message
news:42fde3d3$0$27362$636a15ce@news.free.fr...
> "Graham" <graham@letsgouk.com> wrote in message news:42fddbea.0@entanet...
>> I want to be able to allow a variable to be set in one subroutine, and
>> use
>> its value in another subroutine, but I obviously can't do this if I use
>
> Idea 3:
> Alwayse use strict and warnings: just let the variable be a lexical scope
> variable (i.e. introduced by "my") outside the definition of both
> functions.
> This lexical scope variable is then automatically accessible in each
> function.
> ==============================
> use strict;
> use warnings;
> my $glob;
> set_glob();
> use_glob();
> sub set_glob { $glob = "xxx" }
> sub use_glob { print "glob is '$glob'\n" }
> ==============================
>
I personally hate using globals like that. It gives you no indication of
where the value is being set or used (except in simple examples like this),
which inevitably means sifting through lines and lines of code if something
goes wrong. My preference would be to go with the obvious:
my $glob = set_glob();
use_glob($glob);
sub set_glob { return 'xxx'; }
sub use_glob { my $temp_glob = shift; print "glob is $temp_glob\n"; }
Matt
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 8320
***************************************