Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Scoping issues with code evaluation asserstions?

by Cirollo (Friar)
on Jun 19, 2003 at 18:56 UTC ( #267306=note: print w/replies, xml ) Need Help??


in reply to Re: Scoping issues with code evaluation asserstions?
in thread Scoping issues with code evaluation asserstions?

That was my first thought, but it didn't make much sense to me. Maybe my understanding of scoping isn't quite as good as I thought, but I thought that you had to declare a variable outside of the scope of the sub in order to create a closure.

Could it be that the one-time compilation of the regex somehow causes the closure?

Also, saying local $ret = ''; breaks under strict:

Variable "$ret" is not imported at /home/apirkle/bin/retest.pl line 8. Global symbol "$ret" requires explicit package name at /home/apirkle/b +in/retest.pl line 6. Global symbol "$ret" requires explicit package name at /home/apirkle/b +in/retest.pl line 8.

Replies are listed 'Best First'.
Re: Re: Re: Scoping issues with code evaluation asserstions?
by diotalevi (Canon) on Jun 19, 2003 at 19:08 UTC

    You'd need to have declared $ret as a global somewhere for local to localize it. Remember that local() declares nothing.

    use vars '$ret'; sub regex { local $ret = '';

    What you didn't realize was that the (?{ ... }) is a closure so when it first ran it captured the original $ret and continued to write to that same instance which wasn't the same one being returned in later iterations.

Re: Re: Re: Scoping issues with code evaluation asserstions?
by sauoq (Abbot) on Jun 19, 2003 at 19:11 UTC
    Could it be that the one-time compilation of the regex somehow causes the closure?

    That's exactly what it is. Your regex() sub isn't the closure, the code in the regular expression code assertion is.

    Also, saying local $ret = ''; breaks under strict:

    Well of course it does. :-) (I should have mentioned that though.) Use our $ret = ''; or declare $ret with use vars qw( $ret ); instead. And keep in mind that whichever way you choose to declare it, it's still a global variable.

    -sauoq
    "My two cents aren't worth a dime.";
    

      I have this clever idea to have the function use its own name as the slot for the return value.

      sub regex { my $in = shift; # Assign something to $ret in a code assertion my $function = (caller 0)[3]; no strict 'refs'; $$function = ''; $in =~ m/^(a)(?{ $$function = 1})/; return $$function; } while(<DATA>) { print regex($_) . "."; } __DATA__ a b a abcd bcda

        I think that's a bit too clever. What if there is a global variable named $regex? You've just clobbered it. I think creating a local alias is probably better.

        use strict; use warnings; sub regex { my $in = shift; my $ret = ''; # Assign something to $ret in a code assertion local *alias = \$ret; $in =~ m/^(a)(?{ $alias = 1})/; return $ret; } while(<DATA>) { print regex($_) . "."; } print "\n"; __DATA__ a b a abcd bcda
        -sauoq
        "My two cents aren't worth a dime.";
        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://267306]
help
Chatterbox?
LanX in train, bad connection
[Corion]: ambrus: Yeah - we're in that situation too, except that there is no time to do the reorganizing :-/
[LanX]: ... so my boss started a project with the newest sun servers and invited the traders to come on weekend to test it... and they were so pleased, that they forced him to keep it in production...
[ambrus]: Corion: sure, this is the long-term plan. The short term is that I have to run this ungodly mess to get results from the new input data today.
[Corion]: ambrus: Most of our "automation" is tied to process exit codes and a shell pipeline :-\
[LanX]: ... a week later they realized that one of the databases - which recorded how much the other banks due to this bank - was not correctly plugged
[ambrus]: Corion: I have no problem with exit codes and shell pipeline. My problem is that the current process requires a lot of manual intervention from me, including editing the source codes.
[ambrus]: (Also a lot of manual intervention by two or three other co-workers, who do other parts of the process.)
[ambrus]: Some of the manual part is unavoidable, but not all.
[choroba]: LanX was there a way to recover the numbers from the remaining information?

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-03-29 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (350 votes). Check out past polls.