469,590 Members | 2,498 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,590 developers. It's quick & easy.

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

47
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 ,,

Expand|Select|Wrap|Line Numbers
  1. sub addWords {
  2.     my $self = shift;
  3.     my $wordsRef = shift;
  4.     my @words = @$wordsRef;
  5.     my $language = shift;
  6.     my $wordId;
  7.     if (!@words || !$language){
  8.         die "addWords invalid arguments.";
  9.     }
  10.     my $languageId = $language->id();
  11.     my $db = DBI->connect("dbi:SQLite:$DB_FILE","","",{RaiseError =>1, AutoCommit => 1, sqlite_unicode => 1}) or die "Unable to connect: $DBI::errstr\n"; 
  12.     foreach my $word (@words){     
  13.         $db->do("INSERT INTO Words (word, languageId) Values ( \"$word\", $languageId);") 
  14.             or print "Failed to add $word to db: $db->errstr()";        
  15.     }
  16.     $db->disconnect or warn "Disconnection failed: $DBI::errstr\n";;
  17. }
  18.  
and I add words as below, it gives me the error,,
Expand|Select|Wrap|Line Numbers
  1. $object = Database->new();
  2. $obj->addWords("Test", 1);
  3.  
Apr 21 '11 #1
1 4629
miller
1,089 Expert 1GB
You're passing 'Test' to your method

Expand|Select|Wrap|Line Numbers
  1. $obj->addWords("Test", 1);
  2.  
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.

Expand|Select|Wrap|Line Numbers
  1. my $wordsRef = shift;
  2. my @words = @$wordsRef;
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1. my @words = ref $wordsRef ? @$wordsRef : $wordsRef;
  2.  
- Miller
Apr 21 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by L. Chen | last post: by
10 posts views Thread by universalbitmapper | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.