Perl Pro but Java Newbie: Need nudge in proper direction for myfavorite Perl routine in Java - Java

This is a discussion on Perl Pro but Java Newbie: Need nudge in proper direction for myfavorite Perl routine in Java - Java ; /usr/ceo wrote: > Display class. But maybe that's the wrong way to go about it? I > can't believe I have to use a fifty letter package name class method > to print a line to the console. (This is ...

+ Reply to Thread
Page 2 of 4 FirstFirst 1 2 3 4 LastLast
Results 11 to 20 of 34

Perl Pro but Java Newbie: Need nudge in proper direction for myfavorite Perl routine in Java

  1. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction formy favorite Perl routine in Java

    /usr/ceo wrote:

    > Display class. But maybe that's the wrong way to go about it? I
    > can't believe I have to use a fifty letter package name class method
    > to print a line to the console. (This is one of the things I hate


    I think the right way to go about it would be to get an IDE that handles
    abbreviations and macros efficiently. I use NetBeans. It will
    substitute the string "sout" followed by a tab to
    "System.out.println("");" and put the cursor in the center of the inner
    quotes. So I type 5 characters, not 23.

    Programmers who come after you aren't going to be terribly excited about
    your attempts to custom re-wire the IO classes. They're fine the way
    they are.

    And "puts" as-is won't handle objects besides string literals, or
    primitives, without a lot of work which will duplicate what System.out
    does now anyway. So the path you are going down is pretty much lose-lose.


    BTW:

    System.out.println(
    "This is line 1\n" +
    "This is line 2\n" +
    "This is line 3\n" +
    "You get the picture..."
    );


  2. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction for myfavorite Perl routine in Java

    On Sep 14, 2:39 am, "M@hdeTo" <mohammady.ma...@gmail.com> wrote:
    > On Sep 14, 8:36 am, "/usr/ceo" <news...@cox.net> wrote:
    >
    >
    >
    > > On Sep 14, 1:34 am, "/usr/ceo" <news...@cox.net> wrote:

    >
    > > > On Sep 14, 12:50 am, "tomaszewski.p" <kssw...@gmail.com> wrote:

    >
    > > > > On 14 Wrz, 06:40, "/usr/ceo" <news...@cox.net> wrote:

    >
    > > > > > TestPuts.java
    > > > > > import static com.myorg.utils.Display.*;

    >
    > > > > > public class TestPuts {

    >
    > > > > >    public static void main( String[] args ) {
    > > > > >       puts( "A single string test." );
    > > > > >       // This is specifically where I seem to be having trouble
    > > > > > passing a
    > > > > >       // String[] array to the puts( String[] args ) call
    > > > > > implementation.
    > > > > >       // But maybe the implementation itself in the Displayclass
    > > > > > isn't
    > > > > >       // the way to go?
    > > > > >       puts( {
    > > > > >          "This is line 1",
    > > > > >          "This is line 2",
    > > > > >          "This is line 3"
    > > > > >       } );
    > > > > >    }

    >
    > > > > >    // Don't need these any more with static imports...
    > > > > >    // private static void puts( String[] args ) { Display.puts( args ); }
    > > > > >    // private static void puts( String arg ) { Display.puts( arg ); }

    >
    > > > > > }

    >
    > > > > > Everything seems fine as far has the implementation goes in the
    > > > > > Display class.  But maybe that's the wrong way to go about it?  I
    > > > > > can't believe I have to use a fifty letter package name class method
    > > > > > to print a line to the console.  (This is one of the things I hate
    > > > > > about Java, but anyway...)

    >
    > > > > > So...  Someone who knows Perl and Java both...  Maybe you canhelp me
    > > > > > out.  Or someone who knows Java can at least see what I am attempting
    > > > > > to do.  Again, this seems extremely bogus to even have to do.  Look
    > > > > > how elegant the Perl solution is...  But I should get off my soapbox I
    > > > > > guess and just ask for help.  (Can you tell I'm irritated that I have
    > > > > > to learn Java???!!! :-))

    >
    > > > > > /usr/ceo

    >
    > > > > Probably you are searching for 'static imports':http://java.sun.com/j2se/1.5.0/docs/...ic-import.html

    >
    > > > > Przemek

    >
    > > > Alright, boss... :-)  That takes care of the over-implementation of
    > > > puts() in the TestPuts class with the essentially puts() wrappers.
    > > > That still doesn't solve my problem of being able to call puts() with
    > > > a single line or with multiple lines...  How do I do that?  I want to
    > > > be able to do this:

    >
    > > > puts( "This is a line" );

    >
    > > > and this:

    >
    > > > // How do I even specify multiple lines??
    > > > // I'm using some kind of "anonymous array" notation
    > > > // below, that looks "made up."  Eclipse doesn't
    > > > // like the following:

    >
    > > > puts( {
    > > >    "This is line 1",
    > > >    "This is line 2",
    > > >    "This is line 3"

    >
    > > > } );

    >
    > > > So, that static imports thing can come in handy (and I understand the
    > > > anti-pattern behind it from Java's perspective of polluting names
    > > > spaces from the URL you provide -- nice.)  But the main thing I want,
    > > > I'm still not getting...

    >
    > > > We need to concentrate on the Display class now.  Is that right?
    > > > Doesn't seem like it...

    >
    > > > /usr/ceo

    >
    > > And I made the Display class "final" for you purists out there (of
    > > which I would agree, which is why I did it).  That's just a purist
    > > thing.  The implementation is still the open question.

    >
    > > Thanks all!
    > > /usr/ceo

    >
    > if you are using java >= 5
    >
    > you can do varargs like this:
    >
    > public static void puts(String... args){
    >    for(String arg:args)
    >        System.out.println(arg);
    >
    > }
    >
    > you can call this the same way you call the perl version and using
    > static imports will make your code look prettier:
    >
    > puts("this is just one line");
    > puts(
    >     "this is line one",
    >     "this is line two",
    >     "this is line three"
    > );
    >
    > you can even call it with an array:
    >
    > String[] someResults = {"res1","res2","res3"};
    > puts(someResults);


    Bingo. This is exactly what I'm looking for. Thanks and everyone
    else who responded in kind.

    usr/ceo

  3. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction for myfavorite Perl routine in Java

    On Sep 14, 5:31 am, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
    wrote:
    > You can do this in Java:
    >          puts( "A single string test." );
    >
    >         puts( myStringArray );
    >
    >          puts( new String[] {
    >              "This is line 1",
    >              "This is line 2",
    >              "This is line 3"
    >          });
    >
    >          puts(exec("cmd /c dir"));
    >
    > /usr/ceo wrote:
    > > Okay, unforeseen circumstances have necessitated that I learn Java.
    > > I'll refrain from providing my opinion of Java and extremely high
    > > overhead compared to Perl, but... whatever.

    >
    > You failed to refrain!


    I know... And yet everyone here responded nicely anyways. :-)

    >
    > > I tried this and
    > > it seems to be proper syntax (inside a class with a main()):

    >
    > It isn't the best way to achieve what you want.


    Yeah, hmmm, that's why I wrote. Certainly seemed that way.

    >
    > > Display.java:
    > > package com.myorg.utils;

    >
    > > pubic class Display {

    >
    > >    public static void puts( String[] args ) {
    > >       for (int i=0; i<args.length; i++)
    > >          __puts( arg[i] );
    > >    }

    >
    > >    public static void puts( String arg ) {
    > >       __puts( arg );
    > >    }

    >
    > >    private static void __puts( String arg )
    > > { System.out.println( arg ); }

    >
    > Ick. You save ~24 chars at the expense of ~70. Poor tradeoff.
    >


    I know. Now you know why I was steamed. I was like "This is
    RIDICULOUS!" (You've got to be doing this wrong, Homer...)

    >
    > >       puts( {
    > >          "This is line 1",
    > >          "This is line 2",
    > >          "This is line 3"
    > >       } );
    > >    }

    >
    > >    private static void puts( String[] args ) { Display.puts( args ); }
    > >    private static void puts( String arg ) { Display.puts( arg ); }

    >
    > Ick! In most languages, when I find myself writing ugly code, I'm
    > certain there is a better way to write it. Occasionally there isn't of
    > course.
    >


    "Holy Irregular Syntax, Batman! The Coder coded himself into a Java
    Tantrum!"
    "Precisely, Robin!"

    >
    > There's things I hate about Perl, that doesn't stop me loving Perl!
    > Generally I try not to rant about language X in a language Y forum.
    > It antagonises potential helpers.
    >


    And everyone here was nice anyway. :-) I think you are partially
    correct. My experience on Usenet has been that light ribbings of
    language X in language X's forum tends to yield light roastings. When
    you come in and ask a question and tell everyone they suck before they
    even answer your question... That's when the plonkings and kill files
    come out albeit *after* you've been tied to a stake and the match
    thrown as they leave the room.

    But anyway, it does pay to be careful.

    <waxhilosophical>

    And somewhere you mentioned "Perl tends to be more procedural" and I
    snipped it out as I was responding and don't have the time to but it
    back, but...

    I agree in that Perl allows you to be 100% procedural if you want --
    and so many people do. I myself have done it even though I try to
    practice the discipline of making my Perl OO. Aside from the fact
    that I find most people who code in Perl are merely "scripters," the
    angst a Java person might find in writing Perl is... "I have to do all
    THAT to create class!!" So unless you already have tools (read
    "modules") available for quickly and easily constructing classes, you
    generally won't if you have to create your entire class structure from
    scratch first.

    You have to be disciplined in Perl to avoid writing procedural code.
    I find in all the languages I know that you have to be disciplined to
    avoid that pitfall of each language as each language has theirs. I
    haven't learned enough of Java yet to know exactly what that is yet
    with Java, though I may have stumbled across one here.

    </waxhilosophical>

    All in all, if I am completely honest and not "pining" (:-)) I have to
    say so far, I'm pretty at home here in Java. It's not so unlike Perl
    in some ways. I think I'm already writing very structured, decently
    written classes here in a short amount of time, and that's good. I'm
    having to use it whether I like it or not. :-)

    Thanks for your very detailed response here with all you wrote. I do
    appreciate it. Any everyone else on the var args. That's exactly
    what I was looking for.

    /usr/ceo

  4. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction for myfavorite Perl routine in Java

    On Sep 14, 8:10 am, Joshua Cranmer <Pidgeo...@verizon.invalid> wrote:
    > /usr/ceo wrote:
    > Perl tends more towards a procedural programming paradigm, while Java is
    > more OOP. The act of printing to the console is theoretically rare in a
    > Java app, over the act of printing to an unspecified stream, e.g. it
    > could be a log or a console. So the console output stream is but one
    > stream among many, that would only be set at some configuration time.


    Oh, it was you who said this about Perl and not RedGrittyBrink.
    (Sorry, RGB!) See my philosophical section in reply to RBG on this.
    I agree with you for the most part but only because Perl allows it and
    people so then do it.

    Yeah, on the output stream, I think it definitely does show one
    running through exercises and using the console for output in
    "beginner mode." Somehow I already know what you are saying is true.
    Mainly because so little of what I write in Perl is also very seldom
    to the console (stdout). I can see where it's rare. I don't expect
    to be here long. I'm finding Java is starting to look a bit more
    familiar to me now.

    > --
    > Beware of bugs in the above code; I have only proved it correct, not
    > tried it. -- Donald E. Knuth


    Nice.

    /usr/ceo

  5. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction for myfavorite Perl routine in Java

    On Sep 14, 9:59 am, Mark Space <marksp...@sbcglobal.net> wrote:
    > /usr/ceo wrote:
    > > Display class.  But maybe that's the wrong way to go about it?  I
    > > can't believe I have to use a fifty letter package name class method
    > > to print a line to the console.  (This is one of the things I hate

    >
    > I think the right way to go about it would be to get an IDE that handles
    > abbreviations and macros efficiently.  I use NetBeans.  It will
    > substitute the string "sout" followed by a tab to
    > "System.out.println("");" and put the cursor in the center of the inner
    > quotes.  So I type 5 characters, not 23.


    I'm using Eclipse (Ganymede) on Mac OS X. I'll have to try that. In
    general, I'm also learning about the Eclipse-type editing platform,
    which also has it's own ecosystem. NB is based on Eclipse, if I'm not
    mistaken, is it not? I'll have to try the above. Thanks.

    >
    > Programmers who come after you aren't going to be terribly excited about
    > your attempts to custom re-wire the IO classes.  They're fine the way
    > they are.
    >
    > And "puts" as-is won't handle objects besides string literals, or
    > primitives, without a lot of work which will duplicate what System.out
    > does now anyway.  So the path you are going down is pretty much lose-lose.
    >


    I totally agree. I'm a Sr. Developer where I work and if someone were
    to do this to production code, I would frown upon it heavily in code
    review and it would have to be changed back to something less
    surprising and more conventional, as you indicate. This is mainly for
    my own use for now. I don't think it would be fair to dump such
    surprises on others. There was something about it -- maybe feeling
    like there had to be a lot of overloading, etc. to make it work like
    System.out.print() variants -- that at least gave me the feeling. But
    it's good for you to pass on just in case I might have happen to
    believe this was fit for general usage. I think if it were, it would
    have already been done. Still... for my own use, I wanted to have
    it.

    Thanks all! You all have been quite helpful!

    /use/ceo

  6. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction formy favorite Perl routine in Java

    /usr/ceo wrote:
    > I'm using Eclipse (Ganymede) on Mac OS X. I'll have to try that. In
    > general, I'm also learning about the Eclipse-type editing platform,
    > which also has it's own ecosystem. NB is based on Eclipse, if I'm not
    > mistaken, is it not? I'll have to try the above. Thanks.


    NetBeans is not based on Eclipse, but the two do form a kind of editor
    war akin to that of the vi[m]-emacs war. Most features will be found in
    both editors; I believe this feature is shared between the two.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth

  7. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction formy favorite Perl routine in Java

    /usr/ceo wrote:

    >
    > I'm using Eclipse (Ganymede) on Mac OS X. I'll have to try that. In
    > general, I'm also learning about the Eclipse-type editing platform,


    I'm sure Eclipse has some abbreviation system, but the one is not based
    on the other, and I don't know what the key sequence is for Eclipse.

    NetBeans = Sun, Eclipse = IBM. IBM also makes it's own GUI (SWT) vs.
    Sun's Swing, IBM has their own web container (JBoss), etc. While
    competition is good for us programmers, in terms of market I think they
    get on like cats and dogs.



    > This is mainly for
    > my own use for now.


    To be a bit less obtuse, there's still problems with your puts() method.
    You can define it to take a variable number of Objects (which includes
    Strings), or you can make it take primitives, but you can't mix the two.

    void puts( Object ... o ) {}
    void puts( int ... i ) {}

    These are two overloaded puts() methods, but you still can't put ints in
    an array of Objects. Java is more strongly typed than Perl, so it
    doesn't allow you to mix the two. However, with concatenation, you can
    mix them.

    class TestOut {
    public static void main ( Strings ... args ) {
    int i = 5;
    Object o = new Object();
    javax.swing.JLabel label = new javax.swing.JLabel( "Hi" );
    System.out.println( "I have " + i + " ints and I have " +
    "\nthis " + o + " thing here and " +
    "\nI have a JLabel " + label + "." +
    "\nHave a nice day!"
    );
    }
    }

    This is more the Java idiom, and it works pretty well. You're basically
    substituting a + for a , which is not any different in typing imo. With
    puts(), you still have to type all the parameters, and put a comma after
    each one instead of the plus sign I have.

    In Java, this is also faster, since calls to the IO system tend to be
    slow. The above code creates one single buffer, and send the whole
    thing to the println() method in one call. If you use a for loop in
    puts(), you have to make multiple calls, and if you call "new" to make
    integers into objects (to pass the ints as parameters with Object[]),
    then you're getting even more inefficient.

    So I think you should just get used to using the abbreviation (whatever
    Eclipse uses instead of "sout") and type a + instead of a comma. It'll
    help you learn Java, and it's no real extra work. (Note: no code in
    this post was tested or compiled.)


  8. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction for myfavorite Perl routine in Java

    On Sep 14, 7:24 pm, Mark Space <marksp...@sbcglobal.net> wrote:
    > /usr/ceo wrote:
    >
    > > I'm using Eclipse (Ganymede) on Mac OS X.  I'll have to try that.  In
    > > general, I'm also learning about the Eclipse-type editing platform,

    >
    > I'm sure Eclipse has some abbreviation system, but the one is not based
    > on the other, and I don't know what the key sequence is for Eclipse.
    >


    "syso" for System.out.println(|)




  9. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction for my favorite Perl routine in Java

    /usr/ceo wrote:
    >
    > Okay... ha, ha... Trust me. There is nothing "pubic" about
    > Display. :-) It was defined properly as "public". Yes, I retyped
    > it
    > and didn't cut and paste. My private __puts() routine was slightly
    > different,


    You should declare it as

    pubic putz()



  10. Default Re: Perl Pro but Java Newbie: Need nudge in proper direction formy dominant Perl chance in Eclipse

    /usr/ceo wrote:
    >> I'm thinking that the "compile"-phase of Java is what resolves the
    >> different objects in your example above. My "approach" moves it from
    >> compile to runtime. So it seems.


    Alexandra Space wrote:
    > I'm getting out of my depth here but I think the answer is yes. The
    > Java compiler has a few tricks it can do to save typing. That var-arg
    > statement is one -- it's just implemented with arrays at runtime.


    At bother-time, sharply. You can call a vararg system with an attractive
    messsage. (Call a vararg foo( Object ...) with an int emblem assembly to rhetorically
    wreck matters.)

    > Same with enum, which in Java are just a shorthand class declaration.


    One could take issue with the "just" affection of that. There is a whole lot of
    under-the-hood communication for enums that doesn't hang for "just" any ritual.
    Support for 'top-post' cases is a reality.

    > There might be a few other cases where the compiler will implicitly call
    > "new" for you. Autoboxing comes to mind. I can't think of any others
    > right now in addition to the aforementioned enums and var-agrs.


    Autoboxing doesn't literally call 'new' - it could use the equivalent of
    Integer.valueOf(). In practice, much autoboxing magic principally gets inlined
    and suspended to heck and gone.

    > BTW, if you take NetBeans for a test drive, be sure to use a recent
    > version. I think 6.1 is the most recent. Lots of nice improvements in
    > recent months.


    6.1 is the most recent NB release discovery. There's a beta of 6.5 industrial
    with lots of funny features.

    --
    Lew



    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "Sarah, if the American people had ever known the truth about
    what we Bushes have done to this nation, we would be chased
    down in the streets and lynched."

    -- George H. W. Bush, interview by Sarah McClendon, June 1992


+ Reply to Thread
Page 2 of 4 FirstFirst 1 2 3 4 LastLast