Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Closure question

by Pug (Monk)
on Mar 02, 2004 at 17:39 UTC ( [id://333312]=perlquestion: print w/replies, xml ) Need Help??

Pug has asked for the wisdom of the Perl Monks concerning the following question:

I have a question concerning Perl's closures.

The code below should, to me, completely work. But when I call doesnt_work @r is not initilized. 8( Which is surpising to me because perl knows that sub doesnt_work exists so why doesn't it init @r when it is called?

#!/usr/bin/perl -w { my @s=("This works\n"); sub works { shift @s} } #This works.. print &works; # Error!! # This gives an error of Use of uninitialized value in print at .... print &doesnt_work; { my @r=("Should this work..\n"); sub doesnt_work{ pop @r; } }

--
Pug

Replies are listed 'Best First'.
Re: Closure question
by broquaint (Abbot) on Mar 02, 2004 at 17:44 UTC
    This is the nature of the execution flow of perl - my @r hasn't been 'executed' yet, so you'll want something along the lines of a BEGIN block if you want it to be initialized and have it after the subroutine call e.g
    foo(); BEGIN { my $str = 'a string'; sub foo { print "\$str is $str\n" } } __output__ $str is a string
    HTH

    _________
    broquaint

Re: Closure question
by Limbic~Region (Chancellor) on Mar 02, 2004 at 18:05 UTC
    Pug,
    What you are using is a one-use closure. Because it is in a naked block it can not be used multiple times. And as broquaint points out, it must be "seen" prior to being used. Here is an example of a re-usable closure that can be used before or after the sub has been "seen".
    #!/usr/bin/perl use strict; use warnings; my $count_1 = iterator( 1 ); print $count_1->(), "\n" for 1 .. 10; my $count_2 = iterator( 'a' ); print $count_2->(), "\n" for 1 .. 10; sub iterator { my $iterator = shift; return sub { $iterator++ }; }
    Cheers - L~R
Re: Closure question
by fletcher_the_dog (Friar) on Mar 02, 2004 at 18:05 UTC
    I have run into this problem before and it has to do with the fact that the statement "my @r=..." hasn't been executed yet. Perl lets you use the @r under strict because your definition for "doesnt_work" is within the lexical scope of @r. But it does not automatically execute code just because it uses a variable that might possible be in a closure or just because it is in a block that contains a definition of a sub.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://333312]
Approved by arden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found