develooper Front page | perl.perl5.porters | Postings from March 2003

Re: (?{ .. })'s and lexicals: A fix ([perl #9010], [perl #8817], [perl #8030], etc)

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
March 16, 2003 12:04
Subject:
Re: (?{ .. })'s and lexicals: A fix ([perl #9010], [perl #8817], [perl #8030], etc)
Message ID:
20030316200440.D9272@fdgroup.com
On Sun, Mar 16, 2003 at 09:37:51PM +0200, Enache Adrian wrote:
> On Sun, Mar 16, 2003 at 12:58:19PM +0000, Dave Mitchell wrote:
> > Perhaps bare regexs should be demoted to blocks and qr// should be
> > promoted to full anon sub status, so that they get cloned on each execution
> > of qr// ???
> > 
> > Although I have to say that personally I don't like the idea of sharing
> > the same pad, simply becuase of run-time compilation, eg
> > 
> >     for (1..1_000_000) {
> > 	$p = '(?{my $x; ...})'; /$p/;
> >     }
> > 
> > This causes the pad to get filled with a million slots for defunct $x's.
> > Eval's have their own pad to avoid this sort of thing.
> 
> I see three cases there - with their moral equivalent.
> 
> 1. sub a { my $x; /(?{ $x++ })/ }
> 	=> 	sub a { my $x; { $x++ } }
> 
> 2. sub a { my $x; my $p = qr/(?{ $x++ })/; $_ =~ $p }
> 	=>	sub a { my $x; sub clos { $x++ }; clos }
> 
> 3. sub a { my $x; my $p = '(?{ $x++ })'; /$p/ }
> 	=>	sub a { my $x; eval '$x++' }
> 
> Does it look correct to you ?

yes, except for (2), which I would put as an anon sub so you get a new
closure for each execution:

2. sub a { my $x; my $p = qr/(?{ $x++ })/; $_ =~ $p }
	=>	sub a { my $x; my $p = sub { $x++ }; $p->() }

Whether it's practical to implement (1)..(3), and let alone what it would
do to backwards compatibility, I don't really have a feel for right now.

I'm also not certain whether it's fair on users to have (1) and (2)
implemented differently - that could cause suprises. eg does

    my $x; $p = 'hello'; /$p(?{$x})/

get treated as case (1) or (2)?

(Disclaimer: I do bug fixes, not language design. Hence my general
vagueness here.)

-- 
"You're so sadly neglected, and often ignored.
A poor second to Belgium, When going abroad."
Monty Python - "Finland"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About