Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: Re: Scoping issues with code evaluation asserstions?

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


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

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.";

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

    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://267310]
help
Chatterbox?
[1nickt]: tobyink perl -MTypes::Standard= is_Int -Mstrict -wE 'say 1 if is_Int 1.0'
[jdporter]: so that I can use the existing expand unix util. Otherwise, I'll probably use Text::Tabs.
[1nickt]: pryrt I guess I don;t really care if user 42 logs on as 42.0 ... more of an academic question at this point.
[LanX]: jdporter: open PIPE,'-|' ?
[LanX]: oh you want the result line by line?
[jdporter]: ok, LanX, then what?
[jdporter]: It doesn't have to be line by line. Just "my program" "writes" to the external prog and also/then "reads" from it.
LanX open (You are not allowed to open to a command that pipes both in and out, but see IPC::Open2, IPC::Open3, and Bidirectional Communication with Another Process in perlipc for alternatives.)
[jdporter]: IPC::Open2, I guess
[jdporter]: yes, that

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-05-24 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?