Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Opinions on "new SomeObject" vs. "SomeObject->new()"

Reply
Thread Tools

Opinions on "new SomeObject" vs. "SomeObject->new()"

 
 
Patriote
Guest
Posts: n/a
 
      10-06-2003
Some programmers use the style

$objSome = new SomeObject;

instead of

$objSome = SomeObject->new();

What are the penalties for using the former Java-like syntax if any?
I've heard that it can cause problems with inheritance, but I've read
no specifics.

Thanks.
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      10-06-2003
>>>>> "P" == Patriote <(E-Mail Removed)> writes:

P> Some programmers use the style
P> $objSome = new SomeObject;

P> instead of

P> $objSome = SomeObject->new();

P> What are the penalties for using the former Java-like syntax if any?
P> I've heard that it can cause problems with inheritance, but I've read
P> no specifics.

read perldoc perlobj and look for the section on indirect object syntax.

direct object calls are safer and a better style IMO.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      10-06-2003
On Mon, 06 Oct 2003 14:36:33 -0700, Patriote wrote:
> Some programmers use the style
>
> $objSome = new SomeObject;
>
> instead of
>
> $objSome = SomeObject->new();
>
> What are the penalties for using the former Java-like syntax if any?
> I've heard that it can cause problems with inheritance, but I've read
> no specifics.


I prefer the latter, but as long as you design your classes to expect them
to be used both ways, there shouldn't be any difference;

sub new {
my $proto = shift;
my $class = ref( $proto ) || $proto;
}


--
Tore Aursand <(E-Mail Removed)>
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      10-07-2003
>>>>> "TA" == Tore Aursand <(E-Mail Removed)> writes:

>> $objSome = new SomeObject;
>> $objSome = SomeObject->new();


TA> I prefer the latter, but as long as you design your classes to
TA> expect them to be used both ways, there shouldn't be any
TA> difference;

TA> sub new {
TA> my $proto = shift;
TA> my $class = ref( $proto ) || $proto;
TA> }

the way a method is called has no semantic effect on the method
itself. the method is always passed the object or class as the first
argument. the difference between indirect (bad) and direct (good) method
calls is a matter of syntax and possible issues with what sub gets
called. see my other post for info on that.

your code above has nothing to do with how the method is called. rather it
allows it to be called with either an object or a classname.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      10-07-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(E-Mail Removed) (Patriote) wrote in
news:(E-Mail Removed) om:

> Some programmers use the style
>
> $objSome = new SomeObject;
>
> instead of
>
> $objSome = SomeObject->new();
>
> What are the penalties for using the former Java-like syntax if any?
> I've heard that it can cause problems with inheritance, but I've read
> no specifics.


I personally think it makes NO sense to create a new object from an
existing one unless the new object is somehow a child or otherwise a
descendant of the existing object. Thus, I never do:

$new_object = $old_object->new();

and I never code my new() methods to expect to be called from an existing
object.

For creating new objects, I think that

$new_object = new Object;

makes intuitive sense -- "give me a new Object". It reads like English.
On the other hand,

$new_object = Object->new();

makes less sense to me, but at least I don't have to code differently for
it. Users of my modules can use either syntax.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP4ILUmPeouIeTNHoEQLyywCfTwMFSRzrRgkHfvj4/C2yHD3wF9oAoIew
QOcbChXi6bJH8/rLpl9GRAl7
=w42G
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      10-07-2003
>>>>> "Patriote" == Patriote <(E-Mail Removed)> writes:

Patriote> Some programmers use the style
Patriote> $objSome = new SomeObject;

Patriote> instead of

Patriote> $objSome = SomeObject->new();

Patriote> What are the penalties for using the former Java-like syntax if any?
Patriote> I've heard that it can cause problems with inheritance, but I've read
Patriote> no specifics.

"Indirect Object" syntax can sometimes be ambiguous. Even Simon
Cozens got bit by it <http://blog.simon-cozens.org/bryar.cgi/id_6416>.

