Re: Detecting line terminators in a CSV file
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Fri, 30 Jun 2006 02:13:40 -0700
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 fieldnamesYou don't *have* to die if open doesn't work! And besides, using the high
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;
}
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
.
- References:
- Detecting line terminators in a CSV file
- From: Daniel Kasak
- Re: Detecting line terminators in a CSV file
- From: John W. Krahn
- Re: Detecting line terminators in a CSV file
- From: Daniel Kasak
- Detecting line terminators in a CSV file
- Prev by Date: Re: Variable inside regular expression
- Next by Date: Perl Binary to Perl Code
- Previous by thread: Re: Detecting line terminators in a CSV file
- Next by thread: Variable inside regular expression
- Index(es):
Relevant Pages
|