I'm only aware of Pascal having lexically scoped/nestable subroutines, likely Modula has inherited them. Many other languages only have one, global, namespace for subroutines - certainly, Python, Ruby, PHP, JavaScript, Visual Basic, C and C++ have it that way.

If you group them into scripted(interpreted) languages vs. compiled, it seems scripting languages mostly don't, but above 2nd generation languages like Basic & Fortran, compiled languages are more likely to have it as a possibility.

As for C++, it has been a while since I've programmed in it, but can't functions be marked as private or public? If they are private are they still in the global name space?

But I believe PL/I, and Intel's system programming language prior to "C", PL/M had the feature. Dunno what other languages...so many have risen and died.

As for the "idiom" being frequent...that doesn't mean it is easily readable. when I see "$var = ..." I think of it as an assignment (it is), not a function declaration. The function declaration ... I tried several variations, just isn't as readable as seeing the form:

sub <description function name> [(arg-proto)] { body....indented...between a sub and a closing }.

The best I've come up with so far for an attempt to make the subroutine name appear has been a 2-3 line-high form like:

my $ print_Need = sub ($) { Inf($_[0] ." needed by " . ($insted ? "(installed) " : "") . $rpm_nvra . "\n") };

But I feel like I might risk harm by such contorted structuring to make a local subroutine stand out with its message.

Even with such contortions, I'm still not excessively happy with the verbose call syntax. Why does the language need "->" for correct meaning, when it would seem more clear (at least in this case) without it. I.e.:

$print_Need("file foobar needs package xyzzy") #seems clearly to be a procedure call #(does it have another syntactic meaning I'm forgetting at the moment? +) #vs. $print_Need->("..."); #seems less clear
I see the "->" and think "ah, we are doing an indirect function call to whatever is in $print_Need. I wonder what's in there. Is it an object?" vs. my intended interpretation of "oh, that's printing the Need passed in the arguments". I can't just read it and know what is going on.

First, I have to go look and see what's assigned to "$print_need". Then I find the assignment "$print_need= sub {...}" and now know that the variable "$print_need" is holding a reference to a local, anonymous function. What does the function do? It's not obvious, as the function isn't named but is "anonymous".

Just because a variable named "print_need" holds a reference to an anonymous function, I still don't immediately relate the name as being a "verb", descriptive of what the "sub" does, which would be more likely true if I saw: "sub print_need(...)" - *LED lightbulb turns on* - ahh. its a sub that is printing the "need" passed in parens.... At least that'd be my first impression...

It could be querying or setting the "print_need" attribute on (args), but I'd lean toward the direct-order interpretation first.

Obviously nothing prevents me doing these things in one of a hundred different ways in perl, but it's an intuitive, self-describing style that I'm searching for -- I really don't like comments unless there is much too much to explain...and if there is, I question whether or not the sub"routine" is "clear" enough....why isn't it clear "on its own"... It may be that the comment is necessary, but my first "shortcut" in writing code I want to be able to "re-use" is to make the code "legible" or "readable" (with trade-offs going to type-ability (a paragraph-long sub might clutter things up a bit, as well as make me not want to call the sub, as its name is too long :-) ). -Linda


In reply to Re^6: Perl scoping not logical...? by perl-diddler
in thread Perl scoping not logical...? by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.