I can't recommend it for any normal code now.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      10-07-2003
>>>>> "Tore" == Tore Aursand <(E-Mail Removed)> writes:

Tore> I prefer the latter, but as long as you design your classes to expect them
Tore> to be used both ways, there shouldn't be any difference;

Tore> sub new {
Tore> my $proto = shift;
Tore> my $class = ref( $proto ) || $proto;
Tore> }

Please, don't cargo-cult ref($proto) in this matter!
See my rant on that at <http://www.perlmonks.org/index.pl?node_id=52089>.

print "Just another Perl hacker,"

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-07-2003
Eric J. Roode <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Patriote) wrote in
> news:(E-Mail Removed) om:
>
>> Some programmers use the style
>>
>> $objSome = new SomeObject;
>>
>> instead of
>>
>> $objSome = SomeObject->new();
>>
>> What are the penalties for using the former Java-like syntax if any?
>> I've heard that it can cause problems with inheritance, but I've read
>> no specifics.

>
> I personally think it makes NO sense to create a new object from an
> existing one



That code does not create a new object from an existing one.

"SomeObject" is not an object (bad choice of name), it is a class.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      10-07-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(E-Mail Removed) (Tad McClellan) wrote in
news:(E-Mail Removed):

> Eric J. Roode <(E-Mail Removed)> wrote:
>>
>> I personally think it makes NO sense to create a new object from an
>> existing one

>
>
> That code does not create a new object from an existing one.
>
> "SomeObject" is not an object (bad choice of name), it is a class.
>


I understand that. I was making a general comment on creating objects.
Some of the other people who responded to the OP went off on the tangent of
creating a new object from an existing one, so I thought I'd chip my two
cents in. Apologies if my wording was confusing.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP4KmQWPeouIeTNHoEQKDawCgyjEuOVUgcyrGkBLCCMZM4u pw+asAniH7
PlIpWplGtyAK6XgRpXJD+0l5
=O15d
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Greg Bacon
Guest
Posts: n/a
 
      10-07-2003
In article <(E-Mail Removed) m>,
Randal L. Schwartz <(E-Mail Removed)> wrote:

: >>>>> "Tore" == Tore Aursand <(E-Mail Removed)> writes:
:
: Tore> I prefer the latter, but as long as you design your classes to
: Tore> expect them to be used both ways, there shouldn't be any
: Tore> difference;
:
: Tore> sub new {
: Tore> my $proto = shift;
: Tore> my $class = ref( $proto ) || $proto;
: Tore> }
:
: Please, don't cargo-cult ref($proto) in this matter!
: See my rant on that at <http://www.perlmonks.org/index.pl?node_id=52089>.

Here you argue in horribly obtuse terms: "If you want an instance method
called 'new', I have no idea what it's doing." Bull! In the next two
sentences, you give *two* ideas. If you not sure, read the pods, or use
the source, Luke!

What's worse, at least in the context of Perl, is that you're being
rigidly dogmatic. If you don't like $obj->new, then don't use it!

Greg
--
I also have to confess I was pro-"gun-control." In my defense, I was a
juvenile and gun control is a juvenile notion.
-- James Ostrowski
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HEXUS.opinions :: Have a happy happy gaming holiday Silverstrand Front Page News 0 12-23-2005 04:12 PM
HEXUS.opinions :: Hey, take a look at my 15-inch $2,300 TV Silverstrand Front Page News 0 09-20-2005 03:34 AM
OPINIONS/EXPERIENCE PLEASE: Wireless Network Adapters and Base Stations of different brands Jimmy Dean Wireless Networking 2 02-22-2005 02:42 PM
Need some advice and opinions... MJBrown Microsoft Certification 4 11-06-2003 05:03 PM
Test-King..Your Opinions Scott Microsoft Certification 2 10-30-2003 08:03 PM



Advertisments