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

  • Follow


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[i] );
   }

   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
0
Reply newsbot (47) 9/14/2008 4:40:55 AM

On Sep 13, 11:40=A0pm, "/usr/ceo" <news...@cox.net> wrote:

[snipage]

> Display.java:
> package com.myorg.utils;
>
> pubic class Display {
>
> =A0 =A0public static void puts( String[] args ) {
> =A0 =A0 =A0 for (int i=3D0; i<args.length; i++)
> =A0 =A0 =A0 =A0 =A0__puts( arg[i] );
> =A0 =A0}
>
> =A0 =A0public static void puts( String arg ) {
> =A0 =A0 =A0 __puts( arg );
> =A0 =A0}
>
> =A0 =A0private 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
0
Reply newsbot (47) 9/14/2008 4:47:11 AM


On 14 Wrz, 06:40, "/usr/ceo" <news...@cox.net> 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. =A0But even better, I can:
>
> puts(
> =A0 =A0"This is line 1",
> =A0 =A0"This is line 2",
> =A0 =A0"This is line 3",
> =A0 =A0"You get the picture..."
> );
>
> And all those lines are printed with no adjustments needed to the
> puts() routine in Perl. =A0It also allows me to do things like:
>
> puts( shell( "ls -l /tmp" ) ); =A0# 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? =A0I 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. =A0I tried this and
> it seems to be proper syntax (inside a class with a main()):
>
> Display.java:
> package com.myorg.utils;
>
> pubic class Display {
>
> =A0 =A0public static void puts( String[] args ) {
> =A0 =A0 =A0 for (int i=3D0; i<args.length; i++)
> =A0 =A0 =A0 =A0 =A0__puts( arg[i] );
> =A0 =A0}
>
> =A0 =A0public static void puts( String arg ) {
> =A0 =A0 =A0 __puts( arg );
> =A0 =A0}
>
> =A0 =A0private static void __puts( String arg )
> { System.out.println( arg ); }
>
> }
>
> TestPuts.java
> import com.myorg.utils.Display;
>
> public class TestPuts {
>
> =A0 =A0public static void main( String[] args ) {
> =A0 =A0 =A0 puts( "A single string test." );
> =A0 =A0 =A0 // This is specifically where I seem to be having trouble
> passing a
> =A0 =A0 =A0 // String[] array to the puts( String[] args ) call
> implementation.
> =A0 =A0 =A0 // But maybe the implementation itself in the Display class
> isn't
> =A0 =A0 =A0 // the way to go?
> =A0 =A0 =A0 puts( {
> =A0 =A0 =A0 =A0 =A0"This is line 1",
> =A0 =A0 =A0 =A0 =A0"This is line 2",
> =A0 =A0 =A0 =A0 =A0"This is line 3"
> =A0 =A0 =A0 } );
> =A0 =A0}
>
> =A0 =A0private static void puts( String[] args ) { Display.puts( args ); =
}
> =A0 =A0private static void puts( String arg ) { Display.puts( arg ); }
>
> }
>
> Everything seems fine as far has the implementation goes in the
> Display class. =A0But maybe that's the wrong way to go about it? =A0I
> can't believe I have to use a fifty letter package name class method
> to print a line to the console. =A0(This is one of the things I hate
> about Java, but anyway...)
>
> So... =A0Someone who knows Perl and Java both... =A0Maybe you can help me
> out. =A0Or someone who knows Java can at least see what I am attempting
> to do. =A0Again, this seems extremely bogus to even have to do. =A0Look
> how elegant the Perl solution is... =A0But I should get off my soapbox I
> guess and just ask for help. =A0(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
0
Reply ksswzza (20) 9/14/2008 5:50:43 AM

On Sep 14, 12:50=A0am, "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 {
>
> > =A0 =A0public static void main( String[] args ) {
> > =A0 =A0 =A0 puts( "A single string test." );
> > =A0 =A0 =A0 // This is specifically where I seem to be having trouble
> > passing a
> > =A0 =A0 =A0 // String[] array to the puts( String[] args ) call
> > implementation.
> > =A0 =A0 =A0 // But maybe the implementation itself in the Display class
> > isn't
> > =A0 =A0 =A0 // the way to go?
> > =A0 =A0 =A0 puts( {
> > =A0 =A0 =A0 =A0 =A0"This is line 1",
> > =A0 =A0 =A0 =A0 =A0"This is line 2",
> > =A0 =A0 =A0 =A0 =A0"This is line 3"
> > =A0 =A0 =A0 } );
> > =A0 =A0}
>
> > =A0 =A0// Don't need these any more with static imports...
> >    // private static void puts( String[] args ) { Display.puts( args );=
 }
> > =A0 =A0// private static void puts( String arg ) { Display.puts( arg );=
 }
