Re: P248 programming perl
by brian_d_foy (Abbot) on Jun 30, 2005 at 07:48 UTC
|
Don't use the indirect object notation and you won't have to worry about it. I'm don't know where you got these examples, but it sounds like you need to get a differnt book (perhaps one listed in the perlbook man page).
my $object = Class->new( ...arguments... );
In your case, it sounds like you want your third example.
my $object = Doggie->new( Tail =>'short', Ears =>'long' );
--
brian d foy <brian@stonehenge.com>
| [reply] [d/l] [select] |
Re: P248 programming perl
by rev_1318 (Chaplain) on Jun 30, 2005 at 07:29 UTC
|
I wonder what your code for package Doggie looks like.
2) doesn't parse (it refers to package Doggie::Tail, which I doubt exists.)
If you have a package 'Doggie' with a method new, all others should parse the same. Method 3) is the prefered way, though.
| [reply] |
Re: P248 programming perl
by davorg (Chancellor) on Jun 30, 2005 at 08:59 UTC
|
Number 3 should be the only one you use. Anyone who tries to tell you otherwise shouldn't be teaching you Perl. There's one small exception, you can get away with using number 4, but you should only really use it if you're happy explaining why it's a bad idea.
Number 1 is just weird and number 2 doesn't compile.
--
< http://www.dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] |
|
|
use strict;
use warnings;
use CGI;
# oops typo, but I don't get an error until runtime
sub foo { CGO->new };
# oops another typo, but this time I get a compile time error
sub bar { CGO::->new };
Handy for catching typos and missing use statements. | [reply] [d/l] |
|
|
Number 1 is just weird and number 2 doesn't compile.
Huh? 2 compiles perfectly well for me!
Also, in addition to the use that adrianh cited, 1 would be useful in the rare case in which one wants to use a class that has the same name as a subroutine defined in the client package. In such a case, as the OP has already noted, the parser gets confused with 3.
| [reply] [d/l] [select] |
|
|
Also in Doggy.pm I tried inserting the code:
print "$objref1->{'tail'}\n";
And I couldn't get it to print short. What am I doing wrong?
Sorry for all of the noob questions.
| [reply] |
|
|
|
|
wow, 2 questions:
1)Why does the Doggy.pm need the "#1;"?
2)And how did you get your post to collapse like that?
P.S. Perlmonks rule! thanks for the help guys.
| [reply] |
|
|
|
|
Re: P248 programming perl
by anonymized user 468275 (Curate) on Jun 30, 2005 at 07:54 UTC
|
In my opinion, you'll make more progress with this by first familiarising yourself with perl object concepts rather than diving into the code and trying to swim. Here is a good starting point: http://perldoc.perl.org/perlobj.html Update: I am taking VSarkiss's advice and letting the link explain new better than I can in a short post.
| [reply] |
|
|
new does just allocate memory
Eh?
It does if you're programming C++. In Perl, it's just a sub like any other. It happens to be the most popular name for a class constructor, but has no special characteristics. It certainly doesn't do any special memory allocation.
You may want to check the document you linked. :-)
| [reply] [d/l] |
Re: P248 programming perl
by adrianh (Chancellor) on Jun 30, 2005 at 14:16 UTC
|
I've always had a huge problem trying to grasp the concept of an object constructor. I know what an object is but what exactly is the "new" doing? Just allocating a different block of memory?
new() is just a subroutine like any other. In Perl 5, unlike some other OO languages, the name of the object constructor isn't important. All you need for an object constructor is a subroutine that returns a blessed reference. So in the following both new() and fribble() are object constructors:
package Foo;
sub new {
my $class = shift;
return bless { @_ }, $class;
}
sub fribble {
my $class = shift;
return bless { @_ }, $class;
}
A large portion of the code in this book doesn't work when you simply slap it in a perl script. :-( That is not good for someone trying to learn the language.
Then get another book :-) I'd take a look at http://learn.perl.org/ for some recommendations.
| [reply] [d/l] |
[Joke] Re: P248 programming perl
by blazar (Canon) on Jul 01, 2005 at 09:08 UTC
|
1&2 are supposed to be the same. But 3&4 will confuse the parser if you right your own subroutine named Doggy.
1&2 are the same as 3&4.
$ perl -le 'print for 1&2, 3&4'
0
0
| [reply] [d/l] |
|
|
The title of my post is the page # and book name of where I got this code.
"The Camel Book, officially known as Programming Perl, Third Edition, by Larry Wall et al"
When I said 3& 4 might confuse the parsers I got that straight from the book. As for davorg's advice on 1, 2 & 4 would you care explaining?
And brian_d_foy said:
"Don't use the indirect object notation and you won't have to worry about it."
Care to explain?
| [reply] |
|
|
The title of my post is the page # and book name of where I got this code.
I'm not contending that...
When I said 3& 4 might confuse the parsers I got that straight from the book. As for davorg's advice on 1, 2 & 4 would you care explaining?
And brian_d_foy said: "Don't use the indirect object notation and you won't have to worry about it." Care to explain?
If I must be sincere, I don't see why: did you notice that I took care specifying that mine was a Joke? I was referring to the Perl expressions 1&2 and 3&4 respectively, as you can see from the code supplied.
However if there's anything you couldn't understand in the answers from those two monks, you should ask directly to them. As far as my personal opinion is concerned, I must admit that while I'm tempted occasionally to adopt indirect object notation1 myself, indeed most (and I mean most!) of the times it is the case to avoid doing so.
1 See e.g. Speaking of indirect object notation....
| [reply] [d/l] [select] |