develooper Front page | perl.perl5.porters | Postings from April 2007

got Smack(ed) lately?

Thread Next
Tom Christiansen
April 17, 2007 19:20
got Smack(ed) lately?
Message ID:
=for your consideration,

The surrounding module,, runs perfectly well, as demonstrated by

    % perl -MSmack -e 'smack && snarf && print "hurray!\n" '

after clipping this message body and placing it in the obviously-named
file: the expected "hurray!" is indeed correctly emitted.  But it is 
a false cheer, for this innocent module nevertheless has a bug or two
lurking in it.

Can you* see the problem?  If so, is it really glaringly obvious to 
everyone but me?  Has awareness of this niggling nasty passed into 
general understanding?

I don't think so, but could of course be wrong.  Yet even if I *am*
mistaken (and so more of you will say "Duh, Tom!" than who will say
"D'oh, Perl!"), what are the poor module writers realistically supposed
to do about this? Must they retroactively insulate themselves from this
strange-action-at-a-distance bug?  This vexing matter may well not
even have existed back when they wrote their innocent module.

Why must module writers understand this?  I really can't see how it's
their fault.  Anything that forces everybody else all to go off and
change their existing module code can't be a good thing.  I would argue
therefore that the fault lies not in these modules, but elsewhere entirely--
pragmatically speaking, that is.




package Smack;
use strict;
use warnings;

use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(smack snarf);

my $DEFNAME = "bindata";
my $NULL = chr(my $bye = 0);
my $RECSEP = "\xff" . $NULL;

sub smack {
    my $file = @_ ? shift : $DEFNAME;
    open(BINDATA, "> :raw", $file) || die "Can't smack > $file: $!";
	local $\ = $RECSEP;
	print BINDATA "line one";
	print BINDATA "line two";
    close(BINDATA) || die "can't close $file: $!";
    printf "%s is size %d (should be 20)\n" , $file, -s $file;
    return 1;

sub snarf {
    my $file = @_ ? shift : $DEFNAME;
    open(BINDATA, "< :raw", $file) || die "Can't snarf < $file: $!";
    local $/ = $RECSEP;
    local $_;
    while (<BINDATA>) {
	printf "line %d of %s: %s\n", $., $file, $_;
    close BINDATA;
    return 1;

# * where you != Audrey 

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About