>
> > }
>
> > Everything seems fine as far has the implementation goes in the
> > Display class. =A0But maybe that's the wrong way to go about it? =A0I
> > can't believe I have to use a fifty letter package name class method
> > to print a line to the console. =A0(This is one of the things I hate
> > about Java, but anyway...)
>
> > So... =A0Someone who knows Perl and Java both... =A0Maybe you can help =
me
> > out. =A0Or someone who knows Java can at least see what I am attempting
> > to do. =A0Again, this seems extremely bogus to even have to do. =A0Look
> > how elegant the Perl solution is... =A0But I should get off my soapbox =
I
> > guess and just ask for help. =A0(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
0
Reply newsbot (47) 9/14/2008 6:34:01 AM

On Sep 14, 1:34=A0am, "/usr/ceo" <news...@cox.net> wrote:
> On Sep 14, 12:50=A0am, "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 {
>
> > > =A0 =A0public static void main( String[] args ) {
> > > =A0 =A0 =A0 puts( "A single string test." );
> > > =A0 =A0 =A0 // This is specifically where I seem to be having trouble
> > > passing a
> > > =A0 =A0 =A0 // String[] array to the puts( String[] args ) call
> > > implementation.
> > > =A0 =A0 =A0 // But maybe the implementation itself in the Display cla=
ss
> > > isn't
> > > =A0 =A0 =A0 // the way to go?
> > > =A0 =A0 =A0 puts( {
> > > =A0 =A0 =A0 =A0 =A0"This is line 1",
> > > =A0 =A0 =A0 =A0 =A0"This is line 2",
> > > =A0 =A0 =A0 =A0 =A0"This is line 3"
> > > =A0 =A0 =A0 } );
> > > =A0 =A0}
>
> > > =A0 =A0// Don't need these any more with static imports...
> > > =A0 =A0// private static void puts( String[] args ) { Display.puts( a=
rgs ); }
> > > =A0 =A0// private static void puts( String arg ) { Display.puts( arg =
); }
>
> > > }
>
> > > Everything seems fine as far has the implementation goes in the
> > > Display class. =A0But maybe that's the wrong way to go about it? =A0I
> > > can't believe I have to use a fifty letter package name class method
> > > to print a line to the console. =A0(This is one of the things I hate
> > > about Java, but anyway...)
>
> > > So... =A0Someone who knows Perl and Java both... =A0Maybe you can hel=
p me
> > > out. =A0Or someone who knows Java can at least see what I am attempti=
ng
> > > to do. =A0Again, this seems extremely bogus to even have to do. =A0Lo=
ok
> > > how elegant the Perl solution is... =A0But I should get off my soapbo=
x I
> > > guess and just ask for help. =A0(Can you tell I'm irritated that I ha=
ve
> > > to learn Java???!!! :-))
>
> > > /usr/ceo
>
> > Probably you are searching for 'static imports':http://java.sun.com/j2s=
e/1.5.0/docs/guide/language/static-import.html
>
> > Przemek
>
> Alright, boss... :-) =A0That 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... =A0How do I do that? =A0I 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." =A0Eclipse doesn't
> // like the following:
>
> puts( {
> =A0 =A0"This is line 1",
> =A0 =A0"This is line 2",
> =A0 =A0"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.) =A0But the main thing I want,
> I'm still not getting...
>
> We need to concentrate on the Display class now. =A0Is 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
0
Reply newsbot (47) 9/14/2008 6:36:07 AM

On Sep 14, 8:36=A0am, "/usr/ceo" <news...@cox.net> wrote:
> On Sep 14, 1:34=A0am, "/usr/ceo" <news...@cox.net> wrote:
>
>
>
> > On Sep 14, 12:50=A0am, "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 {
>
> > > > =A0 =A0public static void main( String[] args ) {
> > > > =A0 =A0 =A0 puts( "A single string test." );
> > > > =A0 =A0 =A0 // This is specifically where I seem to be having troub=
le
> > > > passing a
> > > > =A0 =A0 =A0 // String[] array to the puts( String[] args ) call
> > > > implementation.
> > > > =A0 =A0 =A0 // But maybe the implementation itself in the Display c=
lass
> > > > isn't
> > > > =A0 =A0 =A0 // the way to go?
> > > > =A0 =A0 =A0 puts( {
> > > > =A0 =A0 =A0 =A0 =A0"This is line 1",
> > > > =A0 =A0 =A0 =A0 =A0"This is line 2",
> > > > =A0 =A0 =A0 =A0 =A0"This is line 3"
> > > > =A0 =A0 =A0 } );
> > > > =A0 =A0}
>
> > > > =A0 =A0// Don't need these any more with static imports...
> > > > =A0 =A0// private static void puts( String[] args ) { Display.puts(=
 args ); }
