F
florian
Hello
It might sound slightly like a joke, but I am precisely trying to
implement the above. The reasons are as follows:
- I am programming a library function (in a module) that has a helper
function. Ideally, this helper function should be visible only to the
library function which uses it. Knowing that it is possible to say
{
my $lexical_var = 0;
sub foo {$lexical_var++;}
sub bar {print $lexical_var;}
}
to the effect is that
foo(); foo(); bar();
will print "2", but
print $lexical_var;
will yield an uninitialized variable error, I've tried to do the same
with a subroutine declaration, saying "my sub helper_function { ... ",
to learn that this is not yet implemented. As it is possible to store
a reference to an anonymous subroutine in a variable:
$helper_function = sub { ... };
and use it saying
$helper_function->(<args, if any>); # or:
&$helper_function(<args, if any>);
I've resourcefully tried to use that knowledge to implement it.
However, the fact that my helper function is recursive on top of it
all seems to be a problem: The following works:
$times = 0;
$code_ref = sub {
$times++;
print "Hello, there!\n";
$code_ref->() until ($times == 3);
};
$code_ref->(); # prints "Hello, there!" three times
but this (the line numbers are not part of the code):
1 $times = 0;
2
3 {
4
5 my $code_ref = sub {
6 $times++;
7 print "Hello, there!\n";
8 $code_ref->() until ($times == 3);
9 };
10
11 $code_ref->();
12
13 }
prints
Thus, the anonymous subroutine referenced to in $code_ref is called
exactly once (from line 11), but apparently it cannot be found when
called from within itself (at line 8). (More precise, I assume, would
be to say "from within the subroutine, the variable is not visible",
or "it cannot be dereferenced via this variable")
The explanation that the variable $code_ref has gone out of scope
within the subroutine declaration seems to suggest itself, but I
couldn't claim that I understand this, since the declaration is within
the same block (and hence I'm anything else but confident that it is
in fact the reason).
Is there anybody who can, and would care to, explain the reasons for
this?
Thanks very much!
Florian
It might sound slightly like a joke, but I am precisely trying to
implement the above. The reasons are as follows:
- I am programming a library function (in a module) that has a helper
function. Ideally, this helper function should be visible only to the
library function which uses it. Knowing that it is possible to say
{
my $lexical_var = 0;
sub foo {$lexical_var++;}
sub bar {print $lexical_var;}
}
to the effect is that
foo(); foo(); bar();
will print "2", but
print $lexical_var;
will yield an uninitialized variable error, I've tried to do the same
with a subroutine declaration, saying "my sub helper_function { ... ",
to learn that this is not yet implemented. As it is possible to store
a reference to an anonymous subroutine in a variable:
$helper_function = sub { ... };
and use it saying
$helper_function->(<args, if any>); # or:
&$helper_function(<args, if any>);
I've resourcefully tried to use that knowledge to implement it.
However, the fact that my helper function is recursive on top of it
all seems to be a problem: The following works:
$times = 0;
$code_ref = sub {
$times++;
print "Hello, there!\n";
$code_ref->() until ($times == 3);
};
$code_ref->(); # prints "Hello, there!" three times
but this (the line numbers are not part of the code):
1 $times = 0;
2
3 {
4
5 my $code_ref = sub {
6 $times++;
7 print "Hello, there!\n";
8 $code_ref->() until ($times == 3);
9 };
10
11 $code_ref->();
12
13 }
prints
Hello, there!
Undefined subroutine &main:: called at script line 8.
Thus, the anonymous subroutine referenced to in $code_ref is called
exactly once (from line 11), but apparently it cannot be found when
called from within itself (at line 8). (More precise, I assume, would
be to say "from within the subroutine, the variable is not visible",
or "it cannot be dereferenced via this variable")
The explanation that the variable $code_ref has gone out of scope
within the subroutine declaration seems to suggest itself, but I
couldn't claim that I understand this, since the declaration is within
the same block (and hence I'm anything else but confident that it is
in fact the reason).
Is there anybody who can, and would care to, explain the reasons for
this?
Thanks very much!
Florian