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

Does anyone know how i can nest a functin inside a pattern match? Thanx!

Replies are listed 'Best First'.
(crazyinsomniac) Re: functions inside patterns
by crazyinsomniac (Prior) on Dec 20, 2001 at 10:39 UTC
    What are you talking about cheesyjames? I can guess, and it'd be a pretty good guess at that, but I do not want to. You must tell me what you mean cheesyjames.
    #!/usr/bin/perl -lw use strict; # here is one thing you might want sub what { my $what = shift; return ($what - 5); } my $string = 'a and a b and a 5 and a 6 and a 8 an what?'; print $string; $string =~ s/(\d)/&what($1)/ge;
    The other thing I think you might be thinking (dang that sounds stupid) is something along the lines of m/\d$pattern\b\l\a\h &what('what')/ where you want &what('what') to be expanded, in which case the only thing I can think of is for you to either store the value of &what in a variable.

    update: well you could always use eval, but I find that is in poor taste (I think it is a piss poor choice in design, when one must resort to "passing" functions via STDIN).

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      the function changes dynamically. So I can not call a sub. The script is running from a wrapper program which passes it functions via STDIN. I cant define too much in the perl code without breaking dependencies in the wrapper script. I could get in trouble for disclosing too much but thank you very much for your input!
        "the function changes dynamically. So I can not call a sub."

        A function is a sub, whether it be predeclared ( sub foo{return 'foo'}) or "dynamic" so to speak (a closure my $function = sub {return 'fooo'}; print &$function;).

        "The script is running from a wrapper program which passes it functions via STDIN"

        That sugguests to me you should be well versed in perlsec, cause in my opinion, that no good stratogy.

         
        ___crazyinsomniac_______________________________________
        Disclaimer: Don't blame. It came from inside the void

        perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      Good call! I have been trying to campaign to get the entire code base re-written in C. Just C. Not 4 languages. The bottom line rules I guess.
Re: functions inside patterns
by danger (Priest) on Dec 20, 2001 at 10:48 UTC

    The pattern is given double-quotish interpolation (unless single quotes are the delimiter), so you can interpolate functions in a pattern the same as you would in a double quoted string --- ie, as per this faq. For example:

    $_ = 'this is foo here'; sub foo {return 'foo'} print "Yup\n" if m/@{[foo()]}/; print "Yup\n" if m/${\(foo())}/;

    However, while this answers your question (I think), it isn't necessarily the best thing to be doing (and be careful with context).

Re: functions inside patterns
by andye (Curate) on Dec 20, 2001 at 16:48 UTC
    You could also look at match-time code evaluation (?{ CODE }) and match-time pattern interpolation (??{ CODE }) - covered in Chapter 5 of the Camel Book.

    andy.