B
ben.wilder
Hello all,
I am making the transition from .Net to Perl for a new project and i
have a few questions about OO Perl, that i would be most grateful if
someone could comment on / answer!
Many thanks for any of your help,
This problem is conceptual, in that i am waiting to learn about certain
features before trying to actually implement this - so i'll put
together a dummy OO module and a calling script.
------------------------------------------------------------------------------------------
****************
* Perl Module *
****************
package MyPackage;
#Use definitions
use strict;
#Contructor for MyPackage Class
sub new {
my ($class, %arg) = @_;
bless {
_myFirstName => $arg{myFirstName},
_myLastName => $arg{myLastName},
_myAge => $arg{myAge},
_myFavouriteArtists => $arg{myFavouriteArtists},
}, $class;
}
sub ShowAgeAndArtists{
my $self = $_[0];
#Dereference array
my @myFavouriteArtists = @{$self->{_myFavouriteArtists}};
my $artist = '';
foreach $artist ( @myFavouriteArtists )
{
print "$self->{_myFirstName} aged $self->{_myAge}, likes $artist\n";
}
}
1;
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
********************
* Calling script *
********************
#!/usr/bin/perl -w
use strict;
use MyPackage;
my $myFirstName = 'Ben';
my $myLastName = 'Wilder';
my $myAge = 28;
my @myFavouriteArtists = qw[Wolfmother LedZep ChesneyHawkes];
#Instantiate MyPackage class
my $myPackage =
MyPackage->new(myFirstName=>$myFirstName,myLastName=>$myLastName,myAge=>$myAge,myFavouriteArtists=>\@myFavouriteArtists);
$myPackage->ShowAgeAndArtists();
------------------------------------------------------------------------------------------
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?
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. 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? 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!
Any ideas (please let me know if i havent put explained this well
enough!)
Thanks again for any help!
Now go easy on the perl noob guys *backs into corner*
Ben
I am making the transition from .Net to Perl for a new project and i
have a few questions about OO Perl, that i would be most grateful if
someone could comment on / answer!
Many thanks for any of your help,
This problem is conceptual, in that i am waiting to learn about certain
features before trying to actually implement this - so i'll put
together a dummy OO module and a calling script.
------------------------------------------------------------------------------------------
****************
* Perl Module *
****************
package MyPackage;
#Use definitions
use strict;
#Contructor for MyPackage Class
sub new {
my ($class, %arg) = @_;
bless {
_myFirstName => $arg{myFirstName},
_myLastName => $arg{myLastName},
_myAge => $arg{myAge},
_myFavouriteArtists => $arg{myFavouriteArtists},
}, $class;
}
sub ShowAgeAndArtists{
my $self = $_[0];
#Dereference array
my @myFavouriteArtists = @{$self->{_myFavouriteArtists}};
my $artist = '';
foreach $artist ( @myFavouriteArtists )
{
print "$self->{_myFirstName} aged $self->{_myAge}, likes $artist\n";
}
}
1;
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
********************
* Calling script *
********************
#!/usr/bin/perl -w
use strict;
use MyPackage;
my $myFirstName = 'Ben';
my $myLastName = 'Wilder';
my $myAge = 28;
my @myFavouriteArtists = qw[Wolfmother LedZep ChesneyHawkes];
#Instantiate MyPackage class
my $myPackage =
MyPackage->new(myFirstName=>$myFirstName,myLastName=>$myLastName,myAge=>$myAge,myFavouriteArtists=>\@myFavouriteArtists);
$myPackage->ShowAgeAndArtists();
------------------------------------------------------------------------------------------
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?
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. 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? 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!
Any ideas (please let me know if i havent put explained this well
enough!)
Thanks again for any help!
Now go easy on the perl noob guys *backs into corner*
Ben