Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Scoping issues with code evaluation asserstions?

by sauoq (Abbot)
on Jun 19, 2003 at 17:05 UTC ( #267276=note: print w/replies, xml ) Need Help??


in reply to Scoping issues with code evaluation asserstions?

It seems you have inadvertently created a closure.

If you use local $ret = ''; in that sub, things should work.

Note, however, that by "work" I don't mean that it will return "1.1.1.1.1." It will only return 1 when the ^(a) portion of the regex matches. That's because, if it doesn't match, the regex engine returns right away and never gets to your code assertion.

-sauoq
"My two cents aren't worth a dime.";
  • Comment on Re: Scoping issues with code evaluation asserstions?

Replies are listed 'Best First'.
Re: Re: Scoping issues with code evaluation asserstions?
by Cirollo (Friar) on Jun 19, 2003 at 18:56 UTC
    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.

      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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://267276]
help
Chatterbox?
[Discipulus]: we too; using opsview alarms
[marto]: the key word: outsourcing ;)
[Corion]: marto: Yeah, feels like that ;) You could set up the cronjob that auto-creates tickets :-))
[marto]: the ticketing system does not accept calls via email, nor has it a working API. It's tied into Active Directory for authentication and the Solaris boxes aren't on that domain
[Corion]: The one thing I haven't figured out a solution to is how to get an edge-trigger instead of sending an email every 5 minutes if the usage is above 90%. I want one mail when it goes over 90% but no more emails as long as it stays between 90% and 95%.
[Corion]: marto: Clever! ;)
[Corion]: You can only reach me by pager
[Corion]: Maybe the solution would be to launch a cron job every minute that takes two measurements a minute apart and sends a mail if the usage is below on the first and above threshold on the last measurement
[marto]: that's essentially it :)
[marto]: I think the long term solution would be to have sysadmins that do their job, so I don't have to do everything :P

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2017-01-24 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (203 votes). Check out past polls.