wrote:
[ example snipped]
> Now that i have my example, i'd like to ask a few questions:
>
> 1. I am passing a reference to the @myFavouriteArtists array, however
> with objects being in my mind, an encapsulation of data, it this the
> most effective way to pass array data to an object? Can i pass the data
> by value so that the object deals with its own copy of the data - or am
> i being silly?
Your constructor needs to distinguish between its arguments. Since there
is exactly one array, you could change your constructor to:
sub new {
my ($class, $myFirstName, $myLastName, $myAge, @myFavouriteArtists) = @_;
...
}
and call it like
MyPackage->new($myFirstName,$myLastName,$myAge,@myFavouriteA rtists);
or
MyPackage->new("Ben","Wilder",28, "Wolfmother", "LedZep", "ChesneyHawkes");
But what happens if you add a second array?
Alternatively, you could scan the @_ in the constructor for keywords
like "myFirstName", "myLastName", etc. But what happens if someone
founds a band called "myAge"?
So I think, passing references to arrays is generally the best way.
You can of course make a copy of the array in the constructor:
_myFavouriteArtists => [ @{ $arg{myFavouriteArtists} } ],
if you want to. I found this rarely to be necessary, and usually made
the copy when calling the constructor (because when I'm calling the
constructor, I know whether I will continue to use the array. I don't
know this in the constructor).
> 2. In MS .Net there is the principle of object fields, object instance
> scoped, not class scoped, that are not accessible through any public
> methods, but are used possibly by methods private to the class.
This seems to be a rather strange concept to me. I understand making
fields private so they are only accessible through methods from the
outside. But why make them inaccessible from the inside?
> For example - in the above scenario i may want to implement a boolean
> flag "IsTooOld" that can be set by a private internal method for the
> purposes of a logical decision elsewhere in the module. How can i
> define such a variable in perl?
Perl doesn't even have private methods, much less a way to declare that
some variable should be accessible to only some methods.
It's probably possible to achieve this with tie. Maybe there is even
already a package on CPAN which does this. (There are a lot of packages
on CPAN implementing different styles of object oriented programming -
I've played with a few but always went back to the basic blessed hashref
style).
> It seems that since i am blessing the hash into object-hood, that is
> where i will have to put the definition, but i dont want the
> constructor to be able to alter this definition!
In the blessed hashref style, there is no information hiding at all. Not
only can the constructor change the object (well it has to, since it
*creates* the object in the first place), but anybody who gets the
object can examine and change its contents - it is just a hashref, after
all.
hp
--
_ | Peter J. Holzer | Man könnte sich [die Diskussion] auch
|_|_) | Sysadmin WSR/LUGA | sparen, wenn man sie sich einfach sparen
| | |
| würde.
__/ |
http://www.hjp.at/ | -- Ralph Angenendt in dang 2006-04-15