problems with croak

  • Thread starter Michael TEpperis
  • Start date
M

Michael TEpperis

I call a function of an modul inside an eval-statement and catch $@
after returning.

_croak_ works in the cases marked as ONE and THREE. if
I pass a wrong file name, I suspect an error in case TWO too,
but I fail. ($class->{'error_out'} is set to 1)


this is the function:
....--------------------------------------------
sub parse_doc
{
my $class = shift;
ONE my $file_name = shift || croak "Usage: parse_doc(file_name,
[\%attr])";
my $attr = shift; # %attr (headings, sub_char)

eval {open FILE_CSV, "$file_name";};
$csvxml_error = "Couldn't open file: $file_name. $@" if $@;
TWO croak "$csvxml_error" if ($class->{'error_out'} == 1 && $@);
my @col_headings;
$attr->{headings} = 0 unless (exists($attr->{headings}));
if ($attr->{headings} == 0) ### No headings to be used from file
{
if ($class->{column_headings})
{
@col_headings = @{$class->{column_headings}};
}
}
my $line;
if ($attr->{headings} != 0)
{
$line = <FILE_CSV>;
my $cols_returned = $get_header->($line, \@col_headings,
defined($attr->{sub_char})? $attr->{sub_char}:undef );
$csvxml_error = "There were no columns returned for headers, please
check your CSV file" if (!$cols_returned);
THREE croak "$csvxml_error" if ($class->{'error_out'} == 1);
return 0 if (!$cols_returned);
}
return 1;
}
....--------------------------------------------

any ideas?
tia
michael
 
A

Anno Siegel

Michael TEpperis said:
I call a function of an modul inside an eval-statement and catch $@
after returning.

_croak_ works in the cases marked as ONE and THREE. if
I pass a wrong file name, I suspect an error in case TWO too,
but I fail. ($class->{'error_out'} is set to 1)

open() doesn't die when the file can't be opened, it just returns undef.
So wrapping "open" in eval, like you do, is useless. Instead, catch
the error yourself:

open FILE_CSV, "$file_name" or
croak "Couldn't open file: $file_name: $!";
this is the function:
...--------------------------------------------
sub parse_doc
{
my $class = shift;
ONE my $file_name = shift || croak "Usage: parse_doc(file_name,
[\%attr])";
my $attr = shift; # %attr (headings, sub_char)

eval {open FILE_CSV, "$file_name";};
$csvxml_error = "Couldn't open file: $file_name. $@" if $@;
TWO croak "$csvxml_error" if ($class->{'error_out'} == 1 && $@);

[rest snipped]

Anno
 
J

John Bokma

Michael said:
I call a function of an modul inside an eval-statement and catch $@
after returning.

_croak_ works in the cases marked as ONE and THREE. if
I pass a wrong file name, I suspect an error in case TWO too,
but I fail. ($class->{'error_out'} is set to 1)
TWO croak "$csvxml_error" if ($class->{'error_out'} == 1 && $@);

No need for the "" $csvxml_error, and no need for the () after the if.

You understand && ? Could it be that $class->{'error_out'} == 1 but $@
is false? (or vice versa)

try:
croak "error_out" if $class->{'error_out'} == 1;
croak $@ if $@;

Probably tells you more.

if ($attr->{headings} == 0) ### No headings to be used from file
{
if ($class->{column_headings})
{
@col_headings = @{$class->{column_headings}};
}
}

No need for two ifs, you can use and or &&


my $line;
if ($attr->{headings} != 0)

move the $line inside the if, of what I prefer:

$attr->{headings} == 0 and return 1;

You can drop the if then.
{
$line = <FILE_CSV>;
my $cols_returned = $get_header->($line, \@col_headings,
defined($attr->{sub_char})? $attr->{sub_char}:undef );
$csvxml_error = "There were no columns returned for headers, please
check your CSV file" if (!$cols_returned);
THREE croak "$csvxml_error" if ($class->{'error_out'} == 1);
return 0 if (!$cols_returned);
}
return 1;

and make that something like (if you drop if)

return $cols_returned ? 1 : 0;

Not tested, etc.
 
J

John Bokma

Anno said:
open() doesn't die when the file can't be opened, it just returns undef.
So wrapping "open" in eval, like you do, is useless. Instead, catch
the error yourself:

open FILE_CSV, "$file_name" or
croak "Couldn't open file: $file_name: $!";

Missed that one, I jumped straight to #2 :-D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top