in reply to Half-serious quest for prefix anonymous refs taking
I see a different problem with dopen
my $x = dopen( 'file-not-found' ); __END__ Can't open `file-not-found': No such file or directory readline() on closed filehandle $fh
Ouch. How about this instead:
my $x = dopen( '/dev/null', 'file-not-found', '/etc/passwd' ); sub dopen { my @t = map { my $fh; open $fh, '<', $_ and <$fh> or warn "Can't open `$_': $!\n"; } @_; \@t; } __END__ Can't open `/dev/null': Can't open `file-not-found': No such file or directory
That has a different problem. It reports an error just because the file happens to be empty.
How about this, then:
my $x = dopen( '/dev/null', 'file-not-found', '/etc/passwd' ); sub dopen { my $out = []; FILE: foreach my $file ( @_ ) { my $fh; if ( ! open $fh, '<', $file ) { warn "Can't read '$file': $!\n"; next FILE; } push @{$out}, <$fh>; } return $out; } __END__ Can't read 'file-not-found': No such file or directory
Looks good! It's readable, it complains at the right times, and it doesn't die when I meant it to warn.
I know this does not answer the question you asked. You seem to be looking for a way to do "sub dopen { [ map { open my $f, '<', $_ or warn "Can't open `$_': $!\n"; <$f> } @_ ] }" without the oh-so-confusing [] hanging around at the outskirts. My point is, that's the least of your problems. Write it so it works and so that someone can understand it.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Half-serious quest for prefix anonymous refs taking
by blazar (Canon) on May 29, 2008 at 17:42 UTC |