After some testing i noticed that while(<FILE>) returns a line-by-line data of the file and it is storing it in $_
That's good by what i need is to get to whole data content in one var so the above solution aint gonna help me, of course i could concatenate every line after another to construct the whole data of the file but that would be dumb.
I noticed after trying every weird idea that came into my mind that $data=<FILE> would save the whole file's contents in a split of a sec without the need for looping, so what i did to make the job done was the following:
#=========== INSERTING MULTIPLE FILENAMES & CONTENTS INTO DATABASE === +========== my $data; my $insert = $db->prepare( 'INSERT INTO articles (title, body) VALUES +(?, ?)' ); my @files = glob "$ENV{'DOCUMENT_ROOT'}/data/text/*.txt"; foreach (@files) { open FILE, "$_" or die $!; $_ = map m{([^/]+)\.txt}, $_; #Strip out of the string th path +and extension leaving only the filename intact $data = <FILE>; #Grab the whole file contents at +once $insert->execute($_, $data) or print $db->errstr; close FILE; }
All that in order to achieve opening of all files one-by-one and inserting each of the filename and its corresponding content as an entry in mysql table 'articles'

update:

After running this test script:

foreach (@files) { open FILE, "$_" or die $!; print "$_", br() x3; $_ = map m{([^/]+)\.txt}, $_; #Strip out of the string the path + and extension leaving only the filename intact print "$_", br() x3; $data = <FILE>; #Grab the whole file contents at +once print "$data"; exit; close FILE; }
i noticed 2 errors i cannot solve:

a) The '$_' doesn't hold the value of the current filename opened without paths and extension but instead it has the number of '1' in it. Its a syntax error obviously that i have made and don't know how to properly write it.

b) @data though it is correct because it contains the contents of each file when looping and contents are stored by me in utf8 when i write the text files
But although i'm not striping out the filename correctly, even if i was, i will still need to re-encode the filename to utf8 before inserting into the database because filenames are greek-iso.
i can do that as Encode::from_to($_, 'ISO-8859-7', 'utf-8') but the question is will i have to or the $insert->execute($_, $data); will do that automatic for me before inserting the values in the db?


In reply to Re^6: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE by Nik
in thread Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE by Nik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.