Error Can't use string ("Test") as an ARRAY ref while "strict refs" in use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wiinie
    New Member
    • Feb 2011
    • 47

    Error Can't use string ("Test") as an ARRAY ref while "strict refs" in use

    Hi guys,

    I have an error "Can't use string ("Test") as an ARRAY ref while "strict refs" in use ~~~"

    I am trying to add word into the database, and then it gives me this error. please help ,,

    Code:
    sub addWords {
    	my $self = shift;
    	my $wordsRef = shift;
    	my @words = @$wordsRef;
    	my $language = shift;
    	my $wordId;
    	if (!@words || !$language){
    		die "addWords invalid arguments.";
    	}
    	my $languageId = $language->id();
    	my $db = DBI->connect("dbi:SQLite:$DB_FILE","","",{RaiseError =>1, AutoCommit => 1, sqlite_unicode => 1}) or die "Unable to connect: $DBI::errstr\n"; 
    	foreach my $word (@words){ 	
    		$db->do("INSERT INTO Words (word, languageId) Values ( \"$word\", $languageId);") 
    			or print "Failed to add $word to db: $db->errstr()";		
    	}
    	$db->disconnect or warn "Disconnection failed: $DBI::errstr\n";;
    }
    and I add words as below, it gives me the error,,
    Code:
    $object = Database->new();
    $obj->addWords("Test", 1);
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    You're passing 'Test' to your method

    Code:
    $obj->addWords("Test", 1);
    And then you're pulling it into a parameter which is good, but in the next line you're trying to treat it like an array reference.

    Code:
    my $wordsRef = shift;
    my @words = @$wordsRef;
    That is what your error means.

    Do you mean to pass an array references as your second parameter, or do you only want it to be an optional array like this:

    Code:
    my @words = ref $wordsRef ? @$wordsRef : $wordsRef;
    - Miller

    Comment

    Working...