For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > November 2005 > hash on private member variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author hash on private member variable
sam

2005-11-27, 7:56 am

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. Sinan Unur

2005-11-27, 7:56 am

sam <samwun@telpacific.com.au> wrote in news:4389ae12$1
@news.rivernet.com.au:

> 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
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html

bsder

2005-11-27, 6:59 pm


A. Sinan Unur wrote:
> sam <samwun@telpacific.com.au> wrote in news:4389ae12$1
> @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

> 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.
>
>
>
>
> 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;
> }
>
>
>
>
> Why are you using prototypes with method calls?
>
>
>
>
> What do you think the line above does?
>
>
>
>
> 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?
>
>
>
>
> ????
>
>
> Sinan

A. Sinan Unur

2005-11-27, 6:59 pm

bsder <snort_sam@yahoo.com> wrote in news:438a2e33@news.rivernet.com.au:

>
> A. Sinan Unur wrote:
> 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?


....
[color=darkred]

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
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html

robic0

2005-11-27, 6:59 pm

On Mon, 28 Nov 2005 09:07:45 +1100, bsder <snort_sam@yahoo.com> wrote:

>
>A. Sinan Unur wrote:
>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.
[color=darkred]
>but this give me error.
>
>What is the correct syntax for that?
>
>Thanks
>Sam
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com