Follow Slashdot stories on Twitter


Forgot your password?
Perl Programming

Perl 6 Grammars and Regular Expressions 202

An anonymous reader writes "Perl 6 is finally coming within reach. This article gives you a tour of the grammars and regular expressions of the Perl 6 language, comparing them with the currently available Parse::RecDescent module for Perl 5. Find out what will be new with Perl 6 regular expressions and how to make use of the new, powerful incarnation of the Perl scripting language."
This discussion has been archived. No new comments can be posted.

Perl 6 Grammars and Regular Expressions

Comments Filter:
  • by Scott Lockwood ( 218839 ) * on Monday November 08, 2004 @01:22PM (#10755955) Homepage Journal
    How will this affect people running slash? In a slightly related question, when will Slash support Mod_Perl 2.x, and Apache 2?
  • Big problem (Score:2, Interesting)

    by Smallpond ( 221300 ) on Monday November 08, 2004 @01:30PM (#10756053) Homepage Journal
    Perl 6 will support Perl 5 regular expressions by using the :p5 modifier.

    Meaning that it is not backward compatible without modifying your source code.

    Note to those who are going to respond "Just install both!": look at the first line of your perl scripts.
  • by Sebastopol ( 189276 ) on Monday November 08, 2004 @01:43PM (#10756205) Homepage
    I'm surprised by the regex grammar. It looks a lot like how I use boost::spirit::rule for parsing regex in C++:


    # note this is just a language example, not an accurate name matcher
    grammar Names
    rule name :w { };
    rule singlename { + };

    C++::boost::spirit--- // rule for parsing a token string
    rule split = *(*space_p >>
    (+graph_p)[append(tok)] >>

    msg "Parsing input\n"; // 1. Parse declarations
    while (!header_ok && getline(input, line) && input.good())
    parse(line.c_str(), split);

    There are even grammar classes in Spirit.

    I sure hope perl6 is faster! ;-)

  • by Anonymous Coward on Monday November 08, 2004 @01:44PM (#10756224)
    I've got a fully multithreaded perl script running under Win32. It wasn\'t too bad to write but some parts sucked. One of the things sucked because Win32 doesn't support alarm() calls and you have to manually poll sockets and I hate that shit, or use vec() and that's just insane (how many people understand how vec() works anyway???) The other big thing that sucked was the crappy mechanism for sharing complex data structures between threads. All's honky-dory if you're just sharing a scalar variable, but don't try a HoH or anything like that cause you'll find out that there's no auto-vivification, and you have to manually create the data structure in every thread that needs to access it, and that right there leads to lots of possibilities for "interesting" bugs to show up (it's not very fun to code or pretty to look at or understand once it's done).
    But anyway none of that is really here nor there, but I just wanted to comment that Perl "scripting" isn't just CGI forms and stuff like that. You can really do complex applications in Perl. It's a full-featured language, portable (moreso that even Java, just look at the list of archs in the script), and able to handle most tasks that don't require a tiny memory footprint or direct CPU register manipulation.
  • by musterion ( 305824 ) on Monday November 08, 2004 @01:45PM (#10756233)
    Regular Expressions to Context Sensitive (at least) Parsing. I'm not a big Perl Geek, but I use it on a daily basis.
  • Adoption (Score:5, Interesting)

    by base_chakra ( 230686 ) * on Monday November 08, 2004 @01:47PM (#10756251)
    Years ago, Eric Raymond wrote []:
    "Perl XS is acknowledged to be a nasty mess. My guess is the Perl guys would drop it like a hot rock for our [Python's] stuff --
    that would be as clear a win for them as co-opting Perl-style regexps was for us." [emphasis added]
    Maybe I misinterpreted ESR's intended message, but it would be disappointing if hypercompetition prevented Perl's already-influential regex extensions from exerting a positive influence on other platforms. Raymond seems to imply that the Python team only grudgingly included support for Perl-style regex. I understand that developement teams in similar niches each want to make a big splash in the industry, hopefully Python's great increase in popularity has softened the survivalist attitude that seems to characterize this Raymond quote from Python-Dev. Evolving regex can benefit everyone.

    Note to those ready to mod me Troll/Flamebait: I'm not trying to pick on Python, I just happened to be acquainted with this candid quote.
  • by WWWWolf ( 2428 ) <> on Monday November 08, 2004 @01:55PM (#10756339) Homepage

    Yeah, Perl 5 hasn't changed that much over time. But it has been around for a while. Perl 6 is just different.

    From what I have seen from the announcements, the Perl 6 syntax looks far cleaner, probably more consistent and less ugly. Some of the new tricks look genuinely handy. For example, if it seems like type checking would be a good idea, you can have it if you want it, even on compile time!

    Especially the regular expressions side seems pretty interesting, as noted in this article. Regular expressions have always been a poor but effective replacement for grammar-based parsing, and now finally Perl is going to have both integrated. There's probably going to be less whining about line noise.

    And then there's something that I find especially interesting, though it hasn't been explained in detail yet: Complete tuning of the object system. In case you haven't noticed, Perl 5's object system is a complete and utter mess that looks and smells like it has been added as an afterthought, and rest assured it's going to be changed radically for better in Perl 6. I'm definitely waiting eagerly to see what Perl 6's take is going to look like - I sure hope it's something like Ruby, only it smells like a camel =)

  • by Mr. Muskrat ( 718203 ) on Monday November 08, 2004 @02:12PM (#10756503) Homepage
    Sounds to me like you prefer PHP and therefore spent more time perfecting your understanding of it. If you know and understand a language (any language) your work will require less time and will (surprise, surprise) be easier for you.

    (I'm a recovering language bigot so I can say this. :-P )

  • by Anonymous Coward on Monday November 08, 2004 @02:50PM (#10756970)
    I don't quite understand where you're coming from, because Perl is one of the few languages that has allowed me to code in the way I think. For most languages, I have to think like the computer does, ie. momentarily turn my brain into an i386 CPU (or whatever arch it is). But not so with Perl. It's been a real joy to be able to write in an almost Enlish-like syntax, eg:
    foreach $line (@data) {
    chomp $line;
    my ($x, $y, $z) = split(':', $line);
    update_coords($x, $y, $z);
  • by MrBoombasticfantasti ( 593721 ) on Monday November 08, 2004 @02:51PM (#10756984)
    I tried to absorb the syntax docs one afternoon, but it gave me nightmares. [...] Ever since I've been haunted by perverse unreadbility of it all.

    When I started to learn Perl (coming from a C background) I had quite a different experience. I really felt I had "come home", or something like that. Sure, you can write obscure code, but that's no different from C. But you don't have to, it can be very clear.

    I'll give credit to the fact that perl is compact, terse, to the point and has a reputation for string manipulation.

    I just love it for the short development times, and the fact that you can really use it for just about any environment. Want to do CGI? Sure! Just GUI? No problem! Connect to about every database there is, that's no biggie.

    And what about CPAN? That's a part of Perl too! You get all that ready-to-run code for just about any problem domain.

    IMO, "the right job" for perl is about 2% of all programming tasks out there.

    Maybe you are right, but somehow I get a lot of those 2% jobs... ;-)

    Now you have to excuse me, I have some perl coding to do! ;-)

  • by Anonymous Coward on Monday November 08, 2004 @02:55PM (#10757018)
    Perl is a language, so it follows that it is a communication medium. By that it should be able to communicate something to a party outside just the author and the perl interpreter.

    Not so. I think of Perl as akin to the language lovers the mindless observer, there is no the observant, one can never predict what will happen the couple, it's pure passion.

  • Re:Yes. (Score:3, Interesting)

    by sleepingsquirrel ( 587025 ) <Greg,Buchholz&sleepingsquirrel,org> on Monday November 08, 2004 @03:02PM (#10757082) Homepage Journal
    Don't forget that perl patterns support recursion. Perl can match (at least some) context-free grammars...
    #match context free grammar --[0{N}1{N}] (e.g. 01, 0011, 000111, etc.)
    $cf = qr/01|0(??{$cf})1/;

    print "matched $init\n" if ($init=~m/^$cf$/)
    ...and (some) context-sensitive grammars...
    #match context sensitive grammar --[0{N}1{N}2{N}] (012, 001122, 000111222,...)
    $t=qr/12|1( ??{$t})2/;
    $cs=qr/(?=$h 2+$)(?=0+$t$)/x;

    print "matched $init\n" if ($init=~m/^$cf$/)
  • Re:Perl goodness (Score:5, Interesting)

    by Anonymous Coward on Monday November 08, 2004 @03:22PM (#10757319)
    A web browser? That's:
    perl -MHTML::Strip -MIO::All -e 'print HTML::Strip->new->parse(io($ARGV[0])->scalar )'
    A web server? That's:
    perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'
  • by Anonymous Coward on Monday November 08, 2004 @03:46PM (#10757684)
    I get sick of the 'standard' backlash every time a Perl article is posted. Why do people have such a problem with Perl? It's an excellent, high-level general purpose programming language with a huge range of extension modules available []. I have personally used Perl for many projects, as do TicketMaster [], ValueClick [], Morgan Stanley [] and Ryanair [] and I've also learnt a lot about software engineering and computing through Perl.

    Yes, it does include a lot of symbols, but there is payback to learning them, and really most programs won't use much beyond $ % # () [] {}. Unlike some languages [], Perl is not what I would describe as a 'bondage' language. If you want to program sloppy, you can program sloppy. That's fine by Perl. And this generousity is what gives Perl its bad reputation. This is funny since I and most knowledgeable Perl programmers can write perfectly clear and maintainable code. The way we do this is no secret--it's just by commenting appropriately, using meaningful identifier names and following the Perl style guidelines [].

    People can mock Perl all they like, but it is still a widely used powerful programming language and I am more productive in it than any other language. As a parting comment, a Cisco employee once told me (off the record of course!) that "Cisco would fall apart without Perl".

  • by ajs ( 35943 ) <> on Monday November 08, 2004 @05:51PM (#10759671) Homepage Journal
    Perl 6 will probably not be faster than boost, but keep in mind that you also gain the power of a fully dynamic programming language in Perl 6's rules. Rules act as closures and can also contain Perl 6 code. Hypothetical variables are really going to blow people's minds (I know they took me a while to grasp, and when I did, I just sat around saying "wow" for a while :-)...)
  • by jonadab ( 583620 ) on Monday November 08, 2004 @06:05PM (#10759849) Homepage Journal
    > What does Perl6 offer a satisfied Perl5 user? Is it faster? Smaller?

    It features better support for key paradigms, including object-oriented
    programming (finally, a real object model), functional programming (we're
    getting continuations), and even some improvements for contextual programming.
    In other words, Perl6 will be a substitute not just for Perl5 but also for
    Scheme and Smalltalk.

    Also, the whole Parrot thingydoo is going to allow software written in one
    language to seamlessly use libraries written in another language, without all
    the ugly messing around you have to do to accomplish that in Perl5. You'll
    be able to construct a complete data structure in Perl code and pass it to
    a library written in Python, for example.

    Read the Apocalypse articles twice. The first time you'll recoil in utter
    horror. (I did.) Then read them again, and you'll be very excited. I am.
    The bummer is that we're still a while off from the release of 6.0
  • by mpmansell ( 118934 ) on Monday November 08, 2004 @08:13PM (#10761240)
    Personally I have a love/hate relationship with Perl.

    The purist Computer Scientist in me loathes it. It is ugly, dangerous and has a weird botched together syntax.

    Just as I self rightously convince myself of these self evident facts, some real cool trick saves the day and the wild inner hacker in me starts telling the CS part to stop being such a bore :)

    Perl is a great language, though like all powerful tools it can be dangerous if misused. It is sometimes ugly, and just as often beautiful.

    All that said, given the general trend within /. these days, I do suspect that most denigrators do so because they cannot handle all the squiggles :) God help them if they ever encountered APL :D
  • by Kristoffer Lunden ( 800757 ) on Monday November 08, 2004 @08:53PM (#10761541) Homepage
    The purist Computer Scientist in me loathes it.

    "Much as I hate to say it, the Computer Science view of language design has gotten too inbred in recent years. The Computer Scientists should pay more attention to the Linguists, who have a much better handle on how people prefer to communicate."

    --Larry Wall
  • Dangerous? (Score:3, Interesting)

    by TheLink ( 130905 ) on Tuesday November 09, 2004 @11:48PM (#10773488) Journal
    How's perl dangerous?

    In my experience (as a IT security guy) C and PHP are more dangerous than perl.

    C - "runs arbitrary code of the attacker's choice" given _common_ stupid programmer mistakes.

    PHP - developers fond of features that encourage bad/insecure ways of doing things - e.g. magic quotes, global track vars. Take away such popular PHPisms and PHP starts to look like perl ;).
  • Re:Adoption (Score:3, Interesting)

    by ajs ( 35943 ) <> on Wednesday November 10, 2004 @12:24PM (#10777496) Homepage Journal
    Exactly. Python and Perl are not really competitors in the strictest sense. They both build on each other. In many ways, I think Larry would have made some of the choices that Python did, had he started out in the 80s knowing what he knows now, and that's evidenced by how much of Perl 6 draws from Python (as well as Ruby, Scheme, LISP, Smalltalk, C++ and Java).

    Of course, the basic approaches to language design follow different philosophies (Perl's is one of inclusion, Python's is one of exclusion... both are valid). But I think in the final analysis both will be first-class citizens of the high-level language world for a long time to come, and the cooperation fostered by Parrot's runtime will help to ensure that.

    ESR's problem with Perl is (and I've spoken to him at length about this) that it is easy to write bad code. I don't think there are many Perl programmers who would disagree with that. ESR's solution is to use Python. Valid choice. My solution (and that of many Perl programmers) is to write clean code, maintain it through CPAN and make sure Perl 6 makes it easier to write cleaner code. Again, a valid choice.

"For a male and female to live continuously together is... biologically speaking, an extremely unnatural condition." -- Robert Briffault