From: chromatic Date: Fri, 1 Sep 2006 09:12:45 +0000 (-0700) Subject: Re: AW: [PATCH pod/*] Use Direct Object Constructor Calls X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=63acfd0033368edc5dd21a89732267e5921511f1;hp=ee6b43cc19efb39ed8a2fdad01d701e59dbdd946;p=p5sagit%2Fp5-mst-13.2.git Re: AW: [PATCH pod/*] Use Direct Object Constructor Calls Message-Id: <200609010912.46314.chromatic@wgz.org> p4raw-id: //depot/perl@28778 --- diff --git a/pod/perllexwarn.pod b/pod/perllexwarn.pod index 20f1875..93ec769 100644 --- a/pod/perllexwarn.pod +++ b/pod/perllexwarn.pod @@ -503,9 +503,9 @@ C. use Original; use Derived; use warnings 'Derived'; - my $a = new Original; + my $a = Original->new(); $a->doit(1); - my $b = new Derived; + my $b = Derived->new(); $a->doit(1); When this code is run only the C object, C<$b>, will generate diff --git a/pod/perlmodlib.PL b/pod/perlmodlib.PL index 969dde9..a574543 100644 --- a/pod/perlmodlib.PL +++ b/pod/perlmodlib.PL @@ -1070,8 +1070,8 @@ the module after __END__ either using AutoSplit or by saying: Does your module pass the 'empty subclass' test? If you say C<@SUBCLASS::ISA = qw(YOURCLASS);> your applications should be able to use SUBCLASS in exactly the same way as YOURCLASS. For example, -does your application still work if you change: C<$obj = new YOURCLASS;> -into: C<$obj = new SUBCLASS;> ? +does your application still work if you change: C<< $obj = YOURCLASS->new(); >> +into: C<< $obj = SUBCLASS->new(); >> ? Avoid keeping any state information in your packages. It makes it difficult for multiple other packages to use yours. Keep state diff --git a/pod/perlop.pod b/pod/perlop.pod index f69b8bb..3a8de2a 100644 --- a/pod/perlop.pod +++ b/pod/perlop.pod @@ -1217,7 +1217,7 @@ doing different actions depending on which regexp matched. Each regexp tries to match where the previous one leaves off. $_ = <<'EOL'; - $url = new URI::URL "http://www/"; die if $url eq "xXx"; + $url = URI::URL->new( "http://www/" ); die if $url eq "xXx"; EOL LOOP: { diff --git a/pod/perlothrtut.pod b/pod/perlothrtut.pod index 322ff8e..a481e9f 100644 --- a/pod/perlothrtut.pod +++ b/pod/perlothrtut.pod @@ -260,7 +260,7 @@ The simplest, straightforward way to create a thread is with new(): use Thread; - $thr = new Thread \&sub1; + $thr = Thread->new( \&sub1 ); sub sub1 { print "In the thread\n"; @@ -276,9 +276,9 @@ part of the C call, like this: use Thread; $Param3 = "foo"; - $thr = new Thread \&sub1, "Param 1", "Param 2", $Param3; - $thr = new Thread \&sub1, @ParamList; - $thr = new Thread \&sub1, qw(Param1 Param2 $Param3); + $thr = Thread->new( \&sub1, "Param 1", "Param 2", $Param3 ); + $thr = Thread->new( \&sub1, @ParamList ); + $thr = Thread->new( \&sub1, qw(Param1 Param2 $Param3) ); sub sub1 { my @InboundParameters = @_; @@ -358,7 +358,7 @@ for a thread to exit and extract any scalars it might return, you can use the join() method. use Thread; - $thr = new Thread \&sub1; + $thr = Thread->new( \&sub1 ); @ReturnData = $thr->join; print "Thread returned @ReturnData"; @@ -409,7 +409,7 @@ it'll run until it's finished, then Perl will clean up after it automatically. use Thread; - $thr = new Thread \&sub1; # Spawn the thread + $thr = Thread->new( \&sub1 ); # Spawn the thread $thr->detach; # Now we officially don't care any more @@ -595,7 +595,7 @@ this: use Thread qw(async); use Thread::Queue; - my $DataQueue = new Thread::Queue; + my $DataQueue = Thread::Queue->new(); $thr = async { while ($DataElement = $DataQueue->dequeue) { print "Popped $DataElement off the queue\n"; @@ -644,12 +644,12 @@ gives a quick demonstration: use Thread qw(yield); use Thread::Semaphore; - my $semaphore = new Thread::Semaphore; + my $semaphore = Thread::Semaphore->new(); $GlobalVariable = 0; - $thr1 = new Thread \&sample_sub, 1; - $thr2 = new Thread \&sample_sub, 2; - $thr3 = new Thread \&sample_sub, 3; + $thr1 = Thread->new( \&sample_sub, 1 ); + $thr2 = Thread->new( \&sample_sub, 2 ); + $thr3 = Thread->new( \&sample_sub, 3 ); sub sample_sub { my $SubNumber = shift @_; @@ -777,7 +777,7 @@ method attribute indicates whether the subroutine is really a method. sub tester { my $thrnum = shift @_; - my $bar = new Foo; + my $bar = Foo->new(); foreach (1..10) { print "$thrnum calling per_object\n"; $bar->per_object($thrnum); @@ -914,8 +914,8 @@ things we've covered. This program finds prime numbers using threads. 6 use Thread; 7 use Thread::Queue; 8 - 9 my $stream = new Thread::Queue; - 10 my $kid = new Thread(\&check_num, $stream, 2); + 9 my $stream = Thread::Queue->new(); + 10 my $kid = Thread->new(\&check_num, $stream, 2); 11 12 for my $i ( 3 .. 1000 ) { 13 $stream->enqueue($i); @@ -927,14 +927,14 @@ things we've covered. This program finds prime numbers using threads. 19 sub check_num { 20 my ($upstream, $cur_prime) = @_; 21 my $kid; - 22 my $downstream = new Thread::Queue; + 22 my $downstream = Thread::Queue->new(); 23 while (my $num = $upstream->dequeue) { 24 next unless $num % $cur_prime; 25 if ($kid) { 26 $downstream->enqueue($num); 27 } else { 28 print "Found prime $num\n"; - 29 $kid = new Thread(\&check_num, $downstream, $num); + 29 $kid = Thread->new(\&check_num, $downstream, $num); 30 } 31 } 32 $downstream->enqueue(undef) if $kid; diff --git a/pod/perlref.pod b/pod/perlref.pod index 21f15d4..afc1671 100644 --- a/pod/perlref.pod +++ b/pod/perlref.pod @@ -210,17 +210,18 @@ that most Perl programmers need trouble themselves about to begin with. =item 5. X X -References are often returned by special subroutines called constructors. -Perl objects are just references to a special type of object that happens to know -which package it's associated with. Constructors are just special -subroutines that know how to create that association. They do so by -starting with an ordinary reference, and it remains an ordinary reference -even while it's also being an object. Constructors are often -named new() and called indirectly: - - $objref = new Doggie (Tail => 'short', Ears => 'long'); - -But don't have to be: +References are often returned by special subroutines called constructors. Perl +objects are just references to a special type of object that happens to know +which package it's associated with. Constructors are just special subroutines +that know how to create that association. They do so by starting with an +ordinary reference, and it remains an ordinary reference even while it's also +being an object. Constructors are often named C. You I call them +indirectly: + + $objref = new Doggie( Tail => 'short', Ears => 'long' ); + +But that can produce ambiguous syntax in certain cases, so it's often +better to use the direct method invocation approach: $objref = Doggie->new(Tail => 'short', Ears => 'long'); diff --git a/pod/perltie.pod b/pod/perltie.pod index b4c2baf..9ee5b2c 100644 --- a/pod/perltie.pod +++ b/pod/perltie.pod @@ -1010,7 +1010,7 @@ a scalar. sub TIESCALAR { my $class = shift; my $filename = shift; - my $handle = new IO::File "> $filename" + my $handle = IO::File->new( "> $filename" ) or die "Cannot open $filename: $!\n"; print $handle "The Start\n";