hash on private member variable

Discussion in 'Perl Misc' started by sam, Nov 27, 2005.

  1. sam

    sam Guest

    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
     
    sam, Nov 27, 2005
    #1
    1. Advertising

  2. sam <> 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 27, 2005
    #2
    1. Advertising

  3. sam

    bsder Guest

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

    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.
    >
    >
    >>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
     
    bsder, Nov 27, 2005
    #3
  4. bsder <> wrote in news::

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

    > 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?


    ....

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


    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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 27, 2005
    #4
  5. sam

    robic0 Guest

    On Mon, 28 Nov 2005 09:07:45 +1100, bsder <> wrote:

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

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

    >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.
    >>
    >>
    >>>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
     
    robic0, Nov 28, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peng Yu
    Replies:
    3
    Views:
    1,116
    Simon Forman
    Sep 21, 2009
  2. rp
    Replies:
    1
    Views:
    586
    red floyd
    Nov 10, 2011
  3. ittium
    Replies:
    5
    Views:
    462
    88888 Dihedral
    Jan 12, 2012
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    672
    David A. Black
    Jul 2, 2008
  5. A. Farber
    Replies:
    5
    Views:
    199
    A. Farber
    Aug 23, 2008
Loading...

Share This Page