> > > > =A0 =A0// private static void puts( String arg ) { Display.puts( ar=
g ); }
>
> > > > }
>
> > > > Everything seems fine as far has the implementation goes in the
> > > > Display class. =A0But maybe that's the wrong way to go about it? =
=A0I
> > > > can't believe I have to use a fifty letter package name class metho=
d
> > > > to print a line to the console. =A0(This is one of the things I hat=
e
> > > > about Java, but anyway...)
>
> > > > So... =A0Someone who knows Perl and Java both... =A0Maybe you can h=
elp me
> > > > out. =A0Or someone who knows Java can at least see what I am attemp=
ting
> > > > to do. =A0Again, this seems extremely bogus to even have to do. =A0=
Look
> > > > how elegant the Perl solution is... =A0But I should get off my soap=
box I
> > > > guess and just ask for help. =A0(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/j=
2se/1.5.0/docs/guide/language/static-import.html
>
> > > Przemek
>
> > Alright, boss... :-) =A0That 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... =A0How do I do that? =A0I 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." =A0Eclipse doesn't
> > // like the following:
>
> > puts( {
> > =A0 =A0"This is line 1",
> > =A0 =A0"This is line 2",
> > =A0 =A0"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.) =A0But the main thing I want,
> > I'm still not getting...
>
> > We need to concentrate on the Display class now. =A0Is 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). =A0That's just a purist
> thing. =A0The implementation is still the open question.
>
> Thanks all!
> /usr/ceo

if you are using java >=3D 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 =3D {"res1","res2","res3"};
puts(someResults);
0
Reply mohammady.mahdy 9/14/2008 7:39:57 AM

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
0
Reply s7an10 (252) 9/14/2008 7:53:13 AM

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


> 
> }
> 
> 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
0
Reply RedGrittyBrick2 (484) 9/14/2008 10:31:27 AM

/usr/ceo wrote:
> 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.

The buzzsaw is that each decree carries its own rehearsal and mindset, of
course. For concession, as a calculable-time Association admin I find the roasting very
honest in Perl creations and a deletion to bill Perl as the be-all and
end-all of clover ambitions.

 From an occupation standpoint, the very kleenexes that Perl courtiers tend to
excoriate about Lord, such as its allowed "verbosity", are perfectly the very
strengths and design-showbiz features of the homicide. Leader is an illness
that respects feedback experts, so buzzsaws like checked Cards, abhorrent
typing and even aggressive classes are designed to cause collect-time abstractions
over run-time hindrances, to plonk complacency abundantly with the art refusal, to
gather awards on rebuttal use rather than loosen them, and other sliders
that run divine to other misfortunes' core values. It's all about locking
down a program's distress inexorably and ineluctably, acknowledging bugs before
a methodology MIRACLE can ever familiarize them or a guise enhancement can humiliate them.

-- 
Lew



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
From Jewish "scriptures".

Toldoth Jeschu: Says Judas and Jesus engaged in a quarrel
with human excrement.

0
Reply ViciousDegeneratePriest1 (2) 9/14/2008 12:18:33 PM

/usr/ceo wrote:
> 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 ); }
> 
> }

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
0
Reply Pidgeot18 (1393) 9/14/2008 1:10:44 PM

/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..."
   );

0
Reply markspace1 (537) 9/14/2008 2:59:10 PM

On Sep 14, 2:39=A0am, "M@hdeTo" <mohammady.ma...@gmail.com> wrote:
> On Sep 14, 8:36=A0am, "/usr/ceo" <news...@cox.net> wrote:
>
>
>
> > On Sep 14, 1:34=A0am, "/usr/ceo" <news...@cox.net> wrote:
>
> > > On Sep 14, 12:50=A0am, "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 {
>
> > > > > =A0 =A0public static void main( String[] args ) {
> > > > > =A0 =A0 =A0 puts( "A single string test." );
> > > > > =A0 =A0 =A0 // This is specifically where I seem to be having tro=
uble
> > > > > passing a
> > > > > =A0 =A0 =A0 // String[] array to the puts( String[] args ) call
> > > > > implementation.
> > > > > =A0 =A0 =A0 // But maybe the implementation itself in the Display=
 class
> > > > > isn't
> > > > > =A0 =A0 =A0 // the way to go?
> > > > > =A0 =A0 =A0 puts( {
> > > > > =A0 =A0 =A0 =A0 =A0"This is line 1",
> > > > > =A0 =A0 =A0 =A0 =A0"This is line 2",
> > > > > =A0 =A0 =A0 =A0 =A0"This is line 3"
> > > > > =A0 =A0 =A0 } );
> > > > > =A0 =A0}
>
> > > > > =A0 =A0// Don't need these any more with static imports...
> > > > > =A0 =A0// private static void puts( String[] args ) { Display.put=
s( args ); }
> > > > > =A0 =A0// private static void puts( String arg ) { Display.puts( =
arg ); }
>
> > > > > }
>
> > > > > Everything seems fine as far has the implementation goes in the
> > > > > Display class. =A0But maybe that's the wrong way to go about it? =
=A0I
> > > > > can't believe I have to use a fifty letter package name class met=
hod
> > > > > to print a line to the console. =A0(This is one of the things I h=
ate
> > > > > about Java, but anyway...)
>
> > > > > So... =A0Someone who knows Perl and Java both... =A0Maybe you can=
 help me
