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

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

by diotalevi (Canon)
on Jun 19, 2003 at 19:17 UTC ( #267314=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re: Re: Re: Re: Scoping issues with code evaluation asserstions?
Download Code
Re: Re: Re: Re: Re: Scoping issues with code evaluation asserstions?
by sauoq (Abbot) on Jun 19, 2003 at 19:34 UTC

    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://267314]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-12-29 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls