tags:

views:

358

answers:

3

I have this Perl subroutine that is causing a problem:

sub new
{
    my $class = shift;

    my $ldap_obj = Net::LDAP->new( 'test.company.com' ) or die "$@";

    my $self = {
                _ldap = $ldap_obj,
                _dn ='dc=users,dc=ldap,dc=company,dc=com',
                _dn_login = 'dc=login,dc=ldap,dc=company,dc=com',
                _description ='company',
                };

    # Print all the values just for clarification.
    bless $self, $class;

    return $self;
}

what is wrong on this code :

i got this error Can't modify constant item in scalar assignment at Core.pm line 12, near "$ldap_obj,"

+6  A: 

I think your code should be;

my $self = {
                _ldap => $ldap_obj,
                _dn =>'dc=users,dc=ldap,dc=company,dc=com',
                _dn_login => 'dc=login,dc=ldap,dc=company,dc=com',
                _description =>'company',
};

Change it and try with $perl -c module.pm

Space
+7  A: 

Since you are not using the "fat comma" ( => ), your underscore names on the left side aren't getting auto-quoted and thus, without sigils ('$', '@', or '%'), they can only be subroutine names. Since you haven't declared these subs, perl is taking them for constants, and telling you that you can't assign to constants (or sub invocations).

The proper fix is to change your assignments into double arrows

_ldap => $ldap_obj, #...

Also, Brad's post reminds me, even if they were auto-quoted, you couldn't assign a lexical to a literal string, even if perl interpreted it as an auto-quoted string.

Axeman
+1  A: 

A Hash is just a list of key, value pairs. There is a syntax construct to help differentiate the keys from the values. It is known as a "fat arrow" =>. This construct forces the left hand argument into a string, and then gets transformed into a simple comma.


This is what you meant to write:

perl -MO=Deparse -e'$s = { a => 1 }'
$s = {'a', 1};
-e syntax OK

This is what you actually wrote:

perl -MO=Deparse -e'$s = { a = 1 }'
Can't modify constant item in scalar assignment at -e line 1, near "1 }"
-e had compilation errors.
$s = {'a' = 1};

This is why it I would recommend you always start out a Perl program with warnings enabled.

perl -w -MO=Deparse -e'$s = { a = 1 }'
Unquoted string "a" may clash with future reserved word at -e line 1.
Can't modify constant item in scalar assignment at -e line 1, near "1 }"
-e had compilation errors.
BEGIN { $^W = 1; }
$s = {'a' = 1};
perl -w -MO=Deparse -e'$s = { a => 1 }'
Name "main::s" used only once: possible typo at -e line 1.
BEGIN { $^W = 1; }
my $s = {'a', 1};
-e syntax OK

This last example shows why you should also use strict.

perl -w -Mstrict -MO=Deparse -e'$s = { a => 1 }'
Global symbol "$s" requires explicit package name at -e line 1.
-e had compilation errors.
BEGIN { $^W = 1; }
use strict 'refs';
${'s'} = {'a', 1};

I should have declared $s before trying to use it:

perl -w -Mstrict -MO=Deparse -e'my $s = { a => 1 }'
BEGIN { $^W = 1; }
use strict 'refs';
my $s = {'a', 1};
-e syntax OK

This is why I always start out my Perl programs with:

use strict;
use warnings;
Brad Gilbert