explicitly declare closures???
Just thought I'd run the following up the flagpole to see if anyone laughs at it Closures are useful, powerful things, but they can also be dangerous and counter-intuitive, espcially to the uninitiated. For example, how many people could say what the following should output, with and without $x commented out, and why: { my $x = "bar"; sub foo { # $x # <- uncommenting this line changes the outcome return sub {$x}; } } print foo()->(); One possibility in perl 6 would be to require outer lexicals used in an inner sub to be explicitly declared, eg sub foo { my $x = $_[0]; return sub { "hello $x" } } in perl5, this implicitly creates a closure; in perl6, this gives a compile time error of 'no such variable $x at line 2' while sub foo { my $x = $_[0]; return sub { my closure $x; "hello $x" } } gives the perl5-ish behaviour ie by default lexicals are only in scope in their own sub, not within nested subs - and you have to explicitly 'import' them to use them. The main difficuly is whether globals should be masked by outer lexicals, ie $x = 'global'; { my $x = 'outer lex'; $foo = sub { $x }; } should the sub give a 'no such variable $x' compile time error, or just return the current value of $main::x ?? Maybe whether closures can be implicity created could be governed by a 'use strict closures' pragma? In which case this "feature" could be added to perl5 too. Anyway, I've got to go now, those nice men in the white coats are bringing me my medicine Dave M.
RE: HOw to Unsub
Usually the generic way is to send email to [EMAIL PROTECTED], so in your case try [EMAIL PROTECTED] Ilya -Original Message- From: Patel, Sharad To: Eric Roode; [EMAIL PROTECTED] Sent: 08/21/2001 7:22 AM Subject: HOw to Unsub HI Guys Sorry for this but I need to know how to Unsubscribe. Any ideas ?? Regards -Original Message- From: Eric Roode [mailto:[EMAIL PROTECTED]] Sent: Tuesday, August 21, 2001 2:22 PM To: [EMAIL PROTECTED] Subject: Re: explicitly declare closures??? John Porter wrote: > >Dave Mitchell wrote: >> ie by default lexicals are only in scope in their own sub, not within >> nested subs - and you have to explicitly 'import' them to use them. > >No. People who write closures know what they're doing. > >When's the last time someone "accidentally" wrote a closure? People using mod_perl do it all the time: http://www.perlreference.com/mod_perl/guide/obvious.html I don't agree with the original proposal, since 'use diagnostics' will tell you about closures -- but people DO create closures by mistake, and it can be a very difficult bug to trace if you aren't used to closures. -- Eric J. Roode[EMAIL PROTECTED] Senior Software Engineer, Myxa Corporation Email Disclaimer The information in this email is confidential and may be legally privileged. It is intended solely for the addressee. Access to this email by anyone else is unauthorised. If you are not the intended recipient, any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. When addressed to our clients any opinions or advice contained in this email are subject to the terms and conditions expressed in the governing KPMG client engagement letter.
Re: explicitly declare closures???
Dave Mitchell wrote: > ie by default lexicals are only in scope in their own sub, not within > nested subs - and you have to explicitly 'import' them to use them. No. People who write closures know what they're doing. When's the last time someone "accidentally" wrote a closure? -- John Porter Science class should not end in tragedy.
Re: explicitly declare closures???
John Porter wrote: > >Dave Mitchell wrote: >> ie by default lexicals are only in scope in their own sub, not within >> nested subs - and you have to explicitly 'import' them to use them. > >No. People who write closures know what they're doing. > >When's the last time someone "accidentally" wrote a closure? People using mod_perl do it all the time: http://www.perlreference.com/mod_perl/guide/obvious.html I don't agree with the original proposal, since 'use diagnostics' will tell you about closures -- but people DO create closures by mistake, and it can be a very difficult bug to trace if you aren't used to closures. -- Eric J. Roode[EMAIL PROTECTED] Senior Software Engineer, Myxa Corporation
HOw to Unsub
HI Guys Sorry for this but I need to know how to Unsubscribe. Any ideas ?? Regards -Original Message- From: Eric Roode [mailto:[EMAIL PROTECTED]] Sent: Tuesday, August 21, 2001 2:22 PM To: [EMAIL PROTECTED] Subject: Re: explicitly declare closures??? John Porter wrote: > >Dave Mitchell wrote: >> ie by default lexicals are only in scope in their own sub, not within >> nested subs - and you have to explicitly 'import' them to use them. > >No. People who write closures know what they're doing. > >When's the last time someone "accidentally" wrote a closure? People using mod_perl do it all the time: http://www.perlreference.com/mod_perl/guide/obvious.html I don't agree with the original proposal, since 'use diagnostics' will tell you about closures -- but people DO create closures by mistake, and it can be a very difficult bug to trace if you aren't used to closures. -- Eric J. Roode[EMAIL PROTECTED] Senior Software Engineer, Myxa Corporation Email Disclaimer The information in this email is confidential and may be legally privileged. It is intended solely for the addressee. Access to this email by anyone else is unauthorised. If you are not the intended recipient, any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. When addressed to our clients any opinions or advice contained in this email are subject to the terms and conditions expressed in the governing KPMG client engagement letter.
Re: explicitly declare closures???
On Tue, Aug 21, 2001 at 09:21:35AM -0400, Eric Roode wrote: > John Porter wrote: > > > >Dave Mitchell wrote: > >> ie by default lexicals are only in scope in their own sub, not within > >> nested subs - and you have to explicitly 'import' them to use them. > > > >No. People who write closures know what they're doing. > > > >When's the last time someone "accidentally" wrote a closure? > > People using mod_perl do it all the time: > http://www.perlreference.com/mod_perl/guide/obvious.html > > I don't agree with the original proposal, since 'use diagnostics' > will tell you about closures -- but people DO create closures by > mistake, and it can be a very difficult bug to trace if you aren't > used to closures. You are refering only to one type of closure, nested subroutines. And if people make these by mistake then perl will tell them with a warning like Variable "$x" will not stay shared Graham.
Re: explicitly declare closures???
Dave Mitchell <[EMAIL PROTECTED]> writes: > Just thought I'd run the following up the flagpole to see if anyone > laughs at it > > Closures are useful, powerful things, but they can also be > dangerous and counter-intuitive, espcially to the uninitiated. For example, > how many people could say what the following should output, > with and without $x commented out, and why: > > { > my $x = "bar"; > sub foo { > # $x # <- uncommenting this line changes the outcome > return sub {$x}; > } > } > print foo()->(); Well, I would expect it to output 'foo' on both occasions, and I'm more than a little surprised to discover that it doesn't. Looks like a bug to me. -- Piers Cawley www.iterative-software.com
Re: explicitly declare closures???
Piers Cawley <[EMAIL PROTECTED]> wrote: > > { > > my $x = "bar"; > > sub foo { > > # $x # <- uncommenting this line changes the outcome > > return sub {$x}; > > } > > } > > print foo()->(); > > Well, I would expect it to output 'foo' on both occasions, and I'm > more than a little surprised to discover that it doesn't. Looks like a > bug to me. Using the notation $outer:x, $foo:x and $anon:x to refer to whatever $x might be in the 3 scopes: With the $x: foo() is a closure created at compile time. By the time the main {} block has been executed (but before foo() is called), the $outer:x is undef, and $foo:x is 'bar' (standard closure stuff). When foo() is executed, the anon sub is cloned, and at that time, $anon:x is set from from foo's pad, so it gets 'bar'. Without the $x: foo is no longer a closure - ie it doesnt have a private copy of $x in its pad. At cloning time, sub {$x} picks up its value of $x from $outer:x, since there isn't a $x in foo's pad - thus it picks up 'undef' from $outer:x that went out of scope a while ago. If there was an explicit declaration that your were closing (is that the right verb?), it might be clearer to the user what what going on. { my $x = "bar"; sub foo { # my outer $x; # <- uncommenting this line changes the outcome return sub {my outer $x}; } } print foo()->(); (I've changed 'closure' to 'outer' since John Porter's just pointed out to me privately that 'closure' is grammatical nonsense) Or to put it all another way Any bare use of a lexical in an inner sub, such as "$x" is roughly equivalent to my $x = copy_of_the_value_at_compile_or_clone_time_of_the_outer($x); so a bare '$x' has the combined implicit actions of creating a new $x for the current scope (or even till the end of the current sub), and initialising to a partiular value at a particular time. That's quite a lot of semantic baggage attached to an innocent bare var! So instead, I'd like "$x" to be an error, and "my outer $x" to be shorthand for my $x = copy_of_the_value_at_compile_or_clone_time_of_the_outer($x); But I can see I'm not on to a winner here
Re: explicitly declare closures???
Dave Mitchell wrote: > foo() is a closure created at compile time. By the time the main {} block > has been executed (but before foo() is called), the $outer:x is undef, > and $foo:x is 'bar' (standard closure stuff). When foo() is executed, > the anon sub is cloned, and at that time, $anon:x is set from from foo's pad, > so it gets 'bar'. That explains it. I still don't like it. ;-) > (I've changed 'closure' to 'outer' since John Porter's just pointed > out to me privately that 'closure' is grammatical nonsense) > So instead, I'd like > > "my outer $x" to be shorthand for I guess you missed where I suggested that putting "my" on that declaration is also counter-sensical, not to mention redundant. "my" implies a brand-spanking-new lexical variable attached to this very scope. The semantics of "outer" (or "closed"...) can be defined to imply a lexical variable. -- John Porter
Re: Will subroutine signatures apply to methods in Perl6
Garrett Goebel wrote: > > Any word from on high whether subroutine signatures will apply to methods in > Perl6? There's RFC128 and RFC97... but they both mostly dodge the issue of > methods. > > The absense of method signatures for specifying required, optional, and > named parameters... not to mention type-checking for validation and dispatch > are why we've ended up with an assortment of parameter handling modules with > conflicting styles. It was my intention that it was implicit in rfc97 that method call syntax would be rewritten before the application of the polymorphism rules. I did not include a method-call syntax example in the summary, since I thought it orthogonal to the issue of polymorphism by argument types. Perl5 method syntax and "indirect object" syntax both are alternate ways of specifying the absolutely named method based on the type of the object, which appears as the first argument. Rfc97 would come into play based on the types of the second and later arguments, as well as possibly backporting the existing dispatch mechanism to work with as well as without parentheses. -- David Nicol 816.235.1187 "... raised indoors and tested by certified technicians"