Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Tying with a class method

by ninja-joe (Monk)
on May 02, 2002 at 15:04 UTC ( [id://163556]=perlquestion: print w/replies, xml ) Need Help??

ninja-joe has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I have a class, and I want to have a persistent tie to a dbm file. I'm using strict also. I want to have two subs, a connect and disconnect. If I were to do
sub connect { my $self = shift; tie %data, "AnyDBM_File", "data", O_RDWR, 0644 or die "Cannot open data for read.\n"; return 1; } sub disconnect { untie %data; }
Would it work the way I'd hope it would? Or would the tie to data be lost because of the scope of the sub? Could I then do something like
sub data { my $self = shift; my $bar = shift; if(@_) { $data{$bar} = $self->{FOO}; } }
And expect it to behave correctly? Perhaps it would be better to tie the dbm file to a class member, but I don't know how one would do that.

Replies are listed 'Best First'.
Re: Tying with a class method
by broquaint (Abbot) on May 02, 2002 at 15:24 UTC
    If %data is visible to both connect() and disconnect() then it should work as expected (e.g if %data is a pacakge variable). However if you'd still like to use lexical variables and have it visible to both functions you could use closure behaviour
    { my %data; sub connect { my $self = shift; tie %data, "AnyDBM_File", "data", O_RDWR, 0644 or die "Cannot open data for read.\n"; return 1; } sub disconnect { untie %data; } } # %data has fallen out of scope and only exists # within connect() and disconnect()
    So %data would now be within the scope of both the subs, and nothing else. Unfortunately this would trip up data(), but you could also include that within the block that connect() and disconnect() are in. For more info on closures check out Why Closures? and other related notes.
    HTH

    _________
    broquaint

      I was under the impression that "use strict" would disallow me from using package level variables. I'm probably wrong though. Suppose if I wanted to make a hash a member of that class, regardless of whether strict minded or not, would it just work like: $self->hash{"foo"} = "bar"; and %self->hash ? Where I'd be able to do a statement such as
      tie %self->hash, "AnyDBM_File", "data", O_RDWR, 0644;
        I was under the impression that "use strict" would disallow me from using package level variables
        The use of strict merely discourages the use of package variables. As long as a variable exists within the package's symbol table you're free to use it without it being fully qualifying.

        If you had a hash as a data member of the blessed object (which itself was a hash) then you would use it like so

        # assign to hash $self->{hash}->{foo} = 'bar'; # get keys from hash keys %{$self->{hash}}; # tie hash tie %{$self->{hash}}, "AnyDBM_File", "data", O_RDWR, 0644;
        If you're a little confused about how how classes and objects work I'd recommend reading Tom Christiansen's oo tutorial and perhaps also read up on references in perl.
        HTH

        _________
        broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://163556]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found