Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

For God's sake, stop researching for a while and begin to think!


devel / comp.lang.perl.misc / Variable scopes?

SubjectAuthor
* Variable scopes?Otto J. Makela
+- Re: Variable scopes?E. Choroba
`- Re: Variable scopes?Eric Pozharski

1
Variable scopes?

<87eduwkriv.fsf@tigger.extechop.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=334&group=comp.lang.perl.misc#334

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: om@iki.fi (Otto J. Makela)
Newsgroups: comp.lang.perl.misc
Subject: Variable scopes?
Date: Tue, 25 Oct 2022 15:38:00 +0300
Organization: Games and Theory
Lines: 49
Message-ID: <87eduwkriv.fsf@tigger.extechop.net>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="ad4c5862634c9d6c0a4dee2af4fbb9c8";
logging-data="2105776"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+amqNwrZgGKcUksAvPlRuF"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:/FX/8hoQkPNFD3K/Bvd6h0BsmO0=
sha1:8efP0AGTQkQdSNGbScOKR04aTCc=
Mail-Copies-To: never
X-URL: http://www.iki.fi/om/
X-Face: 'g'S,X"!c;\pfvl4ljdcm?cDdk<-Z;`x5;YJPI-cs~D%;_<\V3!3GCims?a*;~u$<FYl@"E
c?3?_J+Zwn~{$8<iEy}EqIn_08"`oWuqO$#(5y3hGq8}BG#sag{BL)u8(c^Lu;*{8+'Z-k\?k09ILS
 by: Otto J. Makela - Tue, 25 Oct 2022 12:38 UTC

This is a boiled-down version of a bit of sofware where I discovered I
apparently don't fully understand variable scopes. In the following,
the "our" variable $request will be undef in the unnamed Start
subroutine if I just try to define it with "my". Apparently using it in
foreach is the cause, but I am a bit unclear on how/why this happens?

Anyone have pointers to web pages etc where this is spelled out?

Also, using a global variable here is obviously kludgy. Forgive me.

----

#!/usr/bin/perl
use strict;
use warnings;
use XML::Parser::Lite;

my $xmldata = <<'END';
<response>
<record value="first"/>
<record value="second"/>
</response>
END

our $request;

my $parser = XML::Parser::Lite->new(
Handlers => {
Start => sub {
shift;
# Only process "record" blocks
return unless shift eq 'record';
# Load parsed xml into hash for access
my %data=@_;
print $request,"\t",$data{'value'},"\n";
},
},
);

foreach $request ( qw(datarequest) ) {
# Here we would retrieve xml data from SOAP using $request etc
$parser->parse($xmldata);
}

--
/* * * Otto J. Makela <om@iki.fi> * * * * * * * * * */
/* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */
/* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */
/* * * Computers Rule 01001111 01001011 * * * * * * */

Re: Variable scopes?

<48606301-72ea-4d7a-870d-5a2eaf935c2en@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=335&group=comp.lang.perl.misc#335

  copy link   Newsgroups: comp.lang.perl.misc
X-Received: by 2002:a05:622a:1804:b0:39c:c7ba:4ac1 with SMTP id t4-20020a05622a180400b0039cc7ba4ac1mr34754434qtc.457.1666776882601;
Wed, 26 Oct 2022 02:34:42 -0700 (PDT)
X-Received: by 2002:a81:63c4:0:b0:349:543f:99f3 with SMTP id
x187-20020a8163c4000000b00349543f99f3mr37903137ywb.392.1666776882333; Wed, 26
Oct 2022 02:34:42 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.perl.misc
Date: Wed, 26 Oct 2022 02:34:41 -0700 (PDT)
In-Reply-To: <87eduwkriv.fsf@tigger.extechop.net>
Injection-Info: google-groups.googlegroups.com; posting-host=37.0.113.253; posting-account=USpXmwoAAAAOjE41JhQBd7rH9gy6hU1c
NNTP-Posting-Host: 37.0.113.253
References: <87eduwkriv.fsf@tigger.extechop.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <48606301-72ea-4d7a-870d-5a2eaf935c2en@googlegroups.com>
Subject: Re: Variable scopes?
From: choroba@matfyz.cz (E. Choroba)
Injection-Date: Wed, 26 Oct 2022 09:34:42 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3699
 by: E. Choroba - Wed, 26 Oct 2022 09:34 UTC

