Question - anonymous recursive functions

A

Alex

Hello,

I want a function to be private inside a module, so - I made it
anonymous. Also I want it to be recursive - how to do that?

use diagnostics;
use warnings;
use strict;

my $F1 = sub($)
{
my $n = shift;
print "$n\n";
$F1->($n + 1) if $n < 10;
};

F1(0);

This gives me the error message:

------------------------

Global symbol "$F1" requires explicit package name at tp205.pl line
27.
Execution of tp205.pl aborted due to compilation errors (#1)
(F) You've said "use strict vars", which indicates that all
variables
must either be lexically scoped (using "my"), declared beforehand
using
"our", or explicitly qualified to say which package the global
variable
is in (using "::").

Uncaught exception from user code:
Global symbol "$F1" requires explicit package name at tp205.pl line
27.
Execution of tp205.pl aborted due to compilation errors.
at tp205.pl line 30
 
A

Alex

Hello,

I want a function to be private inside a module, so - I made it
anonymous. Also I want it to be recursive - how to do that?

use diagnostics;
use warnings;
use strict;

my $F1 = sub($)
{
my $n = shift;
print "$n\n";
$F1->($n + 1) if $n < 10;

};

F1(0);

This gives me the error message:

------------------------

Global symbol "$F1" requires explicit package name at tp205.pl line
27.
Execution of tp205.pl aborted due to compilation errors (#1)
(F) You've said "use strict vars", which indicates that all
variables
must either be lexically scoped (using "my"), declared beforehand
using
"our", or explicitly qualified to say which package the global
variable
is in (using "::").

Uncaught exception from user code:
Global symbol "$F1" requires explicit package name at tp205.pl line
27.
Execution of tp205.pl aborted due to compilation errors.
at tp205.pl line 30

-------------------------

Please help
Thanks
Alex

My mistake above - I call it as

$F1->(0);

But the question remains....
 
A

Alexander Bartolich

Alex said:
[...]
my $F1 = sub($)
{
my $n = shift;
print "$n\n";
$F1->($n + 1) if $n < 10;
};
[...]
Global symbol "$F1" requires explicit package name at tp205.pl line
27.

The definition of »$F1« is finished only at the »};«.
Hence you can use it only afterwards.
The fix is trivial.

my $F1;
$F1 = sub($)
{
my $n = shift;
print "$n\n";
$F1->($n + 1) if $n < 10;
};
 
A

Alex

Quick and easy!
Thank you, Alexander

Alex said:
[...]
my $F1 = sub($)
{
my $n = shift;
print "$n\n";
$F1->($n + 1) if $n < 10;
};
[...]
Global symbol "$F1" requires explicit package name at tp205.pl line
27.

The definition of »$F1« is finished only at the »};«.
Hence you can use it only afterwards.
The fix is trivial.

my $F1;
$F1 = sub($)
{
my $n = shift;
print "$n\n";
$F1->($n + 1) if $n < 10;
};

--
Brüder, in die Tonne die Freiheit,
Brüder, ein Stoppschild davor.
Egal was die Schwarzen Verlangen
Rufen wir: Ja! Brav im Chor.
 
P

Peter Makholm

Alex said:
I want a function to be private inside a module, so - I made it
anonymous. Also I want it to be recursive - how to do that?

You can also use namespace::clean for private functions:

package Foo;

use namespace::clean qw(foo);

sub foo { return 42 }
sub bar { return foo() + 1 }

1;

This would effectivly make the foo function private to Foo.pm.

//Makholm
 
M

Marc Girod

You can also use namespace::clean for private functions:

Thanks!
The best answers are are those to questions you were too stupid to
make.
I, in this case.

Marc
 
P

Peter Makholm

Peter Makholm said:
You can also use namespace::clean for private functions:

I don't really like this interface, so I wrapped it up as an attribute
for subroutines. Soon to be available on CPAN as Sub::private[0] or at
Github[1].

The you could just write:

package Foo;

use Sub::private;

sub foo :private {
return 42;
}

sub bar {
return foo() + 1;
}

1;

0) Allready uploaded, just waiting for the indexer
1) http://github.com/pmakholm/sub-private-perl/tree/master

//Makholm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top