[Proposal] Q: RE lexical scopes and sub declarations

Discussion in 'Perl Misc' started by Michele Dondi, Dec 1, 2004.

  1. 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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 1, 2004
    #1
    1. Advertising

  2. On Wed, 01 Dec 2004 12:49:04 +0100, Michele Dondi
    <> wrote:

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 2, 2004
    #2
    1. Advertising

  3. Michele Dondi

    Anno Siegel Guest

    Michele Dondi <> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, Dec 2, 2004
    #3
  4. Michele Dondi <> wrote:
    > On Wed, 01 Dec 2004 12:49:04 +0100, Michele Dondi
    ><> wrote:
    >
    >>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?



    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 { ... }
    }


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 2, 2004
    #4
  5. On 2 Dec 2004 21:50:55 GMT, -berlin.de (Anno
    Siegel) wrote:

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 3, 2004
    #5
  6. On Thu, 2 Dec 2004 16:48:00 -0600, Tad McClellan
    <> wrote:

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 3, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-2?B?SmFuIFJpbmdvuQ==?=

    Proposal for signed/unsigned modifier in class declarations

    =?iso-8859-2?B?SmFuIFJpbmdvuQ==?=, Feb 3, 2007, in forum: C++
    Replies:
    10
    Views:
    568
    =?iso-8859-2?B?SmFuIFJpbmdvuQ==?=
    Feb 6, 2007
  2. Ben
    Replies:
    2
    Views:
    897
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,039
    Lawrence D'Oliveiro
    May 20, 2011
  4. Michal
    Replies:
    1
    Views:
    83
    Robert Klemme
    Aug 27, 2004
  5. Replies:
    12
    Views:
    462
    Øyvind Røtvold
    Oct 7, 2012
Loading...

Share This Page