my @files = glob "$ENV{'DOCUMENT_ROOT'}/data/text/*.txt"; my @filenames = map {/([^\/]+)\.txt$/} @files; my @dbfiles = $db->do( 'SELECT title, body FROM articles' ); my $replace = $db->prepare( 'INSERT INTO article (title, body) VALUES (?, ?)' ); foreach $filename (@filenames) { #Check every filename(item in @menu_files) as well as its content to see if is already inserted in table articles under columns title and body #============ INSERT NEW FILENAMES & CONTENTS INTO DATABASE IF ANY ============= #If filename and content dont already exist in the db then do insert them unless( grep /^\Q$filename\E$/, @dbfiles ) { open FILE, "<$filename" or die $!; @data = ; close FILE; unless( grep /^\Q$data\E$/, @dbfiles ) { $db->do('INSERT INTO articles (title, body) VALUES (?, ?)', undef, $filename, $data) } } }