Brad Fitzpatrick (brad) wrote,
Brad Fitzpatrick

subclassing IO::Handle

Subclassing IO::Handle is.... interesting. An IO::Handle object is just a blessed typeglob (a generated symbol), and not a hashref or pseudo-hash like other perl objects, so there's no good place to put attributes for classes/subclasses. Well, except since you have the whole symbol table entry, you can just use the hashref hanging off that symbol, since it was auto-generated.

The end result: (in progress)
use strict;
use MogileFS;
use DBI;
use DBD::mysql;

sub get_dbh { return DBI->connect("DBI:mysql:mogilefs", "mogile", "");  }

# create a MogileFS handle for domain "test"
my $mg = MogileFS->new(
                       domain          => 'test',
                       get_dbh         => \&get_dbh,
                       cache_memcached => undef,

# tells the framework we want to put a new file in the system.
# new_file returns an IO::File object (IO::Handle) that's subclassed
# by MogileFS and ready for writing.
my $fh = $mg->new_file("whitakers_mom");
print $fh "Worddddd\n";

# and when you close it, it sets the flags in the database that makes it
# start replicating everywhere:

Implementing close:
sub close {
    my $self = shift;  # is a typeglob

    # actually close the file

    # get a reference to the hash part of the $fh typeglob ref
    my $href = \%{*$self};

    # ... set DB flags based on device/fileid in above $href
Tags: mysql, perl, tech

  • Happy Birthday!

    Happy 20th Birthday, LiveJournal! 🐐🎂🎉

  • hi

    Posting from the iPhone app. Maybe I'm unblocked now.

  • Why, hello...

    Long time no see. How's my baby doing?

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.