Re: Detecting line terminators in a CSV file



Daniel Kasak wrote:
John W. Krahn wrote:

Are you sure that you want to "fix" this?

Certain. I need to find the correct line terminator, and then tell MySQL
what it is.

perldoc -f binmode

binmode is what I was after - thanks :)

Then don't forget to use the correct characters for this: "\015" for Carriage
Return and "\012" for Line Feed; as using "\n" may get translated to something
other than just "\012".


# Parse the 1st line of the import file and extract fieldnames
eval{
open SOURCE, $options->{source}
|| die "Failed to open file $options->{source}.\nIs the file
already open?";
};

if ( $@ ) {
Gtk2::Ex::Dialogs::ErrorMsg->new_and_run(
title => "Error opening file!",
text => $@
);
return FALSE;
}

You don't *have* to die if open doesn't work! And besides, using the high
precedence || operator means it won't die even if you wanted it to.

open SOURCE, '<', $options->{source} or do {
Gtk2::Ex::Dialogs::ErrorMsg->new_and_run(
title => 'Error opening file!',
text => $!
);
return FALSE;
};

It's working on my system as-is. I'll check out that precedence thing
when I get some spare time.

That depends on what your definition of "working" is. Precedence means that:

open SOURCE, $options->{source} || die "message ...";

will only die if $options->{source} contains the string '' or '0' or the
number 0 or the value 'undef'. You need to either add parentheses:

open( SOURCE, $options->{source} ) || die "message ...";

or use the low precedence 'or' operator:

open SOURCE, $options->{source} or die "message ...";


You should also include the $! or the $^E variable in the error message so you
will know *why* the file could not be opened.

The point I should have made originally and that I will make now is that you
shouldn't use eval() to print an error message from a built-in function. When
open() fails it returns undef and puts the error message in $!. eval() will
not catch this or the error message. eval() will only catch the die() and its
message but since it won't die when open fails... But since you don't
actually want the program to die, why use die in the first place?


if ( substr( $fieldnames, length( $fieldnames ) -2, 2 ) eq "\r\n" ) {

You don't have to call the length() function, you can just use a negative number:

if ( substr( $fieldnames, -2, 2 ) eq "\r\n" ) {

Now *that* is useful. Thanks :)

And since the offset -2 can only be two characters long you can omit the
length argument as well:

if ( substr( $fieldnames, -2 ) eq "\r\n" ) {




John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: Problems connecting to internet via GPRS
    ... The error message is basically saying that it can't find System.SR.dll so there's no error message. ... da die optionale Ressource der Assembly, ... If the application forces the connection itself (for example via web ... Es kann keine Fehlermeldung angezeigt werden, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Embedded Functions in MATLAB with MPC555
    ... the Model Explorer and specify everything about the signal type on the input ... this would either work or give you a different error message. ... % Diese Variablen hälten Eigenschaften die Funktionen ... if (isempty(byte_identifier)) ...
    (comp.soft-sys.matlab)
  • Re: finding compile time errors
    ... It looks a *lot* like the Perl DBI module which might be the ... Is there a die in PHP with an error message? ... Of course there's a way to kill a script with dieand an error ...
    (comp.lang.php)
  • How to catch runtime error?
    ... error message for every execution of the sub ... die} ... the program runs w/o any problem for reasonable sizes. ...
    (comp.lang.perl.misc)
  • Re: Is DataSet belong another namespace
    ... You must have written some class called "DataSet" that is taking precedence. ... > But it result in an error in run time, the error message is ... > The namespaces I used are ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)