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

[GT] Proposed patch for Indicators::new()



The constructor function 'new' for GT::Indicators attempts to
reference the variable $args in a slightly illegal way. To avoid an
interpreter error the author has added 'no strict "refs"' to the code.

To improve compatibility and to keep "strict" turned on at all times,
I have modified the sub as follows:

===== OLD VERSION =====
sub new {
    my ($type, $args, $key, $func) = @_;
    my $class = ref($type) || $type;

    no strict "refs";

    my $self = {};
    if (defined($args)) {
        if ( $#{$args} < $#{"$class\::DEFAULT_ARGS"} ) {
            for (my $n=($#{$args}+1); $n<=$#{"$class\::DEFAULT_ARGS"}; $n++) {
                push @{$args}, ${"$class\::DEFAULT_ARGS"}[$n];
            }
        }
        $self->{'args'} = GT::ArgsTree->new(@{$args});
    } elsif (defined (@{"$class\::DEFAULT_ARGS"})) {
        $self->{'args'} = GT::ArgsTree->new(@{"$class\::DEFAULT_ARGS"});
    } else {
        $self->{'args'} = GT::ArgsTree->new(); # no args
    }

    if (defined($func)) {
        die "We tried to pass a 'func' parameter to an indicator,
please convert the module...";
    }
    $self->{'func'} = sub { die "Please convert this module to NOT use
\$self->{'func'} ..."; };

    return manage_object(\
AT
{"$class\::NAMES"}, $self, $class,
$self->{'args'}, $key);
}
===== END OF OLD VERSION =====

===== NEW VERSION ====
sub new {
    my ( $type, $args, $key, $func ) = @_;
    my $class = ref($type) || $type;

    my $self = {}; $args ||= [];
    my @DEFAULTS; eval("\
AT
DEFAULTS = \
AT
$class\::DEFAULT_ARGS");
    splice( @DEFAULTS, 0, scalar(@$args) );
    push @$args, @DEFAULTS;
    $self->{'args'} = GT::ArgsTree->new( @$args );

    if ( defined($func) ) {
        die "We tried to pass a 'func' parameter to an indicator,
please convert the module...";
    }

    $self->{'func'} = sub {
        die "Please convert this module to NOT use \$self->{'func'} ..."
    };

    my @NAMES; eval("\
AT
NAMES = \
AT
$class\::NAMES");
    return &manage_object( \
AT
NAMES, $self, $class, $self->{'args'}, $key );
}
===== END OF NEW VERSION ====

Both versions of the sub work equally well. The new one is just a
little tighter and more compatible.

Regards,
Nick