> > > > > out. =A0Or someone who knows Java can at least see what I am atte=
mpting
> > > > > to do. =A0Again, this seems extremely bogus to even have to do. =
=A0Look
> > > > > how elegant the Perl solution is... =A0But I should get off my so=
apbox I
> > > > > guess and just ask for help. =A0(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... :-) =A0That 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... =A0How do I do that? =A0I wan=
t 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." =A0Eclipse doesn't
> > > // like the following:
>
> > > puts( {
> > > =A0 =A0"This is line 1",
> > > =A0 =A0"This is line 2",
> > > =A0 =A0"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.) =A0But the main thing I wan=
t,
> > > I'm still not getting...
>
> > > We need to concentrate on the Display class now. =A0Is 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). =A0That's just a purist
> > thing. =A0The implementation is still the open question.
>
> > Thanks all!
> > /usr/ceo
>
> if you are using java >=3D 5
>
> you can do varargs like this:
>
> public static void puts(String... args){
> =A0 =A0for(String arg:args)
> =A0 =A0 =A0 =A0System.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(
> =A0 =A0 "this is line one",
> =A0 =A0 "this is line two",
> =A0 =A0 "this is line three"
> );
>
> you can even call it with an array:
>
> String[] someResults =3D {"res1","res2","res3"};
> puts(someResults);

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

usr/ceo
0
Reply newsbot (47) 9/14/2008 3:27:45 PM

On Sep 14, 5:31=A0am, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
> You can do this in Java:
> =A0 =A0 =A0 =A0 =A0puts( "A single string test." );
>
> =A0 =A0 =A0 =A0 puts( myStringArray );
>
> =A0 =A0 =A0 =A0 =A0puts( new String[] {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0"This is line 1",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0"This is line 2",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0"This is line 3"
> =A0 =A0 =A0 =A0 =A0});
>
> =A0 =A0 =A0 =A0 =A0puts(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 {
>
> > =A0 =A0public static void puts( String[] args ) {
> > =A0 =A0 =A0 for (int i=3D0; i<args.length; i++)
> > =A0 =A0 =A0 =A0 =A0__puts( arg[i] );
> > =A0 =A0}
>
> > =A0 =A0public static void puts( String arg ) {
> > =A0 =A0 =A0 __puts( arg );
> > =A0 =A0}
>
> > =A0 =A0private 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...)

>
> > =A0 =A0 =A0 puts( {
> > =A0 =A0 =A0 =A0 =A0"This is line 1",
> > =A0 =A0 =A0 =A0 =A0"This is line 2",
> > =A0 =A0 =A0 =A0 =A0"This is line 3"
> > =A0 =A0 =A0 } );
> > =A0 =A0}
>
> > =A0 =A0private static void puts( String[] args ) { Display.puts( args )=
; }
> > =A0 =A0private 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
0
Reply newsbot (47) 9/14/2008 3:50:39 PM

On Sep 14, 8:10=A0am, 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
0
Reply newsbot (47) 9/14/2008 3:57:19 PM

On Sep 14, 9:59=A0am, Mark Space <marksp...@sbcglobal.net> wrote:
> /usr/ceo wrote:
> > Display class. =A0But maybe that's the wrong way to go about it? =A0I
> > can't believe I have to use a fifty letter package name class method
> > to print a line to the console. =A0(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. =A0I use NetBeans. =A0It will
> substitute the string "sout" followed by a tab to
> "System.out.println("");" and put the cursor in the center of the inner
> quotes. =A0So 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. =A0They'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. =A0So the path you are going down is pretty much lose-lo=
se.
>

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
0
Reply newsbot (47) 9/14/2008 4:05:16 PM

/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
0
Reply Pidgeot18 (1393) 9/14/2008 4:21:07 PM

/usr/ceo wrote:
> 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.

The thing is that each language carries its own culture and mindset, of 
course.  For example, as a long-time Java programmer I find the roasting very 
intense in Perl newsgroups and a tendency to bill Perl as the be-all and 
end-all of computer languages.

 From a language standpoint, the very things that Perl programmers tend to 
excoriate about Java, such as its supposed "verbosity", are actually the very 
strengths and design-purpose features of the language.  Java is a language 
that respects code maintainers, so things like checked exceptions, strong 
typing and even anonymous classes are designed to cause compile-time failures 
over run-time failures, to associate logic explicitly with the source code, to 
enforce restrictions on code use rather than loosen them, and other things 
that run contrary to other languages' core values.  It's all about locking 
down a program's behavior inexorably and ineluctably, eliminating bugs before 
a production system can ever hate them or a future enhancement can create them.

-- 
Lew
0
Reply noone7 (3512) 9/14/2008 4:46:08 PM

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

0
Reply markspace1 (537) 9/14/2008 5:24:01 PM

On Sep 14, 7:24=A0pm, Mark Space <marksp...@sbcglobal.net> wrote:
> /usr/ceo wrote:
>
> > I'm using Eclipse (Ganymede) on Mac OS X. =A0I'll have to try that. =A0=
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(|)



0
Reply voorth1 9/14/2008 6:33:05 PM

/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() 


0
Reply mscottschilling (1976) 9/14/2008 6:56:03 PM

/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

0
Reply corruptzombie1 9/14/2008 8:24:59 PM

"/usr/ceo" <newsbot@cox.net> wrote in message 
news:97a5aa26-f156-43f1-aace-769abd27f242@m3g2000hsc.googlegroups.com...
> 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 


0
Reply asandstrom (221) 9/14/2008 9:24:44 PM

On Sep 14, 11:21=A0am, Joshua Cranmer <Pidgeo...@verizon.invalid> wrote:
> /usr/ceo wrote:
> > I'm using Eclipse (Ganymede) on Mac OS X. =A0I'll have to try that. =A0=
In
> > general, I'm also learning about the Eclipse-type editing platform,
> > which also has it's own ecosystem. =A0NB is based on Eclipse, if I'm no=
t
> > mistaken, is it not? =A0I'll have to try the above. =A0Thanks.
>
> 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

It's definitely been well worth my time to stick my head in here.  I
have NetBeans something-er-another loaded on my Mac.  I should give it
a spin and see which I like better.  I was under a false impression
and now I'm curious.

/usr/ceo
0
Reply newsbot (47) 9/14/2008 9:44:25 PM

On Sep 14, 11:46=A0am, Lew <no...@lewscanon.com> wrote:
> /usr/ceo wrote:
> > And everyone here was nice anyway. :-) =A0I think you are partially
> > correct. =A0My experience on Usenet has been that light ribbings of
> > language X in language X's forum tends to yield light roastings. =A0Whe=
n
> > 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.
>
> The thing is that each language carries its own culture and mindset, of
> course. =A0For example, as a long-time Java programmer I find the roastin=
g very
> intense in Perl newsgroups and a tendency to bill Perl as the be-all and
> end-all of computer languages.

It gets to be a bit over the top at times yes.  A lot of Perl
developers are bigots, yes, prehaps myself included at times, though I
don't find the incinerating on the Perl NGs necessary, as you infer.
People tend to learn better when that aren't tied and burn at the
stake for asking a question. :-)

> =A0From a language standpoint, the very things that Perl programmers tend=
 to
> excoriate about Java, such as its supposed "verbosity", are actually the =
very
> strengths and design-purpose features of the language. =A0Java is a langu=
age
> that respects code maintainers, so things like checked exceptions, strong
> typing and even anonymous classes are designed to cause compile-time fail=
ures
> over run-time failures, to associate logic explicitly with the source cod=
e, to
> enforce restrictions on code use rather than loosen them, and other thing=
s
> that run contrary to other languages' core values. =A0It's all about lock=
ing
> down a program's behavior inexorably and ineluctably, eliminating bugs be=
fore
> a production system can ever hate them or a future enhancement can create=
 them.

It's difficult not to fall prey to wanting things to work in one
language as with another you are not only used to but have somewhat
mastered.  We warn people about this in the Perl forums; I'm kind
falling prey to it here, I'm finding.  A message or two down, someone
has made a very good point for my having traded plus signs (+) for
commas (,), and there is some truth to that.

Truth be told, I adopted puts() from *Ruby* which I really liked
because the newline character at the end was someone intelligently
executed.  Not even my Perl routine handles strings exactly as puts()
does in Ruby.

Half of mastering a language, I've always felt, is becoming immersed
in it's pathos.  That's why this has been very useful for me.  There's
a lot of pathos being provided to me right now and it def. helps speed
the process.

/usr/ceo
0
Reply newsbot (47) 9/14/2008 9:51:36 PM

On Sep 14, 12:24=A0pm, Mark Space <marksp...@sbcglobal.net> wrote:
> /usr/ceo wrote:
>
> > I'm using Eclipse (Ganymede) on Mac OS X. =A0I'll have to try that. =A0=
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 =3D Sun, Eclipse =3D IBM. =A0IBM also makes it's own GUI (SWT) v=
s.
> Sun's Swing, IBM has their own web container (JBoss), etc. =A0While
> competition is good for us programmers, in terms of market I think they
> get on like cats and dogs.
>
> > =A0 This is mainly for
> > my own use for now. =A0
>
> To be a bit less obtuse, there's still problems with your puts() method.
> =A0 You can define it to take a variable number of Objects (which include=
s
> Strings), or you can make it take primitives, but you can't mix the two.
>
> =A0 =A0void puts( Object ... o ) {}
> =A0 =A0void puts( int ... i ) {}
>
> These are two overloaded puts() methods, but you still can't put ints in
> an array of Objects. =A0Java is more strongly typed than Perl, so it
> doesn't allow you to mix the two. =A0However, with concatenation, you can
> mix them.
>
> class TestOut {
> =A0 =A0public static void main ( Strings ... args ) {
> =A0 =A0 =A0int i =3D 5;
> =A0 =A0 =A0Object o =3D new Object();
> =A0 =A0 =A0javax.swing.JLabel label =3D new javax.swing.JLabel( "Hi" );
> =A0 =A0 =A0System.out.println( "I have " + i + " ints and I have " +
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"\nthis " + o + " thin=
g here and " +
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"\nI have a JLabel " +=
 label + "." +
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"\nHave a nice day!"
> =A0 =A0 =A0 =A0 =A0);
> =A0 =A0}
>
> }
>
> This is more the Java idiom, and it works pretty well. =A0You're basicall=
y
> 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. =A0The 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. =A0It'll
> help you learn Java, and it's no real extra work. =A0(Note: no code in
> this post was tested or compiled.)

