Skip to content

Instantly share code, notes, and snippets.

@xenoterracide
Created May 8, 2012 03:41
Show Gist options
  • Select an option

  • Save xenoterracide/2632369 to your computer and use it in GitHub Desktop.

Select an option

Save xenoterracide/2632369 to your computer and use it in GitHub Desktop.
class
class MyClass {
method foo { return $self->{foo} }
method info { return load_class('Class::Info')->new }
}
MyClass->new->foo;
# to be the equivalent of
{
use strict;
use warnings;
use utf8; # so our class can be named with utf8
package MyClass {
use namespace::autoclean;
use Scalar::Util qw( blessed );
# use Class::Load qw( load_class );
# or similar for a feature that I'm hoping will be in Class::Load in the future
# for now I'll show with require
sub new { # or something better, point is that there's a default simple constructor
my $class = shift;
my $self = ref $_[0] eq 'HASH' ? $_[0] : { @_ };
bless $self, $class;
return $self;
}
sub foo { my $self = shift; return $self->{foo} }
sub info { #doesn't actually do what I'm really suggesting
my $self = shift;
require Class::Info;
return Class::Info->new;
}
}
}
MyClass->new->foo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment