I have a script, with lots of deep recursion and noticed, that with the usage of
references the whole programm becomes *much* slower (i noticed that in an
earlier state, when i wanted to put something often used into a sub. The programm
took at first less than 1 second, after i created the sub it took more than 5 seconds to
complete.)
I'm not sure what this has to do with references but subroutine
calling it expensive in Perl. As are constructing and unpacking
argument lists.
Now i am at a point where i want to use recursion, and perl says its evena deep
recursion.
If you want to use recursion I don't think you can do it without
subroutines. You'd have to refactor your code to be iterative.
So, should i throw away all the parameters and just use global variables to get rid
of referencing and dereferencing ?
I think it would have been clearer if you'd given us an example but if
I've understood correctly you effectively have a bunch of variables
that you are just passing unchanged down the recursion. Yes I'd
replace these by variables that are by some definition "globals". That
is to say they are used inside a subroutine but are not passed as
arguments. There are two obvious ways to go about this...
# Original
sub foo {
my ($this,$that,$other) = @_;
# stuff using $this,$that,$other...
# ...but not changing $this or $that
foo ($this, $that, whatever);
# stuff...
}
######################################
# With closure
sub foo {
my ($this,$that,$other) = @_;
my $r;
$r = sub{
my ($other) = @_;
# stuff using $this,$that,$other...
$r->(whatever);
# stuff...
};
$r->($other);
undef $r; # break circular reference
}
####################################
# With package variables and local()
our ($this,$that);
sub r {
my ($other) = @_;
# stuff using $this,$that,$other...
$r->(whatever);
# stuff...
}
sub foo {
(local($this,$that),my ($other)) = @_;
r($other);
}