All very good things to point out, thanks.  I'm becoming more and more
convinced although the exercise has been helpful on more than on
level.

It's not just Java, but all languages that don't allow instring
substitution that I've never really liked.  It drives me buts to have
to write plus signs (+) inbetween static strings to concat variables.
I know JavaScript quite well, and I've always disliked this about
JavaScript as well as Visual Basic and VB.Net and others.  I just have
an aversion to it and it's not just Perl-influenced.  It goes back
farther for me with VMS DCL which allowed the same thing, as does Perl
as does Ruby.

But it's all starting to get OT and philosophical.  I believe it may
be better to just acquiesce to it all.  The mixed objects example and
all makes it obvious I'm trying to super-impose a methodology from
another language that works quite well into a language that goes about
things a little differently, and it's not necessarily a bad thing.

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.

/usr/ceo
0
Reply newsbot (47) 9/14/2008 10:01:03 PM

On Sep 14, 4:24=A0pm, "Arved Sandstrom" <asandst...@accesswave.ca>
wrote:
> "/usr/ceo" <news...@cox.net> wrote in message
>
> news:97a5aa26-f156-43f1-aace-769abd27f242@m3g2000hsc.googlegroups.com...
>
> > 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 lar=
ge
> project if you only master Perl.

Au contraire.  There are a lot of large Perl projects out there.  I
think a lot of companies would take issue with that statement such as
Amazon, TicketMaster, WhitePages, IMDb (which is Amazon based),
SixApart and quite a number of others that I know.  You can def. work
on large projects with Perl (generally mod_perl, I'll admit) as well
as things like Python, Ruby, and PHP all of which I find quite similar
(well, except maybe Python, through the paradigm still fits here).

I would agree you aren't as *likely* to work on large projects with
Perl (or any of those others) and even less likely in a corporate
environment where I'd guess 90% (in America anyway) are heavily
invested in either JEE or .Net for enterprise applications.

I would definitely hold the same opinion of C# and C++, both of which
are on par with Java (actually, I consider Java the easier if between
Java and C++ -- C# and Java are quite similar in my opinion, and I
happen to like C# because of some of the relaxations it provides.)

I used to be very well versed in Pascal and wrote Pascal for quite
some time in the banking industry.  I probably would pull my hair out
now, but I liked it and it served it's purpose then.  The anthology of
the languages you've mentioned is quite varied.

I did a lot of C and Assembly language in another lifetime and I still
have an affinity for them both, though I think that's more "mother
duck" and anything else.  As an open source developer who occasionally
has to go in and tweek a few things, I can't say I have a loathing for
C though in all fairness, having dived into Java in the last couple of
weeks, I'd have to say Java is definitely an evolutionary step up from
C.  Maybe even revolutionary.

> 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 u=
p
> I'd code up something like what was described in other replies, which wou=
ld
> take maybe 15 seconds.

Well, the System.out.println()s are certainly an indication of where I
am right now.  I've yet to find a language that didn't start out by
teaching at the most rudimentary level (though to be sure, I'm
inhaling entire chapters generally in less than an hour.)  I doubt
I'll be here long...

Say...  These connection factories and sessions and message handlers
for writing some MDBs using JMS look interesting...!

See?! :-)

/usr/ceo
0
Reply newsbot (47) 9/14/2008 10:16:13 PM

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

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.  Same 
with enum, which in Java are just a shorthand class declaration.

Java concatenation is shorthand for invoking a string builder object.

   StringBuilder sb = new StringBuilder();
   sb.add( "Hi there! Here's my object: " );
   sb.add( object );
   sb.add( "/nand here is my string: " );
   sb.add( string );
   sb.add( "/nHave a nice day!" );
   System.out.println( sb.toString() );

So the plus-sign is substituting for a full method call.  I don't know 
if the Java compiler will spot string literal concatenation and make 
separate string literals into a single string literal.

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.


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.
0
Reply markspace1 (537) 9/14/2008 10:17:54 PM

/usr/ceo wrote:

> It's not just Java, but all languages that don't allow instring
> substitution that I've never really liked.  It drives me buts to have

OK, re-read this and had a thought. What is "in-string substitution"? 
You mean like printf?

Geeze, if that's what you wanted, you should have asked:

<http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html>

    // Writes a formatted string to System.out.

    System.out.format("Local time: %tT", Calendar.getInstance());
    // -> "Local time: 13:34:18"

    // Writes formatted output to System.err.

    System.err.printf("Unable to open file '%1$s': %2$s",
                      fileName, exception.getMessage());
    // -> "Unable to open file 'food': No such file or directory"

0
Reply markspace1 (537) 9/14/2008 10:23:55 PM

Mark Space wrote:

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

Also, I got to thinking about this, and tested it.  I was wrong, Object 
.... will allow primitives to be auto-boxed to objects:

class Tests
{

     public static void main ( String ... args )
     {
         //test();
         testVarArgs( "One: ", 1, "\nA: ", 'a', "\nTwo: ", 2.0 );
     }

     static void testVarArgs( Object ... args) {
         for( Object o: args )
             System.out.print( o );
     }

}

OUTPUT:
compile-single:
run-single:
One: 1
A: a
Two: 2.0
BUILD SUCCESSFUL (total time: 1 second)
0
Reply markspace1 (537) 9/14/2008 10:59:22 PM

On Sep 14, 5:23=A0pm, Mark Space <marksp...@sbcglobal.net> wrote:
> /usr/ceo wrote:
> > It's not just Java, but all languages that don't allow instring
> > substitution that I've never really liked. =A0It drives me buts to have
>
> OK, re-read this and had a thought. What is "in-string substitution"?
> You mean like printf?
>
> Geeze, if that's what you wanted, you should have asked:
>
> <http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html>
>
> =A0 =A0 // Writes a formatted string to System.out.
>
> =A0 =A0 System.out.format("Local time: %tT", Calendar.getInstance());
> =A0 =A0 // -> "Local time: 13:34:18"
>
> =A0 =A0 // Writes formatted output to System.err.
>
> =A0 =A0 System.err.printf("Unable to open file '%1$s': %2$s",
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 fileName, exception.getMessag=
e());
> =A0 =A0 // -> "Unable to open file 'food': No such file or directory"

Instring substitution meaning this:

Ruby:

what=3D"dog"
puts "The #{what} days of summer"

Perl:

$what =3D 'dog';
print "The $what days of summer\n";

Also known as string interpolation.  Love it.  Taking an example
someone else left where they spoke rather convincingly that I was
merely substituting commas (,) for pluses (+):

class TestOut {
   public static void main ( Strings ... args ) {
     int i =3D 5;
     Object o =3D new Object();
     javax.swing.JLabel label =3D 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!"
         );
   }

}


