[Proposal] Q: RE lexical scopes and sub declarations

M

Michele Dondi

This is Yet Another Humble Proposal... I am aware of the risks of
exposing possibly naive ideas like this, but I'm asking anyway.

I wonder wether forward declaring a sub in a lexical scope could be
made to let the actual sub access variables lexically scoped to it. In
other words if

{
my $u;
sub u;
}

# ...

sub u {
$u;
}

could be made to be implicitly equivalent to

{
my $u;
sub u {
$u;
}
}

Of course this a priori depends on p5p and on technical details, but
what I'd like to know is wether you would judge such a semantic
disruptive of anything or potentially useful from the UI point of
view. I've been working on a program recently in which I would have
liked to put all the subs at the end, but with forward declarations at
the top, one of which like above. IMHO it would have marked clearly
the structure of the program.


Michele
 
M

Michele Dondi

This is Yet Another Humble Proposal... I am aware of the risks of
exposing possibly naive ideas like this, but I'm asking anyway.

"Utter nonsense", then?


Michele
 
A

Anno Siegel

Michele Dondi said:
This is Yet Another Humble Proposal... I am aware of the risks of
exposing possibly naive ideas like this, but I'm asking anyway.

Well... not to say these questions couldn't be interesting, but
they hit the average clpm regular in the wrong mode. For one thing,
we are tuned to thinking in Perl, not about Perl, which is an entirely
different business. Also, a reasoned answer to such a proposal takes
more time than the average question. My first reaction was the standard
"Would take too long to answer, so next". That is not rejection of
the idea, but refusal to look at it (now). On Usenet, you got to
learn to read the silence. If it were immediately recognizable as
nonsense (as you implied in your followup), you'd have heard about it.

However, after doing my homework, I don't think it's viable. See below.
I wonder wether forward declaring a sub in a lexical scope could be
made to let the actual sub access variables lexically scoped to it. In
other words if

{
my $u;
sub u;
}

# ...

sub u {
$u;
}

could be made to be implicitly equivalent to

{
my $u;
sub u {
$u;
}
}

You mean you want to declare a lexical variable in one block, and use
it in another? Oh boy, is that wrong! It breaks the concept of lexical
scope, in a long-distance way. I don't think you'll find fans on p5p
for this.

Anno
 
T

Tad McClellan

Michele Dondi said:
"Utter nonsense", then?


I thought so because you can use a vanilla forward declaration
along with the more proper closure that you had presented.

sub foo;

...

{ my $foovar;
sub foo { ... }
}
 
M

Michele Dondi

You mean you want to declare a lexical variable in one block, and use
it in another? Oh boy, is that wrong! It breaks the concept of lexical
Yep!

scope, in a long-distance way. I don't think you'll find fans on p5p
for this.

I understand that... however I would consider that a sort of lexical
scope exportation rather than a breakdown: actually it would be just
the same, but with a better name! Seriously I think that as far as the
declaration part is concerned, the particular syntax would stress well
enough what's going on. OTOH I realize that as for the definition part
there may be in fact variables whose source could be hard to track
down thus degradating code intellegibility and maintainability. Maybe
a slightly more acceptable option would be to require a particular
pragma to activate the feature, a la

sub {
use declarationscope;
print $var1;
# ...
}


Michele
 
M

Michele Dondi

I thought so because you can use a vanilla forward declaration
along with the more proper closure that you had presented.

sub foo;

...

{ my $foovar;
sub foo { ... }
}

I don't think it is *exactly* so. Granted: I don't think this would be
a feature one can't leave without. In fact I've been programming in
Perl for a relatively long time now and I've never ever felt the need
for it up until the other day...

To be fair the more I think of it, I realize that the many different
ways to do it in current code are not just as bad as they seemed to me
in that moment. However, for some reason that due to time restriction
I can't explain here in detail, it would have been sensible for
intelligibility matters to "put" that var near the top of the source,
then go on with the main program itself which is only a limited number
of lines, and following it with a moderately long series of subs. As I
said I'm not so much convinced about this any more. But if one really
wanted...


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top