in reply to Re^4: Perl MIME
in thread Perl MIME - Open file

Just replying to the CSV part. Why do you declare a csv parser object and never use it?

And how is the data separated? The regex shows a pipe or a space. I know of pipe-separated rows that can be named csv, but space OR pipe? Let me assume you meant pipes, where spaces are allowed around the pipes ...

use MIME::Parser; my $parser = new MIME::Parser; my $output = "/tmp/mimex"; $parser->output_dir ($output); my $entity = $parser->read (\*STDIN); open my $fh, "<", $csv_filename; # That line was missing a lot my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char => " +|", allow_whitespace => 1 }); my $processed = 0; #print "test4\n"; while (my $row = $csv->getline ($fh) { @$row or next; # skip empty lines if ($indata == 0) { @data = @$row; if ($row->[0] eq "Subject:") {

Would that be a start?


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^6: Perl MIME
by Pan20 (Novice) on Mar 22, 2012 at 12:11 UTC

    Hi Tux,thanks so much for your help, much appreciated. The data is separated by comma, the data is extracted after it goes through the while loop so am getting each column and row etc... The file is parsed so am getting a txt file in the mimex directory, obviously I cant refer to the file directly cause everytime i am running the code a different file is generated... how am gonna get the tx file..(which is text/plain format/csv..

    open my $fh, "<", $csv_filename;
    is the $csv_filename; will open that txt file stored in mimex directory? How can i open that txt file with all of my data so it gets through the while loop? Thanks a lot for your help.

      If the data is separated by comma's, your own example is horribly wrong, as it splits the data by space or pipe.

      Text::CSV (or Text::CSV_XS) don't need an actual file. They operate on "stream"s just as well. If your data is something that is looking like a filehandle, just pass it to getline () and it should work fine ...

      # make sure $fh is the file-handle to the CSV stream # with the CSV data in memory (say it is in $data), you can # use perlio: # open my $fh, "<", \$data; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); while (my $row = $csv->getline ($fh) { @$row or next; # skip empty lines if ($indata == 0) { @data = @$row; if ($row->[0] eq "Subject:") {

      Enjoy, Have FUN! H.Merijn

        Hi Tux, once last question, the /data to open the text file, in my case is /mimex or is the \*STDIN ? just putting it all together , see below.... looks ok to you? Thanks so much!!!

        #! /usr/bin/perl open FILE,">>/tmp/SRP_RMS.xlog"; print FILE "\nInitialising\n"; my $dbh = DBI->connect($dsn,$user,$pass) or die "Can't Connect to the DB: $DBI::errstr\n"; $indata=0; use MIME::Parser; $parser = new MIME::Parser; $output = "/tmp/mimex"; $parser->output_dir($output); my $entity = $parser->read(\*STDIN); open my $fh, "<", \$data; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); while (my $row = $csv->getline ($fh) { @$row or next; # skip empty lines if ($indata == 0) { @data = @$row; if ($row->[0] eq "Subject:") {