in reply to passing a value to a function

Like the other monks have mentioned the parens after your function name are hurting you.
Another way of creating clearer OO code would be to encapsulate $radiovar an instance attribute of your class.
so you would have:
sub cleararray { my $self = shift; for ( $i = 0; $self->radiovar(); $i++ ) { for ( $j = 0; $self->radiovar(); $j++ ) { @{$self->array()}[$i][$j] = undef; } } }
With this approach you would define two other accessor methods:

so they might look something like this:
sub radiovar { my ( $self, $newval ) = @_; $self->{'radiovar'} = $newval if defined( $newval ); return $self->{'radiovar'}; } sub array { my ( $self, $newval ) = shift; $self->{'array'} = $newval if defined( $newval ); return $self->{'array'}; }
This way you aren't using error prone global variables, but you are writing better OO code.
Not to mention when using global variables inside your objects it kind of defeats the purpose of writing OO in the first place,
in that when a variable's purpose changes or you need to do something different to present that variable then you have to go
change lots more code than if you only had to change one or two subroutines. (That last statement may need some clarification.)

Now if your situation calls for only one $radiovar value per class you could make it a class attribute pertaining to all object of Class X. Which would look something like:

package X; ... my $radiovar; ... sub radiovar { my $newval = shift; $radiovar = $newval if defined( $newval ); return $radiovar; }

Replies are listed 'Best First'.
Re: Re: passing a value to a function
by CharlesClarkson (Curate) on Jun 26, 2001 at 03:38 UTC

    Since you already made some great points about calling and global variables, I'd like to add a bit about the algorithm. First the cleararray sub can be shortened:

    sub cleararray { my $self = shift; @{$self->array()}[$_] = [(0) x $self->radiovar()] for 0 .. $self->radiovar() - 1; }

    Second, it may not be necesary to use $self->radiovar() if $self->array() is already defined for size:

    sub cleararray { my $self = shift; @{$self->array()}[$_] = [(0) x @{$self->array()}] for 0 .. @{$self->array()} - 1; }

    HTH,
    Charles K. Clarkson