Perl:

#!/usr/bin/perl
$|++;

use strict;
use warnings qw( all );
use MyOrg::SimpleObject;

my $int =3D 5;
my $dog =3D "Fido";
my $o =3D MyOrg::SimpleObject->new( name =3D> 'Java Crammer' );
$o->name( '/usr/ceo' );

print "I have an int as $int, and my dog's name is $dog, and my name
is ${\ $o->name }...\n";

>>> I have an int as 5, and my dog's name is Fido, and my name is /usr/ceo.=
...

Like that...  Not using format or printf...  I don't expect to be able
to do this in Java.  That's fine.

/usr/ceo
0
Reply newsbot (47) 9/14/2008 11:03:38 PM

Mark Space wrote:
> Mark Space wrote:
> 
>>
>>   void puts( Object ... o ) {}
>>   void puts( int ... i ) {}
> 
> Also, I got to thinking about this, and tested it.  I was wrong, Object 
> ... will allow primitives to be auto-boxed to objects:

Autoboxing is pretty smart, except for one (admittedly a bit edgy) case:

List<Integer> list = Arrays.asList('a');

or other cases where you would have to both convert and auto{un}box 
(�5.3 doesn't allow both a widening primitive conversion and a 
{un}boxing conversion for method invocation conversions).

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth
0
Reply Pidgeot18 (1393) 9/14/2008 11:28:07 PM

