Hi Graeme

[rearranged from bottom:]
> All I want to do is print off the value of the hash refs at the moment
> before I start building on it.  

Due to my bad english I'm not quite shure what you mean by that.
You want to print the values to be sure they are right, before using them?

> I know I'm not passing anything to the 
> "new" constructor but I'm not sure what I should be passing to it.  
> The 
> name of the class shouldn't be defined outside the class surely?

The name of the class (a package name) is implicitly passed with

 my $obj = My::Class->new(); # Class is named My::Class

This is catched in the new() code by

 my $class=shift;

Similarily, the object is implicitly passed when calling a method:

 my $result=$obj->method();

This is catched in the new() code by

 my $self=shift;


Start with 

 perldoc perl

from cmdline to study some documentation.


Am Sonntag, 24. April 2005 19.01 schrieb Graeme McLaren:
> Hi all I'm trying to write a basic 

I suppose you mean "easy, static class" here, not "base class", for my 
comments.

> class that will contain some 
> configuration details, unfortunately it bombs out and doesn't do anything.
>
> ################### start of class ###########################
> #configuration.pm
> package configuration;
> use strict;

add:

  use warnings;

which may give some help if anything doesn't behave as you expect.

> sub new{
>     my $class=shift;
>     my $self=shift;
>
>     bless($self, $class);
>     return $self;
> }

The sense of a new() method is to create an object ($self), so it is not 
passed to new().

Additionaly, since new() already returns the created object, you dont need a 
methode "get_conf" with the sematic of "get the configuration object".

Instead, you could use get_conf() to get a specific configuration value.

> sub get_conf{
>     my $self=shift;
>     my %conf=(
>               db_name="bla1",
>        username="bla2",
>        password="bla3"
>
>     );
>
> return \%conf;
>
> }
>
> 1;


So, I would rewrite new() and get_conf() as follows:
[untested]

# create configuration object:
#
sub new {
 my $class=shift;
 # create the object to return, the object containing
 # the configuration variables and their values:
 my $self={
  name1=>value2,
  name2=>value2,
 }
 # return the object, consisting of reference to a hash,
 # blessed to your class:
 return bless $self, $class; 
}

# get some configuration value
#
sub get_conf {
 my $self=shift;
 my $var=shift;
 die "unknown config var" 
  unless exists $self->{$var};
 return $self->{$var};
}


> ################ end of class ####################
>
>
> Mt CGI script is this:
>
> ##################### CGI Script #######################
> #!/usr/bin/perl -w
> use strict;
> use configuration;
>
> print "Content-type: text/html\n\n";
>
> my $conf=configuration->new;
> my $conf->get_conf;
>
> print "$conf->{'db_name'}";

According to my code above, the script gets adapted:

 my $conf=configuration->new; # as you wrote
 print $conf->get_conf('name1');

> ###########################################
>
> Anyone have any ideas?


BUT:

If your configuration has not to be generic, but contain only some fixed 
values for your web interface, and you use the configuration only in one 
script, there is no need to use a class!

Simply use a hash.

So, your script could simply be (without a configuration package):

#!/usr/bin/perl -w
use strict;
use warnings;

print "Content-type: text/html\r\n\r\n";

my %conf=(bla=>'blubb', foo=>'bar');

# print the configuration
print join "<br />", map {$_. ": ".$conf{$_}} keys %conf;

# and now use the config values in the script code by 
# accessing hash keys, eg:
# do_something_with ($conf{bla});


hth, joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to