Hi Graeme (I repost this email too to the list, this way somebody can correct me)
> Re: returning a hash from a method call ? > Datum: Heute 21.08:32 > Von: "Graeme McLaren" <[EMAIL PROTECTED]> > An: [EMAIL PROTECTED] > > Hey Joe, thank you for your reply, what you said all makes sense and I like > the idea of setting all of the conf data during object creation. > > Could I simply put "$self->{conf}={table=>'blog',...};" in the constructor > before it is returned then use your sub? ...to access $self->{conf}, yes (another question is the interface of such a class) > sub get_users_table_and_columns{ > return $_[0]->{conf}; > } > > > As nothing is being passed to the method "get_users_table_and_columns" would > the equivalent of $_[0] be exactly the same as my $self=shift; ? Not exactly; $_[0] is the first element of @_ (the arguments passed to the method) without retrieving it from @_. This same first element is contained in $self after "$self=shift", shift retrieving the first element from @_. See perldoc -f shift . When calling a method of an object, like $obj->get_users_table_and_columns; $obj is passed implicitly to the method as first argument. Above call is similar to get_users_table_and_columns($obj). > I think that way would be a good way of doing it as it provides some > indirection through an accessor/mutator function. Yes, best access/change object attributes only by methods, not directly. > The way I was thinking of doing it I wanted to have a sub that would return > a hash which I could then pass to another function to make use of the data. > Why would I only have one key of the hash printed out when I do: > > my $conf=configuration->new; > die Dumper($conf->get_users_table_and_columns); Don't know... > Thanks again for your reply and advice you can give me would be great. > Here's the full module. I'll try to comment this module in detail as it is, and provide a possible alternative. > Cheers, > > Graeme :) > > ################## start of module ######################### > #configuration.pm > package configuration; > use strict; use warnings; # additionaly > sub new{ > my $class=shift; > my $self={}; > bless($self, $class); > return $self->conf; A new() class method ("class method" because not an object, but a class name is passed als first argument) should return the created object, $self, not the configuration values: return $self; # instead of return $self->conf; (You would call $self->conf after having an object: my $obj=configuration->new(); # create object my %conf=$obj->conf; # call object method ) The two different %conf contents from below can be put into the object as object attributes: sub new my $class=shift; # more params could be provided, e.g. for initializing the attributes my $self={ host => "host", db_name=>"db_name", username=>"username", tab_cols_config=>{ table=>'blog', column1 =>'first_name', }, # etc. }; return bless $self, $class; } > } > > sub conf{ > my $self=shift; > my %conf=( > host => "host", > db_name=>"db_name", > username=>"username", > password=>"password", > > ## templates > registration_form => "/var/www/templates/registration.tmpl" > ); > > foreach my $k ( keys %conf ) { > $self->{$k} = $conf{$k}; > } > return $self; Here, you should return the configuration values, not the object. However, the question is if you don't want to provide the methods sub host { $_[0]->{host} }; sub dbname # etc. sub username sub password sub registration_form # and, see below, sub tab_cols_config instead of conf() and the following get_key(). > > } > > sub get_key{ > my $self = shift; > my $key = shift; > return $self->{$key}; > } This could be an alternative to the proposed 6 subs, but does, as implemented, not add much additional abstraction over the direct read access of the object attributes. At least, get_key() could test if a certain attribute exists and can be accessed: return exists ($self->{$key}) ? $self->{$key} : undef; > sub get_users_table_and_columns{ > my $self=shift; > > my %conf=( > table=>'blog', > column1 =>'first_name', > column2 => 'last_name', > column3 => 'email', > column4 => 'password', > column5 => 'active' > ); > > > return \%conf; > > } # returns hashref sub get_users_table_and_columns{ $_[0]->{tab_cols_config}; } There are other possibilities like a separate class for the tab_cols_config data if there are many table configs... > 1; Maybe the class is a bit overkill, who knows :-) > > ################## End of Module ########################## > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>