Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm a long time reader, but first time seeker. I consider myself at perhaps an intermediate level. Anyway, here's my problem:

The relevant portions of code are as follows:

sub new { my $self = {}; $self->{_ldap} = Net::LDAP->new($LDAP_SERVER); bless $self; return $self; }
Which initializes my module...it's purpose is to make administering an ldap-aware mail server easier in my company.

sub alias_add_entry { my $self = shift; my $alias = shift; my $user = shift; my $result; die "Not enough args to alias_add_entry() $@" if not defined ( +$alias and $user); $result = $self{"_ldap"}->modify ( dn => "cn=$alias,ou=Aliases,dc=mydomain,dc=com", add => { rfc822mailmember => $user } ); return $result; }
This is the first of many subs which returns the error: Global symbol "%self" requires explicit package name at MyModule.pm line xxx, where xxx references the $result = line.
The thing is, this sub works fine:
sub authenticate { my $self = shift; my $pw = shift; my $user = shift; die "Not enough args to authenticate() $@" if not defined $pw; $user = "Manager" if ($user eq ""); my $result = $self->{"_ldap"}->bind ( dn => "cn=$user, dc=sigmatel, dc=com", password => $pw ); return $result; }


Any ideas?

Replies are listed 'Best First'.
Re: My first Perl module...
by snax (Hermit) on Nov 17, 2000 at 13:57 UTC
    In the sub that works you use
    $self->{}
    explicitly dereferencing the $self reference. In the sub that doesn't work you use
    $self{}
    which implies that $self is a hash itself and not a reference -- thus making your use strict; complain about an undefined global hash called %self.

    As a side note I'd suggest dropping the double quotes in your hash keys -- they're not necessary and they force interpolatin where none is necessary (even if you needed interpolation you wouldn't need quotes there).

      Hmm, so is defining %self globally the answer? It doesn't seem like it should be to me, but I'll of course defer to your judgement. :)
        No, defining %self is not the answer (and I don't think snax said it was). You should use a valid syntax for references, like $self->{"_ldap"}, to refer to the anonymous hash that $self refers to.

        /brother t0mas
        t0mas is correct -- I don't suggest that you declare %self globally, especially since it really won't do what you want it to do :)

        Check out perlman:perlref for more details on using references, and poking through perlman:perlobj and/or perlman:perltoot is probably a good idea, too.

        $hash{key} refers to the 'key' element of the hash called %hash. $hash->{key} refers to the 'key' element of the hash referred to in the reference $hash. $hash has nothing to do with %hash. Check out perlref and other documentation mentioned by others.
Re: My first Perl module...
by t0mas (Priest) on Nov 17, 2000 at 13:55 UTC
    You'll need to do something like

    $result = $self->{"_ldap"}->modify() (as you do in authenticate with the bind call)

    Refer to perldoc perlref for more goodies aboute refs.

    /brother t0mas