Question - anonymous recursive functions

Discussion in 'Perl Misc' started by Alex, Aug 11, 2009.

  1. Alex

    Alex Guest

    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
    Alex, Aug 11, 2009
    #1
    1. Advertising

  2. Alex

    Alex Guest

    On Aug 11, 4:52 pm, Alex <> wrote:
    > 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....
    Alex, Aug 11, 2009
    #2
    1. Advertising

  3. Alex wrote:
    > [...]
    > 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.
    Alexander Bartolich, Aug 11, 2009
    #3
  4. Alex

    Alex Guest

    Quick and easy!
    Thank you, Alexander

    On Aug 11, 4:59 pm, Alexander Bartolich <>
    wrote:
    > Alex wrote:
    > > [...]
    > > 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.
    Alex, Aug 11, 2009
    #4
  5. Alex <> writes:

    > 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
    Peter Makholm, Aug 12, 2009
    #5
  6. Alex

    Marc Girod Guest

    On Aug 12, 9:23 am, Peter Makholm <> wrote:

    > 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
    Marc Girod, Aug 12, 2009
    #6
  7. Peter Makholm <> writes:

    >> 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:


    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
    Peter Makholm, Aug 13, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reporter
    Replies:
    3
    Views:
    464
    Mike Schilling
    May 12, 2007
  2. n00m
    Replies:
    12
    Views:
    1,100
  3. vamsi
    Replies:
    21
    Views:
    2,043
    Keith Thompson
    Mar 9, 2009
  4. Replies:
    1
    Views:
    212
  5. florian
    Replies:
    9
    Views:
    106
    Michael Carman
    Apr 21, 2007
Loading...

Share This Page