in reply to Tying with a class method

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

Replies are listed 'Best First'.
Re: Re: Tying with a class method
by ninja-joe (Monk) on May 02, 2002 at 16:49 UTC
    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