Re: GdkRectangle, again

On Tue, 29 Apr 2003, Ross McFarland wrote:

it's nice to be able to set the members without having to call a
would like to be able to $update_rect->{x} = 5; granted it's just as easy to
say $update_rect->x(5); but that's not what you'd first assume would be the
case. these are c structures that in a c app you directly manipulate. when in
perl that equates to a hash. and that would probably be people's first
assumption to what a GtkRectangle would/should be.

there's nothing keeping you from making it functions that operate on a hash so
that you could choose which way. use it as hash ($rect->{x}) or as a function

Given that this is Perl lets make everyone happy.  The following prints


use strict;

my $o = object->new(x => 0);

print $o->x, "\n";

$o->x = 5;

print $o->x, "\n";

$o->{x} = 10;

print $o->x, "\n";

$o->set('x', 15);

print $o->x, "\n";


print $o->x, "\n";
print $o->{x}, "\n";
print $o->get('x'), "\n";

package object;

sub new {
        my $class = shift;
        my $self = {
                x => undef,
                y => undef,
                w => undef,
                h => undef,
        return bless $self, $class;

sub set {
        my ($self, $property, $value) = @_;
        $self->{$property} = $value;

sub get : lvalue {
        my ($self, $property) = @_;

        for my $property ('x', 'y', 'w', 'h') {
                eval "*$property = sub : lvalue{
                        my \$self = shift;
                        \$self->get('$property') = \$_[0] if exists \$_[0];

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]