Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@draegtun
Created July 23, 2010 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draegtun/487366 to your computer and use it in GitHub Desktop.
Save draegtun/487366 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use 5.012;
use warnings;
use autodie;
# quick & crude example for comment in:
# http://stackoverflow.com/questions/3308149/perl-function-knowing-receiver-type/3308591#3308591
{
package MyFile;
sub read {
my ($class, $filename) = @_;
bless {
filename => $filename,
fh => do { open my $fh, '<', $filename; $fh },
}, $class;
}
sub as_list {
my $self = shift;
return (readline $self->{fh});
}
sub as_hash {
my $self = shift;
# arbitary hash info follows
my $count;
return map { chomp; ++$count => $_ } readline $self->{fh};
}
}
my @x = MyFile->read('some_file.txt')->as_lines;
my %y = MyFile->read('another_file.txt')->as_hash;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment