Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Ensuring forward compatibility

by Abigail-II (Bishop)
on Apr 14, 2004 at 10:54 UTC ( [id://344980]=note: print w/replies, xml ) Need Help??


in reply to Ensuring forward compatibility

But what about scripts and programs? They don't generally say "package" anywhere. But, if you don't specify a package, that just means use the default package, which is called "main". The solution therefore is to insert, at the top of all your scripts and programs, this line:
package main;
Go and do it today. Tell your friends. Get yourself ready for the shiny new perl6 world.

This problem has already been addressed by Larry in Apocalyse 1, three years ago. I quote:

A closely related question is how Perl is going to recognize when it has accidentally been fed Perl 5 code rather than Perl 6 code. It would be rather bad to suddenly give working code a brand new set of semantics. The answer, I believe, is that it has to be impossible by definition to accidentally feed Perl 5 code to Perl 6. That is, Perl 6 must assume it is being fed Perl 5 code until it knows otherwise. And that implies that we must have some declaration that unambiguously declares the code to be Perl 6.

Now, there are right ways to do this, and wrong ways. I was peeved by the approach taken by DEC when they upgraded BASIC/PLUS to handle long variable names. Their solution was to require every program using long variable names to use the command EXTEND at the top. So henceforth and forevermore, every BASIC/PLUS program had EXTEND at the top of it. I don't know whether to call it Bad or Ugly, but it certainly wasn't Good.

A better approach is to modify something that would have to be there anyway. If you go out to CPAN and look at every single module out there, what do you see at the top? Answer: a ``package'' declaration. So we break that.

I hereby declare that a package declaration at the front of a file unambiguously indicates you are parsing Perl 5 code. If you want to write a Perl 6 module or class, it'll start with the keyword module or class. I don't know yet what the exact syntax of a module or a class declaration will be, but one thing I do know is that it'll set the current global namespace much like a package declaration does.

Now with one fell swoop, much of the problem of programming in the large can be dealt with simply by making modules and classes default to strict, with warnings. But note that the default in the main program (and in one liners) is Perl 5, which is non-strict by definition. We still have to figure out how Perl 6 main programs should distinguish themselves from Perl 5 (with a ``use 6.0'' maybe?), and whether Perl 6 main programs should default to strict or not (I think not), but you can already see that a course instructor could threaten to flunk anyone who doesn't put ``module Main'' at the front each program, and never actually tell their pupils that they want that because it turns on strictures and warnings.

Abigail

Replies are listed 'Best First'.
Re: Re: Ensuring forward compatibility
by kal (Hermit) on Apr 14, 2004 at 11:43 UTC

    I would be dismayed if the solution reached meant that Perl 5 barfed with an odd error - if any Perl 6 program needs to be Perl 5 before Perl 6 is 'switched on', then I would think that the switch should be something that causes an obvious error in Perl 5 when run, so that the user knows that it is Perl 6 code. For example:

    $ perl use 6.0; Perl v6.0.0 required (did you mean v6.000?)--this is only v5.8.1, stopped at - line 1.

    To me, that looks fine. To have some kind of 'module Main;' requirement (as per Larry's last sentance) you get:

    $ perl module Main; Can't locate object method "module" via package "Main" (perhaps you forgot to load "Main"?) at - line 1.

    That just looks like something has broken. But, although 'use 6.0;' looks okay, it kinda fails the EXTEND test he sets out - it's not naturally there. In fact, I don't think there is anything naturally there in a Perl 5 program that you can use to do this. But, maybe Larry only raised that point in relation to modules.

Re: Re: Ensuring forward compatibility
by DrHyde (Prior) on Apr 14, 2004 at 12:21 UTC
    Most people haven't read the apocalypses, or have read them but forgot the details. This was my humble attempt to bring one of the more important issues for perl5 programmers to a wider audience.
      Maybe I should have been more explicit. My point wasn't that you should have read the apocalypse. My point was that you shouldn't spread FUD, suggesting that people should modify their perl5 programs. Putting a package main; on top of your programs is not necessary.

      Abigail

        Thus spake Larry:

        I hereby declare that a package declaration at the front of a file unambiguously indicates you are parsing Perl 5 code.

        So muttering "package main" is *guaranteed* to produce the desired effect.

        Also spake Larry:

        If you want to write a Perl 6 module or class, it'll start with the keyword module or class. I don't know yet what the exact syntax of a module or a class declaration will be (my emphasis)

        So while there will be some way of saying "this is perl6" we don't actually know what it is yet.

        He goes on to say:

        But note that the default in the main program (and in one liners) is Perl 5

        Which it first glance would back up what you say. Except that I have been known to require a program into the middle of another or to read a file and eval it. Perhaps I'm being overly cautious, but to cope with weird edge-cases like that, and no doubt others I haven't thought of, adding an extra line to my program to say "this is old style code" does not seem overly burdensome. Think of it as just a bit more defensive programming.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://344980]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-25 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found