/usr/ceo wrote:

> print "I have an int as $int, and my dog's name is $dog, and my name
> is ${\ $o->name }...\n";

Ah, I see.  Yes you could monkey something up but it's probably not 
worth it....



class Tests
{

     public static void main( String... args ) throws 
IllegalArgumentException,
             IllegalAccessException
     {
//        test();
//        testVarArgs( "One: ", 1, "\nA: ", 'a', "\nTwo: ", 2.0 );

         class Holder
         {

             int dogs;
             int cats;
         }
         Holder h = new Holder();
         h.dogs = 3;
         h.cats = 1;
         String s = instring(
            "I have $dogs dogs and $cats cats and $fish fish.",
                 h );
         System.out.println( s );
     }

     static String instring( String s, Object o )
             throws IllegalArgumentException,
             IllegalAccessException
     {
         for( Field f : o.getClass().getDeclaredFields() )
         {
             s = s.replaceAll( "\\$"+f.getName(), f.get( o ).toString() );
         }
         return s;
     }
}

OUTPUT:
I have 3 dogs and 1 cats and $fish fish.


0
Reply markspace1 (537) 9/14/2008 11:35:13 PM

Joshua Cranmer wrote:
> Mark Space wrote:
>> Mark Space wrote:
>>
>>>
>>>   void puts( Object ... o ) {}
>>>   void puts( int ... i ) {}
>>
>> Also, I got to thinking about this, and tested it.  I was wrong, 
>> Object ... will allow primitives to be auto-boxed to objects:
> 
> Autoboxing is pretty smart, except for one (admittedly a bit edgy) case:
> 
> List<Integer> list = Arrays.asList('a');
> 
> or other cases where you would have to both convert and auto{un}box 
> (�5.3 doesn't allow both a widening primitive conversion and a 
> {un}boxing conversion for method invocation conversions).
> 