On Tuesday, October 25, 2022 at 2:38:06 PM UTC+2, Otto J. Makela wrote:
> This is a boiled-down version of a bit of sofware where I discovered I
> apparently don't fully understand variable scopes. In the following,
> the "our" variable $request will be undef in the unnamed Start
> subroutine if I just try to define it with "my". Apparently using it in
> foreach is the cause, but I am a bit unclear on how/why this happens?
>
> Anyone have pointers to web pages etc where this is spelled out?
>
> Also, using a global variable here is obviously kludgy. Forgive me.
>
> ----
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use XML::Parser::Lite;
>
> my $xmldata = <<'END';
> <response>
> <record value="first"/>
> <record value="second"/>
> </response>
> END
>
> our $request;
>
> my $parser = XML::Parser::Lite->new(
> Handlers => {
> Start => sub {
> shift;
> # Only process "record" blocks
> return unless shift eq 'record';
> # Load parsed xml into hash for access
> my %data=@_;
> print $request,"\t",$data{'value'},"\n";
> },
> },
> );
>
> foreach $request ( qw(datarequest) ) {
> # Here we would retrieve xml data from SOAP using $request etc
> $parser->parse($xmldata);
> }
>
> --
> /* * * Otto J. Makela <o...@iki.fi> * * * * * * * * * */
> /* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */
> /* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */
> /* * * Computers Rule 01001111 01001011 * * * * * * */

The code can be simplified, you don't need the XML parser to demonstrate the behaviour:

#! /usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

# Replace "our" with "my".
our $request = 10;

my $print = sub { say 'print ', \$request, ": $request" };

for $request (12) {
say 'for ', \$request, ": $request";
$print->();
}

You can see the *address* of the variable is different when you use my. I'm not sure it's the expected behaviour, though, as the documentation says:

| If the variable was previously declared with "my", it uses that variable instead of the global one, but it's still localized to the loop.

I've never used "for/foeach $var" without "my". It's a good habit to get into and you then just need to assign the actual iteration value to the outer closed over variable.

#! /usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my $request = 10;

my $print = sub { say 'print ', \$request, ": $request" };

for my $r (12) {
$request = $r;
say 'for ', \$request, ": $request";
$print->();
}

E. Choroba

Re: Variable scopes?

<slrntm70p9.9s9.whynot@orphan.zombinet>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=336&group=comp.lang.perl.misc#336

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: whynot@pozharski.name (Eric Pozharski)
Newsgroups: comp.lang.perl.misc
Subject: Re: Variable scopes?
Date: Thu, 03 Nov 2022 09:00:25 +0000
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <slrntm70p9.9s9.whynot@orphan.zombinet>
References: <87eduwkriv.fsf@tigger.extechop.net>
Injection-Info: reader01.eternal-september.org; posting-host="3094357fb37fc8b21cf340ed776a1f47";
logging-data="1531337"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Gfy7NKXKj/LOwiTYvp2kQ"
User-Agent: slrn/pre1.0.0-18 (Linux)
Cancel-Lock: sha1:pWfOxVwGup0i+QkAn5QyS8K+bwg=
 by: Eric Pozharski - Thu, 3 Nov 2022 09:00 UTC

with <87eduwkriv.fsf@tigger.extechop.net> Otto J. Makela wrote:

*SKIP*
> Also, using a global variable here is obviously kludgy. Forgive me.

I hope this horse is already dead and doesn't deserve any more beating.

*CUT*

--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


devel / comp.lang.perl.misc / Variable scopes?

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor