I premise that I always
so that even if I make frequent mistakes, perl rapidly helps me finding them by complaining loudly. In this case however, the program seemed to be fine at all effects except that it silently "didn't work".use strict; use warnings;
Now, this problem I had had to do with a method that properly trimmed down to a bare minimum looked like this:
Now it happened that I made a trivial mistake in that I called it like a sub (from another method) and like all trivial mistakes it didn't appear to be trivial until I spotted it, which took quite a while.sub parse { my ($self,$hashref)=@_; open my $fh, '<:raw', $self->{FILE} or die $!; while (<$fh>) { chomp; $hashref->{$_}++ for split; } }
Conclusion: What happened? "Obviously" that the hashref given as explicit argument was dereferenced and the value corresponding to the key 'FILE' was accessed, which in turn was undef, so that as per perldoc -f open an anonymous (empty) file was opened for reading and the code in the loop was never executed (so that the fatal error it would have triggered for dereferencing an undef value never popped out either!)
Why didn't do the obvious checks (e.g. definedness in the first place) on the filename I was about to open()? Because basically I was already doing them in the method that called the one (corresponding to that) above.
As a side note, I wonder wether perl could emit a warning when one tries to open() an anonymous file for reading (or for writing) only... or are there common situations in which it could be desirable to do so?
Update: I've noticed that the cmt above has been completely misunderstood, hopefully this followup explains better what I really meant.
For a complete but minimal example check this:
Of course the two lines marked with cmts above are very different and it's hard to imagine how one could type mistakingly the latter for the former, but in my real case it was much less so, IMHO.#!/usr/bin/perl -l use strict; use warnings; package Foo; sub new { my ($self,$file)=@_; bless { FILE => $file }, $self; } sub parse { my ($self,$hashref)=@_; open my $fh, '<:raw', $self->{FILE} or die $!; while (<$fh>) { chomp; $hashref->{$_}++ for split; } } package main; my ($file,%hash)=shift; Foo->new($file)->parse(\%hash); # right Foo::parse(%hash); # wrong print for keys %hash; __END__
In reply to I've been bit in the neck by open() by blazar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |