in reply to evolving an OO solution for a bitstream
Frankly, Perl sucks static-variable-wise. They're not directly supported, and must be hacked-over with my() in a BEGIN block. It didn't feel right.
Bah. Heresy. Statics in modules done need a BEGIN block, they dont even need an enclosing scope unless you are being pedantic. And they can be shared amongst any arbitrary set of procedures. So Id say they are pretty powerful actually.
Incidentally you wrote this:
my $stream = BitStream::new("bigbinfile");
Thats not a method call. Thats a procedure call.
Anyway, I whipped this together before I realized that you had already gone down this road. (Really must read nodes a bit more thoroughly before replying :-)
package File::Bitstream; use strict; use warnings; sub new { my ($class,$file)=@_; open my $fh,"<",$file or die "Cant read '$file':$!"; binmode $fh; return bless { file=>$file, fh=>$fh, buffer=>'', chunk=>1024 },$cl +ass; } sub get_bits { my ($self,$bits)=@_; while (!eof($self->{fh}) and length($self->{buffer})<$bits) { my $chars=''; read($self->{fh},$chars,$self->{chunk}); $self->{buffer}.=unpack "B*",$chars; } return length($self->{buffer}) ? substr($self->{buffer},0,$bits,'' +) : undef; } 1; my $o=File::Bitstream->new($0); my $bits=''; print $bits,$/ while defined($bits=$o->get_bits(13));
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: evolving an OO solution for a bitstream
by spurperl (Priest) on Oct 21, 2003 at 17:39 UTC | |
by dragonchild (Archbishop) on Oct 21, 2003 at 17:51 UTC | |
by demerphq (Chancellor) on Oct 21, 2003 at 18:00 UTC | |
by diotalevi (Canon) on Oct 21, 2003 at 17:49 UTC |