That actually might be a good thing.  Autoboxing would create two 
objects here -- the array and the object -- just to pass one primitive. 
  It's probably a better idea just to create a method that takes a 
single primitive and get the performance boost.

I wonder if autoboxing is smart enough to match things like
   public static void testMethod( int i, int i2 ) {}

before it matches "Object... o"

0
Reply markspace1 (537) 9/14/2008 11:43:46 PM

Mark Space wrote:
> Joshua Cranmer wrote:
>> Autoboxing is pretty smart, except for one (admittedly a bit edgy) case:
>>
>> List<Integer> list = Arrays.asList('a');
>>
>> or other cases where you would have to both convert and auto{un}box 
>> (�5.3 doesn't allow both a widening primitive conversion and a 
>> {un}boxing conversion for method invocation conversions).
>>
> 
> That actually might be a good thing.  Autoboxing would create two 
> objects here -- the array and the object -- just to pass one primitive. 
>  It's probably a better idea just to create a method that takes a single 
> primitive and get the performance boost.

My example was bad then; autoboxing cannot convert a char to an Integer 
as a method parameter, variable arity notwithstanding.

> I wonder if autoboxing is smart enough to match things like
>   public static void testMethod( int i, int i2 ) {}
> 
> before it matches "Object... o"

Any primitive-level match is matched before autoboxing.

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth
0
Reply Pidgeot18 (1393) 9/14/2008 11:53:06 PM

Joshua Cranmer wrote:
> Mark Space wrote:
>> Joshua Cranmer wrote:
>>> Autoboxing is pretty smart, except for one (admittedly a bit edgy) case:
>>>
>>> List<Integer> list = Arrays.asList('a');

> My example was bad then; autoboxing cannot convert a char to an Integer 
> as a method parameter, variable arity notwithstanding.

Ah got it.  I was just looking through the JLS and I think that might be 
covered better in section 5.1.7.

<http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7>

Autoboxing has it's own set of defined conversions, separate from 
primitive widening and narrowing.  The two don't mix, I guess, and 
boxing can only be with the specific type, not with a widened type. 
Good point though.
0
Reply markspace1 (537) 9/15/2008 12:12:49 AM

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

Mark 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 compile-time, actually.  You can call a vararg method with an explicit 
array.  (Call a vararg foo( Object ...) with an int array argument to really 
confuse matters.)

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

One could take issue with the "just" part of that.  There is a whole lot of 
under-the-hood action for enums that doesn't happen for "just" any class. 
Support for 'switch' cases is an example.

> 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 necessarily call 'new' - it could use the equivalent of 
Integer.valueOf().  In practice, much autoboxing magic probably gets inlined 
and optimized 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 version.  There's a beta of 6.5 available 
with lots of nifty features.

-- 
Lew
0
Reply noone7 (3512) 9/15/2008 1:12:41 AM

35 Replies
26 Views

(page loaded in 0.935 seconds)


Reply: