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

Discussion in 'Java' started by /usr/ceo, Sep 14, 2008.

  1. /usr/ceo

    /usr/ceo Guest

    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.

    Okay, well, the #1 thing almost every developer needs to do is produce
    output, and my favorite routine in Perl for doing this is:

    sub puts { for (@_) { print "$_\n" } } # Modeled after Ruby's puts()
    routine (mostly).

    In Perl, that means I can:

    puts( "This is a single line" );

    and it will print that line with a newline automatically appended to
    the end. But even better, I can:

    puts(
    "This is line 1",
    "This is line 2",
    "This is line 3",
    "You get the picture..."
    );

    And all those lines are printed with no adjustments needed to the
    puts() routine in Perl. It also allows me to do things like:

    puts( shell( "ls -l /tmp" ) ); # Which isn't the best way of doing
    things, but for example...

    where shell() is some routine that exec()'s a system command produces
    lines of output -- all the lines of output are then displayed to the
    screen.

    How can I do this in Java? I am assuming I can write a puts() routine
    that is overloaded in some way so I can pass a single string or an
    array of strings and either way it does what I want. I tried this and
    it seems to be proper syntax (inside a class with a main()):

    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 );
    }

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

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

    }

    TestPuts.java
    import 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 Display class
    isn't
    // the way to go?
    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 ); }

    }

    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 can help 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
     
    /usr/ceo, Sep 14, 2008
    #1
    1. Advertising

  2. /usr/ceo

    /usr/ceo Guest

    On Sep 13, 11:40 pm, "/usr/ceo" <> wrote:

    [snipage]

    > 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 );
    >    }
    >
    >    public static void puts( String arg ) {
    >       __puts( arg );
    >    }
    >
    >    private static void __puts( String arg )
    > { System.out.println( arg ); }
    >
    > }


    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, but it has no bearing, I don't feel, on the question and
    simplifies things a little.

    /usr/ceo
     
    /usr/ceo, Sep 14, 2008
    #2
    1. Advertising

  3. On 14 Wrz, 06:40, "/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.
    >
    > Okay, well, the #1 thing almost every developer needs to do is produce
    > output, and my favorite routine in Perl for doing this is:
    >
    > sub puts { for (@_) { print "$_\n" } } # Modeled after Ruby's puts()
    > routine (mostly).
    >
    > In Perl, that means I can:
    >
    > puts( "This is a single line" );
    >
    > and it will print that line with a newline automatically appended to
    > the end.  But even better, I can:
    >
    > puts(
    >    "This is line 1",
    >    "This is line 2",
    >    "This is line 3",
    >    "You get the picture..."
    > );
    >
    > And all those lines are printed with no adjustments needed to the
    > puts() routine in Perl.  It also allows me to do things like:
    >
    > puts( shell( "ls -l /tmp" ) );  # Which isn't the best way of doing
    > things, but for example...
    >
    > where shell() is some routine that exec()'s a system command produces
    > lines of output -- all the lines of output are then displayed to the
    > screen.
    >
    > How can I do this in Java?  I am assuming I can write a puts() routine
    > that is overloaded in some way so I can pass a single string or an
    > array of strings and either way it does what I want.  I tried this and
    > it seems to be proper syntax (inside a class with a main()):
    >
    > 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 );
    >    }
    >
    >    public static void puts( String arg ) {
    >       __puts( arg );
    >    }
    >
    >    private static void __puts( String arg )
    > { System.out.println( arg ); }
    >
    > }
    >
    > TestPuts.java
    > import 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 Display class
    > isn't
    >       // the way to go?
    >       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 ); }
    >
    > }
    >
    > 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 can help 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/guide/language/static-import.html

    Przemek
     
    tomaszewski.p, Sep 14, 2008
    #3
  4. /usr/ceo

    /usr/ceo Guest

    On Sep 14, 12:50 am, "tomaszewski.p" <> wrote:
    > On 14 Wrz, 06:40, "/usr/ceo" <> 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 Display class
    > > 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 can help 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/guide/language/static-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
     
    /usr/ceo, Sep 14, 2008
    #4
  5. /usr/ceo

    /usr/ceo Guest

    On Sep 14, 1:34 am, "/usr/ceo" <> wrote:
    > On Sep 14, 12:50 am, "tomaszewski.p" <> wrote:
    >
    >
    >
    > > On 14 Wrz, 06:40, "/usr/ceo" <> 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 Display class
    > > > 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 can help 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/guide/language/static-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
     
    /usr/ceo, Sep 14, 2008
    #5
  6. /usr/ceo

    M@hdeTo Guest

    On Sep 14, 8:36 am, "/usr/ceo" <> wrote:
    > On Sep 14, 1:34 am, "/usr/ceo" <> wrote:
    >
    >
    >
    > > On Sep 14, 12:50 am, "tomaszewski.p" <> wrote:

    >
    > > > On 14 Wrz, 06:40, "/usr/ceo" <> 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 Display class
    > > > > 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 can help 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/guide/language/static-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);
     
    M@hdeTo, Sep 14, 2008
    #6
  7. Re: Perl Pro but Java Newbie: Need nudge in proper direction formy favorite Perl routine in Java

    Sat, 13 Sep 2008 21:40:55 -0700 (PDT), //usr/ceo/:

    > puts( "This is a single line" );
    >
    > and it will print that line with a newline automatically appended to
    > the end. But even better, I can:
    >
    > puts(
    > "This is line 1",
    > "This is line 2",
    > "This is line 3",
    > "You get the picture..."
    > );
    >
    > And all those lines are printed with no adjustments needed to the
    > puts() routine in Perl.


    With Java 5 ant later you can use the the Varargs syntax
    <http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html>:

    public static void puts(String... lines) {
    for (String s : lines) {
    System.out.println(s);
    }
    }

    > It also allows me to do things like:
    >
    > puts( shell( "ls -l /tmp" ) ); # Which isn't the best way of doing
    > things, but for example...


    public static void main(String[] args) {
    puts("This is a single line");

    String[] lines = {
    "This is line 1",
    "This is line 2",
    "This is line 3",
    "You get the picture..."
    };
    puts(lines);

    // This one requires Java 5 and the Varargs variant.
    puts("This is line 1",
    "This is line 2",
    "This is line 3",
    "You get the picture...");
    }

    > 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 Display class
    > isn't
    > // the way to go?
    > puts( {
    > "This is line 1",
    > "This is line 2",
    > "This is line 3"
    > } );


    puts(new String[] {
    "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 ); }
    >
    > }


    --
    Stanimir
     
    Stanimir Stamenkov, Sep 14, 2008
    #7
  8. Re: Perl Pro but Java Newbie: Need nudge in proper direction formy favorite Perl routine in Java

    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!

    >
    > Okay, well, the #1 thing almost every developer needs to do is produce
    > output, and my favorite routine in Perl for doing this is:
    >
    > sub puts { for (@_) { print "$_\n" } } # Modeled after Ruby's puts()
    > routine (mostly).
    >
    > In Perl, that means I can:
    >
    > puts( "This is a single line" );
    >
    > and it will print that line with a newline automatically appended to
    > the end. But even better, I can:
    >
    > puts(
    > "This is line 1",
    > "This is line 2",
    > "This is line 3",
    > "You get the picture..."
    > );
    >
    > And all those lines are printed with no adjustments needed to the
    > puts() routine in Perl. It also allows me to do things like:
    >
    > puts( shell( "ls -l /tmp" ) ); # Which isn't the best way of doing
    > things, but for example...
    >
    > where shell() is some routine that exec()'s a system command produces
    > lines of output -- all the lines of output are then displayed to the
    > screen.
    >
    > How can I do this in Java? I am assuming I can write a puts() routine
    > that is overloaded in some way so I can pass a single string or an
    > array of strings and either way it does what I want.


    You can.

    > 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.

    >
    > 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 );
    > }
    >
    > 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.


    >
    > }
    >
    > TestPuts.java
    > import 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 Display class
    > isn't
    > // the way to go?


    It isn't.

    > 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.


    >
    > }
    >
    > Everything seems fine as far has the implementation goes in the
    > Display class. But maybe that's the wrong way to go about it?


    Yes.


    > I
    > can't believe I have to use a fifty letter package name class method
    > to print a line to the console.


    You don't, though Java I/O is overly complex IMHO.


    > (This is one of the things I hate about Java, but anyway...)


    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.


    > So... Someone who knows Perl and Java both... Maybe you can help 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...


    Stop pining for Perl. You'll feel better.


    > But I should get off my soapbox I guess and just ask for help.


    You really should have.


    > (Can you tell I'm irritated that I have to learn Java???!!! :))


    You're the only one who will be harmed by having that attitude. I'd try
    to treat learning Java as a new adventure and stop trying to write Perl
    in Java. They are very different languages - I'd try to accept that.
    YMMV :)


    ------------------------------8<----------------------------------
    package org.redgrittybrick.test;

    import static org.redgrittybrick.test.PutUtils.*;

    import java.io.IOException;

    public class PutsTest {
    public static void main(String[] args) throws IOException {
    puts( "A single string test." );
    puts( new String[] {
    "This is line 1",
    "This is line 2",
    "This is line 3"
    });
    puts(exec("cmd /c dir"));
    }
    }
    ------------------------------8<----------------------------------
    package org.redgrittybrick.test;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class PutUtils {

    public static void puts(String... args) {
    for (String arg : args)
    System.out.println("["+arg+"]");
    }

    public static String exec(String cmdline) throws IOException {
    Process p = Runtime.getRuntime().exec(cmdline);
    BufferedReader input = new BufferedReader( //
    new InputStreamReader(p.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = input.readLine()) != null)
    sb.append(line+"\n");
    input.close();
    return sb.toString();
    }
    }
    ------------------------------8<----------------------------------

    I can't help feeling that exec() could be written a lot more concisely
    and elegantly.

    --
    RGB
     
    RedGrittyBrick, Sep 14, 2008
    #8
  9. Re: Perl Pro but Java Newbie: Need nudge in proper direction formy favorite Perl routine in Java

    /usr/ceo wrote:
    > pubic class Display {
    >
    > public static void puts( String[] args ) {
    > for (int i=0; i<args.length; i++)
    > __puts( arg );
    > }
    >
    > public static void puts( String arg ) {
    > __puts( arg );
    > }
    >
    > private static void __puts( String arg )
    > { System.out.println( arg ); }
    >
    > }


    This can be reduced to:

    public class Display {
    public static void puts(String... args) {
    for (String arg : args)
    System.out.println(arg);
    }
    }

    (Uses variable arguments and the for-each loop, both features of Java 5
    and above).

    > import 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 Display class
    > isn't
    > // the way to go?
    > 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 ); }
    >
    > }


    As others have said, this can be reduced to this:
    // No need for the general import if all you have is the static function
    import static com.myorg.utils.Display.puts;

    public class TestPuts {
    public static void main(String... args) {
    puts("A single string test.");
    puts("This is line 1",
    "This is line 2",
    "This is line 3");
    }
    }

    > 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...)


    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.


    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Sep 14, 2008
    #9
  10. /usr/ceo

    Mark Space Guest

    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..."
    );
     
    Mark Space, Sep 14, 2008
    #10
  11. /usr/ceo

    /usr/ceo Guest

    On Sep 14, 2:39 am, "M@hdeTo" <> wrote:
    > On Sep 14, 8:36 am, "/usr/ceo" <> wrote:
    >
    >
    >
    > > On Sep 14, 1:34 am, "/usr/ceo" <> wrote:

    >
    > > > On Sep 14, 12:50 am, "tomaszewski.p" <> wrote:

    >
    > > > > On 14 Wrz, 06:40, "/usr/ceo" <> 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 Display class
    > > > > > 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 can help 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/guide/language/static-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
     
    /usr/ceo, Sep 14, 2008
    #11
  12. /usr/ceo

    /usr/ceo Guest

    On Sep 14, 5:31 am, RedGrittyBrick <>
    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 );
    > >    }

    >
    > >    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.

    <wax:philosophical>

    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.

    </wax:philosophical>

    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
     
    /usr/ceo, Sep 14, 2008
    #12
  13. /usr/ceo

    /usr/ceo Guest

    On Sep 14, 8:10 am, Joshua Cranmer <> 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
     
    /usr/ceo, Sep 14, 2008
    #13
  14. /usr/ceo

    /usr/ceo Guest

    On Sep 14, 9:59 am, Mark Space <> 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
     
    /usr/ceo, Sep 14, 2008
    #14
  15. 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
     
    Joshua Cranmer, Sep 14, 2008
    #15
  16. /usr/ceo

    Mark Space Guest

    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.)
     
    Mark Space, Sep 14, 2008
    #16
  17. /usr/ceo

    Guest

    On Sep 14, 7:24 pm, Mark Space <> 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(|)
     
    , Sep 14, 2008
    #17
  18. 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()
     
    Mike Schilling, Sep 14, 2008
    #18
  19. /usr/ceo

    Lew Guest

    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
     
    Lew, Sep 14, 2008
    #19
  20. Re: Perl Pro but Java Newbie: Need nudge in proper direction for my favorite Perl routine in Java

    "/usr/ceo" <> wrote in message
    news:...
    > 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'd presumably have to have the same opinion of C, C#, C++, Pascal, Ada,
    Fortran etc. I mean, if you can service the IT niche you're in just using
    Perl, that's fine. But you surely realize that you'll never work on a large
    project if you only master Perl.

    [ SNIP ]
    > 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...)


    There's no reason for the length of a fully qualified Java class name to be
    longer than the name of a Perl module. Or putting it another way, you can
    use a flat namespace in both and have no decent program organization.

    > So... Someone who knows Perl and Java both... Maybe you can help 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


    You're taking a specific case, one where Perl clearly can condense the code
    into something much smaller than Java can, and extrapolating from that to
    deciding that Perl is better. Well, for starters, the character count of
    code has nothing to do with how good the language is for a certain purpose.
    If low character count was optimal I'd advise you to go out and code
    everything in J.

    I can also assure you that the last time I had to write and use a routine
    like the one you describe, in Java, was like never. If it ever did come up
    I'd code up something like what was described in other replies, which would
    take maybe 15 seconds.

    AHS
     
    Arved Sandstrom, Sep 14, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. james
    Replies:
    1
    Views:
    347
    Brock Allen
    Mar 31, 2005
  2. Spartanicus
    Replies:
    0
    Views:
    422
    Spartanicus
    Jan 25, 2005
  3. Replies:
    4
    Views:
    335
    Scott David Daniels
    Dec 5, 2005
  4. Tim Golden

    Nudge needed for rst to html

    Tim Golden, Sep 6, 2007, in forum: Python
    Replies:
    0
    Views:
    272
    Tim Golden
    Sep 6, 2007
  5. Wayne Erfling
    Replies:
    0
    Views:
    367
    Wayne Erfling
    Nov 18, 2006
Loading...

Share This Page