0

I'm working my way through Higher Order Perl, and want to try to execute some of its code, in this case FlatDB.pm.

I tried to simulate the calling method outlined in the answers to this question (2621225), but it's not working for me. To wit:

## HOP Chapter 4 section 3.4, p.140
my $FIELDSEP = qr/:/; 

package FlatDB; 

sub new {
  my $class = shift;
  my $file = shift;
  open my $fh, "< $file" or return;
  chomp(my $schema = <$fh>);
  my @field = split $FIELDSEP, $schema;
  my %fieldnum = map { uc $field[$_] => $_ } (0..$#field);
  print "\nfieldnum=",%fieldnum;
  bless { FH => $fh, FIELDS => \@field, FIELDNUM => \%fieldnum,
      FIELDSEP => $FIELDSEP }   => $class;
}
# More subs here - snipped

What I added to try to run the package:

package main;
print "\nat 89";
$obj= FlatDB->new("FlatDB","SampleDB.txt"); 
print "\nat 91"; 

The prints at 89 & 91 are executed, but the print in the 'new' subroutine is not. The 'new' subroutine works if I pull it out of the package, so the problem must be in how I'm trying to call it.

I'm afraid it's something very simple, but I don't see it.

Community
  • 1
  • 1
user1067305
  • 3,233
  • 7
  • 24
  • 29

1 Answers1

1

The only way that method can exit without executing the print statement is through the line

open my $fh, "< $file" or return;

So I imagine the open is failing for some reason. Replace that line with

open my $fh, '<', $file or die qq{Unable to open "$file" for input: $!};

and you will see the reason for the failure

Borodin
  • 126,100
  • 9
  • 70
  • 144