in reply to Re: Doing "it" only once
in thread Doing "it" only once

Indeed there are not unexpectedly many WTDI. As the author of the original post in p6l, I must say that Limbic~Region is one of the few that seem to have authentically understood the real point of the issue and in his much better phrasing than mine it all boils down to the hypothetical possibility of modifying the optree at runtime.

Now, BrowserUK informed us here that this kind of code self-modification was very common in early languages, but also regarded as a horror, which is one of the reasons that led to structured programming and later to OO programming, etc. OTOH it has become common again, in FP languages, as a compiler optimization.

But then it's hard to think that in such a dynamic and multi-paradigmatic language like Perl, be it 5 or 6, something exactly along these lines could be done. So my question, in the context of Perl6, regarded the possibility to do so by means of some syntactic sugar visually distinctive enough to avoid an unintentional use of it and to make clear what that the one using it wants and at the same time cheap enough, type-wise, to make it preferable over any of the already proposed alternatives.

Now, wrt you code above:

while ( <FH> ) { last if $_ eq 'foo'; # Where foo can only appear once in the file print; } while ( <FH> ) { print; }
it obviously looks nice enough and it makes clear why you're doing so. But it's still nearly double the code that one conceptually could want. Not only: if print were really a longer portion of code, you may {want,need} to refactor it into a sub, with the added overhead of a sub call (and here we're being -for once- really paranoid 'bout efficiency and micro-optimizations), which would not have been necessary at all if we had that kind of syntactic sugar...

Replies are listed 'Best First'.
Re^3: Doing "it" only once
by Anonymous Monk on Sep 22, 2005 at 10:57 UTC
    Not only: if print were really a longer portion of code, you may {want,need} to refactor it into a sub, with the added overhead of a sub call (and here we're being -for once- really paranoid 'bout efficiency and micro-optimizations), which would not have been necessary at all if we had that kind of syntactic sugar...
    Well, that would ask for syntactic sugar with an overhead of less than a subroutine call....

    Note that with clever use of string eval you can duplicate the code without having to maintain it twice, and not have the overhead of calling a subroutine from a loop. And you can move the eval as far out as you want (so the overhead of calling it becomes small enough).

      Of course my original question was asked in the context of Perl6, and there my reasoning goes one. Just as obviously eval is the answer to most self-modifying-code(-like) needs in Perl5, but we all rather prefer to avoid it if possible...