hash on private member variable

S

sam

Hi,

I need to use hash on a private member variable.
But I don't know how to make it happen. Here is the code:

#!/usr/bin/perl

package dbm_lib;

use strict;
use Time::Local;
use Data::Dumper;

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

sub gen_name()
{
my $self = shift;
localtime(time);
return rand(time).".pl";
}

sub create()
{
my $self = shift;
my ($name) = "/usr/local/code.dbm";
dbmopen($self->{%_map},$name,0666);
$file_name = $self->gen_name();
$status = "0"; # 0 - not yet execute; 1 - had been executed.
$val = join("\t",$file_name, ,$status);
$map{$file_name} = $val;
}

sub cclose()
{
my $self = shift;
dbmclose($self->{_map});
}

1;


Thanks
Sam
 
A

A. Sinan Unur

I need to use hash on a private member variable.
But I don't know how to make it happen. Here is the code:

1. What does it mean to "use hash on a private member variable"?

2. The code you posted does not compile.

Please read the posting guidelines for this group to learn how you can
help yourself, and help others help you.
sub new {
my $class = shift;
my $self = { _map => undef };
bless ($self, $class);
return $self;
}

Are you saying you want $self->{_map} to be a reference to an anonymous
hash? Then, make it so:

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

Why are you using prototypes with method calls?
{
my $self = shift;
localtime(time);

What do you think the line above does?
return rand(time).".pl";
}

If gen_name is invoked more than once in a second, this will return
identical filenames ... I have a feeling that will not be good for your
program. What are you trying to do?
sub create()
{
my $self = shift;
my ($name) = "/usr/local/code.dbm";
dbmopen($self->{%_map},$name,0666);
$file_name = $self->gen_name();
$status = "0"; # 0 - not yet execute; 1 - had been executed.
$val = join("\t",$file_name, ,$status);
$map{$file_name} = $val;

????


Sinan
 
B

bsder

A. Sinan Unur said:
@news.rivernet.com.au:




1. What does it mean to "use hash on a private member variable"?
Since this is OO style perl, its memeber variable is _map.
I supposed use %_map thruout the entire perl code.
eg.
dbmopen($self->{%_map},$name,0666);

but this give me error.

What is the correct syntax for that?

Thanks
Sam
 
A

A. Sinan Unur

bsder said:
Since this is OO style perl, its memeber variable is _map.
I supposed use %_map thruout the entire perl code.
eg.
dbmopen($self->{%_map},$name,0666);

but this give me error.

What is the correct syntax for that?
....

Well, did you try reading the code?

$self is a reference to an anonymous hash. _map is a key in that hash.
$self->{_map} is the value, which happens to be another reference to
another anonymous hash. You want to dereference that reference:

%{ $self->{_map} }

It would be a good idea to read perldoc perlreftut.

Sinan
 
R

robic0

Since this is OO style perl, its memeber variable is _map.
I supposed use %_map thruout the entire perl code.
eg.
dbmopen($self->{%_map},$name,0666);
Yeah, it looks like you want "_map" to be the name of a hash
or reference to a hash or anonymous hash. Hard to tell.
Its not good to try to do everything in the constructor,
and in actuality things are added outsied of it.

'_map' => {}; ???

It could always be populated later ..
$self->{'_map'}->{'key1'} = $val;

There lots of ways, but you don't need to reserve the name
"_map" with undef. Do it when you do it. Somewhere along the line
it has to be initialized and populated. If you are going to pass
it to another module, it should be pre-typed and allocated as
above.
The _map var is just holding a reference, it could be to a
anonymous one or a globule private module one thats fixed/named.

%hmodule = (); #module global
$self = {'_map' => \%hmodule};

Either way the syntax is the same for passing it around.
And also it can be stored outside of the $self class this way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top