Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
Perl Programming

Exegesis 3 Released (Perl 6 Examples) 85

chromatic writes "On the heels of Larry's most recent revelation, the mad scientist of Perl (Damian Conway) has followed up with Exegesis 3. His article gives working Perl 6 code examples of Larry's design decisions." Lots of good stuff in here.
This discussion has been archived. No new comments can be posted.

Exegesis 3 Released (Perl 6 Examples)

Comments Filter:
  • by Sneakums ( 2534 ) on Saturday October 06, 2001 @08:41AM (#2395151)
    His article gives working Perl 6 code examples...

    How can these be working examples when Perl 6 does not even exist yet?

  • man (Score:2, Insightful)

    by Anonymous Coward
    I really used to like perl i thought it was great...

    But now that i have been using C++ alot and really learned the in's and out's of the language, perl code really looks kinda nasty.

    Yick. I never thought i'd become one of those perl is messy and crusty and ugly types, but looking at that code, blech.

    So now they're gonna start using non-ascii characters in the code? Oh that's a joy, like i really wanna have to look up how to make greek characters in text editors.

    They blew it, perl 5 was acceptable and pretty useable. Perl 6 just looks silly.

    If i have to resort to using a scripting language for something in the future i definatly will avoid perl, especially perl 6.

    Perl really is a hacked together mess.

    Cool hack, but not cool to actually use for something important.
    • Hopefully you won't be marked as a troll, because I think in a way you are correct. Larry is recreating the language to be even more palatable to perl gurus but I think more hostile to newbies.

      On the other hand, if you can obscure complex operations into short syntactic sequences, you can keep the programmer from shooting himself in the foot. Look at Java - its syntax is probably as plain and up-front as you can get, but the need to spell most things out explicitly means most programmers spell them out incorrectly. Therein lies the magic of some syntactic freakshows like regular expressions - imagine programmers defining their own NDFAs? I understand this is not a great example as other languages support perl-like regexes, but maybe you get my drift.

      I think the conlcusion is that perl will be like Haskell and Lisp in a way (not a functional language per se) in that programming in perl will simply require a different approach and strategy. You'll have to think in perl in order to properly program in perl. It simply won't be for translating C into an interpreted environment or sh into a portable syntax, it'll be its own paradigm.

    • I too used to fight tooth and nail against co-workers to prove that Perl really wasn't that bad. Having been forced to code in other languages at that job though, Perl is starting to look as bad as everyone was saying.

      pity.
    • So now they're gonna start using non-ascii characters in the code? Oh that's a joy, like i really wanna have to look up how to make greek characters in text editors.

      More and more, Perl seems to resemble APL.
      The emphasis on one-liners, TMTOWTDI, VHL monadic or dyadic operators, and reputation as a write-only language.
      Larry Wall seems to have the goal of evolving Perl into a natural language or something equally complex.
      Non-ASCII characters seem like a mistake to me as well, Unicode support is far from universal and it introduces unnecessary complications, especially for parsing Perl programs.
      At the very least, there should be some alternate form of ASCII representation, possibly similar to Morgan Stanley's A+ [aplusdev.org].
      Most modern APL style languages use ASCII characters for precisely this reason.
      If using a tool is too complex, people will look elsewhere.

    • Perl is a language that stood out because of the way it came to be. It differs from others by the careful balancing of ideosyncrecy and shameless emulation. The guiding principles were always practicality ( making simple things easy) and linguistic metaphors ( $_ means 'it', @ is to $ as plural to singular, : is like the adverbial 'ly' in english, etc.).

      The fact that Perl succeded so well is proof that these design principles were right, despite the legions of naysayers.

      Now Larry Wall leads Perl into the unknown by trying a bolder iteration of the same principles. He may well fail, either because the world of computing has changed or because these principles have reached their ceiling. But he is doing the right thing. Innovation doesn't come from trying to keep everyone happy.

      To all those naysayers, Perl 5 is there, stay with it and be happy.

  • The flattening star *@array seems to give basically the same behavior as an real array parameter dereference in perl5, gobbling every other parameter to the end. OTOH, making an array parameter require a actual array is an improvement, I guess, because at least there is now a way to pass scalar after array without having it be gobbled up by the array.

    For me tho, I guess I'll stick with array reference arguments, anyways. Probably still cheaper and faster. I wonder also if what Damian says is true:

    In other words, a @ parameter in Perl 6 is like a \@ context specifier in Perl 5. .

    I suspect that passing an actual array is still "by value". Seems like alot of work to avoid confusion that is not really very confusing.

    • There is no such thing as passing by value in Perl 5, and I doubt there will be in Perl 6. In Perl 5, @_ gets refs to the arguments. The common practice of saying:

      sub foo {
      my ( $this, $that ) = @_;
      ...
      }

      &nbsp
      isn't only to give better names than $_[0] and $_[1] to the arguments; it's also to get values instead of references.
    • I suspect that passing an actual array is still "by value".


      Nope, it's by constant reference (and appending an 'is rw' to the end of the variable name makes it non-constant reference).

  • horse? (Score:2, Funny)

    by davereed ( 525994 )
    perhaps o'reilly will change the perl animal to a horse now that it's not so ugly?
  • Some thoughts (Score:4, Insightful)

    by reynaert ( 264437 ) on Saturday October 06, 2001 @10:02AM (#2395276)

    Some of this new stuff seems to be seriously blurring the difference between language and library.

    Of course it's cool to write something like @costs, but why must be an operator? It seems to me it would work just as well as an ordinary procedure.

    I'm wondering if they are thinking about constructs to define new syntax at runtime. In Scheme for example most of the syntactic forms are defined in the library using define-syntax .

    • Re:Some thoughts (Score:5, Insightful)

      by scrytch ( 9198 ) <chuck@myrealbox.com> on Saturday October 06, 2001 @10:33AM (#2395336)
      Some of this new stuff seems to be seriously blurring the difference between language and library

      Bingo. Perl has very malleable syntax, which is perhaps not as flexible as, say, SML, but still has had a philosophy of letting you rewrite good chunks of perl in perl. Perl6 is just adding to the potential confusion, letting you write code as twisted and evil as you want to. Besides, if you tend to write mathematical apps commonly ... well, you might not write them in perl, but you probably do at least " on an easy key (I had to copy and paste it myself). " does not need to be an operator. It just can be.
      • Re:Some thoughts (Score:3, Insightful)

        by scrytch ( 9198 )
        Bah, piece of crap turned my sigma characters into something else entirely. My guess is unicode input box, non-unicode form processing... I think the mainframe world is chuckling at us now, they dealt with EBCDIC dialect conversion issues on a daily basis decades ago...
    • Re:Some thoughts (Score:2, Interesting)

      by archen ( 447353 )
      Yeah, I'm pretty leary of any operator that can't be made by a key on my keyboard (or at least with a shift+key).

      Well I'm also sort of leary about any Perl article that goes into talking about "Schwartzian Transformers". Say what?!

      I think Perl 6 is starting to make me paranoid.
      • Re:Some thoughts (Score:2, Interesting)

        by smcdow ( 114828 )

        Well I'm also sort of leary about any Perl article that goes into talking about "Schwartzian Transformers". Say what?!

        Aw, c'mon. Schwartzian Transforms have been around for as long as Perl 5 has been.

        my @output =
        map { $_->[0] }
        sort { $a->[1] cmp $b->[1] }
        map { [$_, expensive_func($_)] }
        @input;

        A sort between two maps. Doesn't get much simpler than that.

    • but why must be an operator?
      It can, therefore it must. That's the way Perl is designed. I seem to recall a leading Perl hacker looking at Python and saying something like, "OK, it's got some good features and it's well-designed -- but there's only one way to do most things!" To Perl people, lovely new language idioms are a positive thing in and of themselves.

      Which is why I finally had to back away from Perl. Perhaps there are programmers who can deal with this kind of complexity and non-linear logic. I'm not one of them.

      Well, I'm a special case -- I'm a lousy programmer who mostly makes his living documenting other people's code. But judging from the code I've read, a lot of other Perl programmers don't think that way either. They treat Perl as kind of a super-BASIC, and their code is full of hand-coded loops and other stuff the fancy Perl idioms are supposed to replace.

  • ...await what LW and the gang make of what i really care about -- built-in language support for OOP. i can live with spelling '->' as '.' and using an underscore for concatenation, but what i really want to know is -- will perl6 allow me to write 50K lines of OO perl that doesn't depend on my client coders being able to read my source to be sure they don't trample over my member objects? all i'm really dying for with perl is some clean feel-good OO syntax and real encapsulation:

    public class MyPerlClass
    {
    my public $member_obj1 = new CGI ();
    my private %hash = ( 'some' => 'values' );
    my private int foo = 1;

    public sub new ( const int $int )
    {
    this.foo += $int;
    return this;
    }

    public sub get_foo ()
    {
    return this.foo;
    }

    public sub set_foo ( const int $new_foo )
    {
    this.foo = $new_foo;
    }

    private sub _recalculate_stuff ()
    {
    # some important stuff here...
    }
    }


    perl-lover and then some, but give me syntax sugary OO dear larry!!!
    d_i_r_t_y

    • OO perl that doesn't depend on my client coders being able to read my source to be sure they don't trample over my member objects?

      Use closures. Run perldoc perltoot , and then read the section Closures as Objects


      • Use closures. Run perldoc perltoot , and then read the section Closures as Objects.

        i have and i do, though closures are somewhat slower and larger than blessed hashes. it would be nice to be able to effectively local'ise the accessibility/visibility of member objects to inside a class using a perl keyword, rather than changing an class' implementation, ie, using keywords like 'private', 'public', and maybe even 'protected'. likewise, it'd be nice to overload the 'my' and 'our' operators to mean the equivalent of my == object declaration, our == class (ie: 'static') declaration. i have to say i really like the look of:

        class Me {

        # class data
        our public $Class_Total;
        our private $Class_Increment;

        # object data
        my public $total; # this.{'total'}
        my private $increment; # this.{'increment'}
        }

        dirty

    • I believe there is extensive remodelling called for by the current Perl6 gameplan, although I think this will further distance Perl 6 from Perl 5 programmers, and most likely some of them will simply move on to other languages which don't seem like such moving targets (although with Python claiming a signficant rewrite in the future, there may not be much of a safe harbor outside of yuck Java).
    • const int $new_foo
      Perl better not introduce variable types and casting, or I'll be ticked.
  • wow, perl meets apl (Score:4, Interesting)

    by scrytch ( 9198 ) <chuck@myrealbox.com> on Saturday October 06, 2001 @10:17AM (#2395305)
    I do love the hell out of perl, but I sure hope I can "use English 'operators';" for things like the hyper-operators. 'course, maybe 'use English' is deprecated ... can't wait for 'use Chinese', imagine what perl would look like then...

    I would much rather see "@diffs = @set1 ^- @set2" expressed as a list comprehension, say @diffs = (each $x - $y suchthat $x in @set1, $y in @set2); (assuming I have said "english operators" on, otherwise I would imagine at least the 'suchthat' operator would be some punctuation char). It's a bit contrived for something as simple as subtracting every member of a couple lists, but list comprehension is beautiful stuff when combined with lazy evaluation in languages like Haskell.

    Oh, and Perl6 is going to have an 'in' operator, right?
    • I'd like to see something like this:

      @diffs = map { $1 - $2 } @list1, @list2
      That makes sense to me at first glance..or maybe better something like this:
      @diffs = map { $_[0] - $_[1] } @list1, @list2
      which would be like taking one element from each list and passing them to the "subroutine" in the block. Anything would make more sense than ^- !
      • Huh? Was this an attempt at humor? I thought the '^' character for "vector" operations was a wonderful idea for perl... Even FORTRAN has had this sort of capability since FORTRAN-90 came out... If you don't want to use the '^' then fine, use some complicated map or looping construct. But I'd pick the '^' any day over those other suggestions!
      • that sounds similar to vector operations. I don't know where Larry is going with his new syntax, but what really seems like it could extend the language are vector operations. Anyone familiar with Matlab (or similar) knows the power of vector operations.

        @a = @b / @c;

        now there you've got potential to extend the language.
  • I got about half of that. No, less than half. I love Perl, but I'm not at all sophisticated in my understanding of it. I mainly use it for mangling text (I have a serious fixation on this, and I love Perl for the built-in text mangling) and for quick scripting. I start to frown when figuring out the difference between list and scalar contexts; when you start to talk about operator overloading, or references to slices of arrays of references to hashes, my eyes glaze over.

    I want to understand this, I really do, but I just don't. Sorry, Larry and Damien, I tried...

  • by dha ( 23395 ) on Saturday October 06, 2001 @02:08PM (#2395808)
    On the one hand, Exegesis 3 tells us that in Perl 6,
    100 < -s $filepath <= 1e6
    is essentially equivalent to:
    (100 < -s $filepath) && (-s $filepath <= 1e6)
    except that the `-s $filepath' only gets evaluated once. We're supposed to be Wow! Neat! about that.

    On the other hand, we are also told:

    Did you notice that cunning $a == $b != NaN test in operator:EQ? This lovely Perl 6 idiom solves the problem of numerical comparisons between non-numeric strings.
    which may be cunning, except of course with wow neat multiway comparisons it is equivalent to ($a == $b) && ($b != NaN), which doesn't do the advertised job.

    Finally, lest we might suspect operators like < and <= are treated differently from == and != for purposes of this `feature', we are told:

    binary <, >, lt, gt, ==, !=, etc. become chainable

    Predict the ultimate (though probably not imminent) demise of multiway comparisons in Perl 6.

    • Actually, that does do the job. If $a == $b, then they either both represent the same number, or they are both NaN, and if they are both NaN, then $b is NaN.
      • Ah, you're right. I was somehow lala imagining it was the application of `==' to one or two strings that would yield NaN, but actually `==' just propagates numeric context, so strings get numerified first, and non-numeric strings numerify as NaN in Perl 6.

        Hadn't noticed that `if ($a < $b < $c) { .. }' is actually illegal in Perl 5. Would've expected it to follow C, where you can be sick with like `int cp = (c!=0); if (a < b <= cp) { .. }' to say `a less than b implies (in the sense of modus ponens) that c is not zero'.

        But, Perl didn't do that, so you can't say twisted stuff like that, so there's no loss to adding multiway comparisons to Perl 6. Wow, Perl is actually cleaner than something!

    • On the other hand, we are also told:
      Did you notice that cunning $a == $b != NaN test in operator:EQ? This lovely Perl 6 idiom solves the problem of numerical comparisons between non-numeric strings.
      which may be cunning, except of course with wow neat multiway comparisons it is equivalent to ($a == $b) && ($b != NaN), which doesn't do the advertised job.

      Actually, it does, as Damian pointed out to me [develooper.com] when I asked about it [develooper.com] earlier today.

  • I started using Perl4 years ago. It was a very useful tool when there wasn't much else available on UNIX. It also seemed like its design was trying to poke some fun at the oddball collection of tools like awk, shell, and sed, that UNIX users were using. Perl4 was already pretty baroque. Perl4 was not really a full-featured modern programming language but a scripting tool.

    Perl5 tried to fix a lot of Perl4's limitations and turn it into a modern programming language, but the end result is rather iffy. It's a language with a bizarre object system grafted on top of the module system and lots of syntactic and semantic pitfalls for the unwary. Still, Perl5 has great libraries and a fast implementation, and I knew most of the weird features from Perl4. So I still use it, particularly when it has a library that does something I need.

    With Perl6, the fun seems completely gone, and the endeavor just seems bizarre. Does a language really need a -//=- operator (I'm not making this up) for default-assignment-that-only-interprets-undef-as-u ndefined, as opposed to the existing ||= operator, default-assignment-that-treats-undef-0-and-the-emp ty-string-as-undefined?

    My enthusiasm for Perl5 was already dampened, although I have probably written thousands of Perl5 scripts and CGI scripts over the years. But I think with Perl6, I'm going to jump ship. Perl isn't the only game in town anymore, and several other scripting languages now also have good libraries and are widely used.

  • I hated Perl4 because it was inconsistent and didn't make sense. Perl5 was MUCH better because there was a certain consistency and a lot of clever things were put in (example, using {} to create anonymous hashes is clever because {} reminds you of hashes, the => is a stringifying comma substitute which makes hash defs look good, but can also be used for other things, etc).

    Now Perl6 almost goes too far! Some of that stuff is handy but excessive. Let's see..

    // .. okay that's cool. I've wished for something like that many times. I hope it finds its way into Perl5. I also hope it comes in a unary version as a shortcut for defined() so I can write fun stuff like:

    while(//<>) { .. }
    or
    print "$_ is defined" if //

    ^ .. uhh.. handy, but ugly and makes me think of pascal. There must be a better syntax. What's wrong with some kind of multi-argument "map" operator? And is this:

    @a ^=~ s/foo/bar/;
    somehow better than this:
    s/foo/bar/ foreach @a
    ??

    _ underscore .. okay now this is too much. Just because dot is "industry standard" for dereferencing doesn't mean perl has to have it, the "->" syntax is clear to me. And now I'd have to put spaces around _? Yuck. Of course with the cool new scalar interpolator $(..) for strings I probably wouldn't use it as much, but still, that's too major a change I think.

    The unary underscore to convert to strings is silly. The article even says that _@thingy is the same as "@thingy". Well, duh, then let's use the second one, anybody off the street could tell it had something to do with strings, but what does the first one do? In my mind, underscore means "placeholder", not "string". Get rid of this syntax please, put it back the way it was!

    To add insult to injury, the article says that dot is optional/uneeded in many places except function calls. So they'd take it away and then make it mostly optional?? Why??

    : for marking adverbs and parameter lists is useful, though it means we have to put up with the new $x ?? $y :: $z operator. And that means we need a space around the "::" which runs all my obfuscated whitespace-free scripts. :-)

    := to bind things is cool. The sooner Perl can get rid of typeglobs and that crap the better. I bet someone understands typeglobs completely, but I sure don't.

    That whole thing with the Sigma summation operator was just ugly voodoo, especially the bit that specifies the precedence. Bleh, what's the point??? He defines the Sigma with a certain precedence, but then doesn't even make use of it in the script example. If you need special precedence, use parantheses!

    What is a "higher-order function"? Apparantly it's an anonymous subroutine with placeholder variables...the syntax doesn't really make sense, why "$^"? That makes me think of the hyper-operators. Probably a "&" should be thrown in there someplace. How about beginning those variables with "$&"?

    I think relying TOO much on punctuation is a bad idea. For one thing it becomes harder and harder to scan a script quickly looking for things. And if you start overloading symbols with all kinds of context-dependent meanings, it becomes impossible to remember what it means. For instance, using "+" to convert strings to numbers sorta makes sense, because plus has something to do with numbers. But it has nothing to do with converting strings to numbers, so people will probably forget what it means. I'd much prefer a "num" bareword.

    Oh well, some cool things, and some things that make me wonder why bother? I think I'll stick with Perl 5 for a while (but I'll take the // operator in Perl 5).

    • I think filehandle properties will let you do the definedness trick, though while() {} trick in Perl 5 is currently magical that way.

      Changing the meaning of the dot operator is pretty big, though. It'll take a while to get used to method calls without -> and concatenation with _.

      Using a leading underscore as a stringification operator could be advantageous... especially when dealing with an expression that returns a list, like a hash or list slice, or a method call. There are lots of things that aren't easily interpolated into a string. Give that one a chance.

      (And, yes, I understand typeglobs. :)

      Higher order functions are functions that can be treated as values... passed to and from other functions. They're terribly handy.

      I also wouldn't worry about excessive punctuation. If someone else doesn't already have plans to provide English.pm for Perl 6, I'll do it. It'll probably end up being handled the same way Ruby does it. Internally, everything's an object, and you can access regexp captures through methods or regexp properties, but there are also optional magic punctuation variables available for Perl afficiandoes or shortcut gurus.

      The really big changes are the dot and the underscore. Most beginner and intermediate Perl hackers aren't going to notice many of the other changes.

  • This new language frightens me. I say that as a perl coder who plays with the old version for a living and makes it do some funky tricks already. Perl 6 is skating worryingly close to the edge at which line noise parses and runs.
  • Would $a ^+= @b do what I want?

    i.e. $a is the sum of all elements of @b?

In practice, failures in system development, like unemployment in Russia, happens a lot despite official propaganda to the contrary. -- Paul Licker

Working...