Brad Fitzpatrick (brad) wrote,
Brad Fitzpatrick
brad

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)
#!/usr/bin/perl
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:
$fh->close;

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

    # actually close the file
    $self->SUPER::close;

    # 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
}
GO PERL! :P
Tags: mysql, perl, tech
Subscribe

  • Ukraine

    Nobody reads my LiveJournal anymore, but thank you to everybody in Russia protesting Putin's insane war against Ukraine. (I know it's risky…

  • Happy Birthday!

    Happy 20th Birthday, LiveJournal! 🐐🎂🎉

  • hi

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

  • Post a new comment

    Error

    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.
  • 7 comments