Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

J2SE 5.0 new features are just sugar from Perl (and other similar languages).

by gmpassos (Priest)
on Oct 11, 2004 at 23:13 UTC ( [id://398329]=perlmeditation: print w/replies, xml ) Need Help??

Recently I was reading the new features of J2SE 5.0, that claim to be the most important upgrade of J2SE and the main point is to make the development easier.

Well, ok, reading that they are interested to make the Java development easier I was interested to see all this new things:

  • Enhanced for Loop:
  • Basically is our foreach() for collections (array of objects):

    new Java way:

    ArrayList ar = new ArrayList(); for(String item : ar) { ... }
    old Java way:
    ArrayList ar = new ArrayList(); for(Iterator i = ar.iterator(); ar.hasNtext(); ) { String item = (String) i.next(); }
    Perl:
    my @ar ; foreach my $item (@ar) { ... }
  • Typesafe Enumerations:
  • Is just the old enum resource from C:

    new Java way:

    public enum int {MENU_FILE, MENU_EDIT, MENU_FORMAT, MENU_VIEW} ;
    old Java way:
    public int MENU_FILE = 0; public int MENU_EDIT = 1; public int MENU_FORMAT = 2; public int MENU_VIEW = 3;
    Perl:
    my ($MENU_FILE, $MENU_EDIT, $MENU_FORMAT, $MENU_VIEW) = (0..4) ;
    Well, Perl already have an easy way to declare a sequence of numbers, so, we don't need a enum word.

  • Static Imports
  • This is the most interesting part of the new features, since this goes directly in the oposit way of a pure OO language like Java, import from another class static declarations. With Perl we use this a lot, is just our Export module, were when we load a module we automatically can get static references in the main package to variables and subs in other packages.

    Java

    ------------------------------------------ package com.name; interface XYZ { public static final double Constant1 = someValue; public static final double Constant2 = anotherValue; } ------------------------------------------ import static com.name.XYZ.*; public class MyClass { ... double value = 2 * Constant1; ... }
    The problem that they haven't realised yet is that we can't import using groups or a specific static declaration like in Perl. With this new feature in Java we always have to import all the static declarations of the other class, what will create problems in the main class, since method overloading will exists. On Perl we can define groups to be exported or get a specific name:
    use XYZ qw(Constant1) ; use XYZ qw(:SOMEGROUP) ;

  • Final Notes
  • Well, for us this new features can be dum, but for a language that don't change it's syntax in the last 10 years is not so dum, specially for who always use Java.

    Maybe the biggest new features are the performance changes, and monitoring (since Thread monitors in Java doesn't work 100%, what make not viable to use them).

    Now as a Perl developer I realise that be able to change the language that we use and know that we have a language that care about an usual way to do things is very good. We don't need to wait Larry Wall to create new features or fix them. We are able to embed Perl in any application, etc... But the most important, we don't need to be misled by a release the claim that is giving to us incredible things, were actually they are just adding simple things that all the world saw years ago.

    Graciliano M. P.
    "Creativity is the expression of the liberty".

    Replies are listed 'Best First'.
    Re: J2SE 5.0 new features are just sugar from Perl (and other similar languages).
    by ihb (Deacon) on Oct 11, 2004 at 23:31 UTC

      About enumerations.

      old Java way:
      public int MENU_FILE = 0; public int MENU_EDIT = 1; public int MENU_FORMAT = 2; public int MENU_VIEW = 3;

      I'd write that as

      public int MENU_FILE = 0, MENU_EDIT = 1, MENU_FORMAT = 2, MENU_VIEW = 3;
      making it not that much worse than your Perl way, even though I agree that manually doing several assignments isn't satisfactory.

      Well, Perl already have an easy way to declare a sequence of numbers, so, we don't need a enum word.

      Your Perl way isn't satisfactory either though. Keeping track of how many variables/constants you have shouldn't be necessary. That's why I like the enum module. It has nice other features too, such as prefixes and bitmask enumerations.

      use enum qw/ :MENU_ FILE EDIT FORMAT VIEW /;

      ihb

      Read argumentation in its context!

        Thanks for your advices. And the module enum show to us how Perl is malleable.

        Graciliano M. P.
        "Creativity is the expression of the liberty".

    Re: J2SE 5.0 new features are just sugar from Perl (and other similar languages).
    by pg (Canon) on Oct 12, 2004 at 00:50 UTC

      I don't think you understand the typesafe enumeration.

      Your Perl example is exactly the same as your old Java example. This is not about some syntax short cut. The new Java way defines a new enum type, which only has four valid values: MENU_FILE, MENU_EDIT, MENU_FORMAT, MENU_VIEW. It does not define four variables as you sort of indicated. The new Java enumeration is more like Pascal enumeration, it is a type, and any variable has that type can only have one of the four enumerated values. That is why they call it "typesafe".

      In Pascal, it also supports the ordering of those four values. Not sure Java does that.

      A better example of data is:

      enum int (SUN, MON, TUE, WEN, THU, FRI, SAT);

      and later you would be able to evaluate something like SAT > TUE.

      Perl is a great language, but hey, Java is also great! Besides it is always a nice thing to see a language evolve, and advance, doesn't matter whether it is Perl or Java.

    New Perl Features are Just Sugar From Language X
    by hardburn (Abbot) on Oct 12, 2004 at 13:41 UTC
      • Foreach: came from csh
      • Regular expressions: borowed from sed/awk/etc., plus lots of academic research before that
      • Closures: LISP and other functional languages
      • map/grep: LISP again
      • Virtual Machines (like Parrot): Java had one first, and Scheme and other LISP varients before that
      • Hashes: significant academic research before Perl got them. I'm not sure if anyone integrated them (or other types of associative arrays) so closely into the langauge before Perl did.

      It would be hard to come up with a language feature that Perl invented itself. Maybe psedo-hashes, but that's not exactly something to be proud of. Creative and academic endeavors always build on previous efforts.

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

        At the moment I'm learning LISP, and it's interesting to see so much stuff that is new and confusing and (probably) coming in Perl 6 already in use in (Common) Lisp for decades - e.g. coroutines & continuations, a really useful macro system, etc...

        To contrast - Common Lisp was standardized in 1986, and the original LISP was implemented in the 1960's, while Perl 1 was released december 1987.

          According to The Implementation of LISP, the original LISP actually was implemented in 1959.

          As for coroutines and continuations, these are not built into Common Lisp. However if you write the right macros and adhere to strict standards, you can achieve them. Or if you postprocess Common Lisp, you can again create continuations. See On Lisp for details.

          What we don't often hear in the Perl community is that continuations are not necessarily a positive feature in a language. For an example of why not, what belongs in a useful stack backtrace if you are using continuations?

      • Hashes: significant academic research before Perl got them. I'm not sure if anyone integrated them (or other types of associative arrays) so closely into the langauge before Perl did.
      • awk, at least.
        Hashes: significant academic research before Perl got them. I'm not sure if anyone integrated them (or other types of associative arrays) so closely into the langauge before Perl did.

        REXX had associative arrays a long time ago.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
          What was the date/version hashes came to Perl ?

          Would the associative containers that come in C++ count ? They were formally made part of the C++ standard in 1994. And Stepanov (one of the HP researchers who developed the STL) had been publishing papers on generic programming since at least 1982 - only 4 years after Kernigan and Ritchie published "The C Programming Language".

          use brain;

        Virtual Machines (like Parrot): Java had one first, and Scheme and other LISP varients before that
        Erm... If Schene had one before Java, Java's not the first.

        Perl 5's a VM, and it's older than the JVM. Having said that, the UCSD P-machine's older still, as is the inform engine and any number of BASICs.

          Maybe you should have read to the end of the line you quoted, where he clearly states that Scheme and LISP had them before Java.

          I'd guess the first programming language that has something I'd call a virtual machine might be Forth.

        It would be hard to come up with a language feature that Perl invented itself.

        Perl's "regexes" are quantitatively more powerful than most regex dialects, rather than just syntactic sugar run amok. I suppose if you want to be really fussy about it, all languages build on work done by Church and Turing (and you can probably go back to Peano and even Euclid if you want), but that's starting to get a little bit ridiculous.

        Hmm... Church numerals in Perl? Sounds like a decent obfu gimmick.

        --
        Yours in pedantry,
        F o x t r o t U n i f o r m

        What about the object system (symbol tables, globs, method calls, autoquoting in braces or before =>)? The different variables associated to the same name: scalar, array, hash, code, io, format (sure lisp has scalars and functions in different namespaces, but surely not so many types)? Lexical and dynamic localization in the same language? Function call without parentheses? Function call syntax depending on prototypes? Arrays and strings resizable efficently? Such simplicity of string operations that you still can't find in any other languge? These are all inventions of perl.

        Also just show me a language with a reference-counting garbage-collector.

          I think most of the things you mention are embracing and extending existing ideas, not new ideas in themselves. Perl gets most of its power by combining good ideas from other langauges. Perl6 has been described as adding back the good ideas that Ruby added to Perl.

          "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

    Re: J2SE 5.0 new features are just sugar from Perl (and other similar languages).
    by SpanishInquisition (Pilgrim) on Oct 12, 2004 at 16:49 UTC
      Languages should borrow from one another. My problem with the Java religion is they often act like they invented these things (especially reflection!) when they have not, and you will oft walk into a conversation and hear "oooh, they implemented something like reflection in java". Umh, no. Java needs to credit sources rather than endorsing plagarism.

      Meanwhile, it's a dynamic language, and being so tightly bound and shackled, it's missing much of the point. Or it's succeeding in being a extra safe language. It's like a sandbox with no toys in it to play with. Extra safe. No sharp edges. Little room for innovation. Never mind the syntax and enforced rules (i.e. checked exceptions) are so painful you never want to write a single line in the language, thus making it safer...

        It's not just Java. The C# folks are the same in this. Ever heard someone from M$ to speak about say garbage collection? The make it sound like they invented it.

        The only MS language for .Net that is kinda interesting is JScript.Net, shame it doesn't get the support it'd deserve :-( Maybe MS is afraid programers would dump C# if they had the same level of support for JScript.Net in Visual Studio and docs.

        Jenda
        We'd like to help you learn to help yourself
        Look around you, all you see are sympathetic eyes
        Stroll around the grounds until you feel at home
           -- P. Simon in Mrs. Robinson

        "It's like a sandbox with no toys in it to play with."

        Good way to represent the miss of good resources to make the programmer life easier. Maybe only now they are thinking to add some resources, or they are just showing to us something that is coming, let's say, in Java 3.

        Graciliano M. P.
        "Creativity is the expression of the liberty".

    Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others having a coffee break in the Monastery: (2)
    As of 2024-04-20 13:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found