Re: my OR our that is the question ?!
On Fri, Aug 10, 2001 at 12:22:43AM +0300, raptor wrote: thanx, Yes I see ... but I was interested WHY when we are in nested subroutines ... the inner-one will see the lexical var only the first time !! I mean the Note that there are _no_ nested subroutines in Perl. You may declare one inside of the code of other, but that doesn't mean the scoping is preserved. So what you basicaly have are two global subroutines with local values. You get the closure for the first invocation. But it's not the same variable anymore. PS: Please trim the text you're answering to. -- Honza Pazdziora | [EMAIL PROTECTED] | http://www.fi.muni.cz/~adelton/ .project: Perl, DBI, Oracle, MySQL, auth. WWW servers, DBD::XBase. Will be off email until Aug 15 -- please don't expect responses until then.
Re: my OR our that is the question ?!
Note that there are _no_ nested subroutines in Perl ..So what you basicaly have are two global subroutines with local values. You get the closure for the first invocation. But it's not the same variable anymore. ]- Now understand thanx alot = iVAN [EMAIL PROTECTED] =
my OR our that is the question ?!
hi, I have the following situation ... it is not big issue but i'm interested why this happen .. so here is it : my $ID = 555; print $ID; sub blah { .. { local $$dbh{AutoCommit} = 0; eval { local $$dbh{RaiseError} = 1; $ID = selectrow query;#say it returns 111 }; . }; print $ID; };#end blah print $ID So this shortened example prints sometimes : 555 111 111- correct rarely : 555 111 555 --- not correct OK... this seemed to me like uninitialised variable ... or there is special behaviour when using lexical-var insde eval but the problem dissappeared when I declared it like that : our $ID; Could someone explain me why this happen. = iVAN [EMAIL PROTECTED] =
Re: my OR our that is the question ?!
Hello, rI have the following situation... it is not big issue but ri'm interested why this happen... r rmy $ID = 555; rsub blah { r... r$ID = selectrow query; r... r} This is, in fact, a big issue. You should see a message in your error log shared variable $ID will not stay shared. You should not use a my variable defined at the top level of your Apache::Registry script in a subroutine. See this entry in the mod_perl guide: http://perl.apache.org/guide/perl.html#my_Scoped_Variable_in_Nested_S The workaround is to make $ID a package global (use vars qw($ID)). Humbly, Andrew -- Andrew Ho http://www.tellme.com/ [EMAIL PROTECTED] Engineer [EMAIL PROTECTED] Voice 650-930-9062 Tellme Networks, Inc. 1-800-555-TELLFax 650-930-9101 --
Re: my OR our that is the question ?!
On Thu, Aug 09, 2001 at 01:36:28PM -0700, Andrew Ho wrote: Hello, rI have the following situation... it is not big issue but ri'm interested why this happen... r rmy $ID = 555; rsub blah { r... r$ID = selectrow query; r... r} This is, in fact, a big issue. You should see a message in your error log shared variable $ID will not stay shared. You should not use a my variable defined at the top level of your Apache::Registry script in a subroutine. See this entry in the mod_perl guide: http://perl.apache.org/guide/perl.html#my_Scoped_Variable_in_Nested_S The workaround is to make $ID a package global (use vars qw($ID)). With Perl 5.6, the following are roughly equivalent: use vars qw($ID); our $ID; However, you will need to put the `our' declaration within the block (which may happen implicitely with Apache::Registry). So (pre-5.6): use vars qw($ID); sub foo { $ID = shift; } But (= 5.6): sub foo { our $ID = shift; } --James
Re: my OR our that is the question ?!
thanx, Yes I see ... but I was interested WHY when we are in nested subroutines ... the inner-one will see the lexical var only the first time !! I mean the REASON of this perl behaviour ! It it closer/easier to think of it that my-vars are visible all the time in their inner scopes (except if u are going outside the scope of the variable itself) .. what I mean if I'm not very clear ... it is easy to think that : { $var is not visible here { my $var = 5; { { sub blah {{ sub xxx{ $var is visible here all the time, not just the first one } } } } } } $var is not visible here } On the other hand 'our' and use vars make a global var which as stated anywhere is not a good programming practice (only in some special cases, this is not one of them I think :) / i mean use global where U need global use local var where u need local/ ). {note : not local-op } So I'm using ASP.pm which compiles all scripts into one package (by default) so the purpose of lexical-scope splitconquer is no more valid... I mean that the variable 'say $ID into script blah.pl I want to be differnt from the $ID var into xxx.pl i.e. I don't want this : package AllCompiledScripts; our $ID; sub compiled_blah { $ID =15;#or if u like our $ID = 15 sub inner_blah{ }; }; sub compiled_xxx { $ID =555 sub inner_xxx{ }; }; 1; I want this : package AllCompiledScrips; compiled_blah :: $ID is not visible here compiled_xxx :: $ID is not visible here sub compiled_blah { my $ID =15 sub inner_blah{ compiled_blah :: $ID visible here but compiled_xxx :: $ID not visible }; }; sub compiled_xxx { my $ID =555 sub inner_xxx{ compiled_xxx :: $ID visible here but compiled_blah :: $ID not visible }; }; compiled_blah :: $ID is not visible here compiled_xxx :: $ID is not visible here 1; So what is the REASON for this copy-on-first-call behaviour.(there have to be some reason, if i'm not totaly dull to see it ) Thanx alot for your attention = iVAN [EMAIL PROTECTED] = rI have the following situation... it is not big issue but ri'm interested why this happen... r rmy $ID = 555; rsub blah { r... r$ID = selectrow query; r... r} This is, in fact, a big issue. You should see a message in your error log shared variable $ID will not stay shared. You should not use a my variable defined at the top level of your Apache::Registry script in a subroutine. See this entry in the mod_perl guide: http://perl.apache.org/guide/perl.html#my_Scoped_Variable_in_Nested_S The workaround is to make $ID a package global (use vars qw($ID)). With Perl 5.6, the following are roughly equivalent: use vars qw($ID); our $ID; However, you will need to put the `our' declaration within the block (which may happen implicitely with Apache::Registry). So (pre-5.6): use vars qw($ID); sub foo { $ID = shift; } But (= 5.6): sub foo { our $ID = shift; }
Re: my OR our that is the question ?!
So what is the REASON for this copy-on-first-call behaviour.(there have to be some reason, if i'm not totaly dull to see it ) It's called a closure. You can read up on it in the Camel book or in Damian Conway's OO book. - Perrin
Re: my OR our that is the question ?!
didn't thought of that :)), but what will broke if the var is copied/aliased every time not just the first time ... I mean the call to closure make a new instance of the sub() every time isn't it ?!? ! I see the closures get bound every-time , but non closures only the first time ! But still can figure out ...if I'm thinking correctly . - closure makes a new instance of of itself and get the latest value of outer-lexical variable in its private space. - there is no reason why normal sub() can get/alias! this value every time, this doens't broke the closure behaviour (i.e. normal sub() doesn't need to make its own private copy but use the outer-scope-lexical ) or I'm wrong again. = iVAN [EMAIL PROTECTED] =
OT: Re: my OR our that is the question ?!
On Fri, Aug 10, 2001 at 01:08:12AM +0300, raptor wrote: didn't thought of that :)), but what will broke if the var is copied/aliased every time not just the first time ... I mean the call to closure make a new instance of the sub() every time isn't it ?!? ! I see the closures get bound every-time , but non closures only the first time ! But still can figure out ...if I'm thinking correctly . - closure makes a new instance of of itself and get the latest value of outer-lexical variable in its private space. - there is no reason why normal sub() can get/alias! this value every time, this doens't broke the closure behaviour (i.e. normal sub() doesn't need to make its own private copy but use the outer-scope-lexical ) or I'm wrong again. This really is more of a Perl topic. Many people mistake anonymous subs and closures. The two are orthoginal: anonymous subs can be closures, named subs and be closures, anonymous subs might not be closures, named subs likewise. It all depends on if named lexical variables are in scope when the sub is defined. --James
Re: my OR our that is the question ?!
On Thu, 9 Aug 2001, Perrin Harkins wrote: So what is the REASON for this copy-on-first-call behaviour.(there have to be some reason, if i'm not totaly dull to see it ) It's called a closure. You can read up on it in the Camel book or in Damian Conway's OO book. oh even here: http://perl.apache.org/guide/perl.html#Understanding_Closures_the_Ea _ Stas Bekman JAm_pH -- Just Another mod_perl Hacker http://stason.org/ mod_perl Guide http://perl.apache.org/guide mailto:[EMAIL PROTECTED] http://apachetoday.com http://eXtropia.com/ http://singlesheaven.com http://perl.apache.org http://perlmonth.com/