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

Hello: I am using Inline::Spew to randomly walk through grammars. I am interested in adding variables to the grammar that can change each time it is compiled. However this only produces blank spaces instead of the value. I'd like to do something like this:
$foo = "Matt"; use Inline Spew => q{ START: sentence sentence: person " " action "." person: "$foo" | "She" | "Barnacle Bill" action: "walks" | "sails" | "swims" }; print spew();
So that a possible output would be - Matt sails. But "Matt" just comes out empty. Any ideas how I might accomplish this? It would be cool to have the user input names that are then added to the grammar before it's compiled. Thanks so much!

Replies are listed 'Best First'.
Re: Adding variables to Spew grammar
by ikegami (Patriarch) on May 05, 2005 at 01:55 UTC
    q{ ... } doesn't interpolate. Use qq{ ... } instead.
Re: Adding variables to Spew grammar
by davido (Cardinal) on May 05, 2005 at 02:21 UTC

    ...or a little more verbosely stated...

    The single quotes, or the q// operator do not let variables or other special metacharacters interpolate. That means that any variable contained within single quotes, or within a q// operator, will be seen as literal text, not as its interpolated value. For example:

    my $var = "Hello world\n"; print q/$var/, "\n"; print qq/$var/; __OUTPUT__ $var Hello world

    You can read up on the various sorts of quote and quote-like operators, how they work, and how interpolation works, in perlop.


    Dave

Re: Adding variables to Spew grammar
by NetWallah (Canon) on May 05, 2005 at 04:03 UTC
    I believe your problem has to do with compile-time vs Run-time.

    The "Inline" module generates code - that code gets generated BEFORE the "$foo="Matt" statement gets evaluated.

    Not sure what the solution to this would be, but you can experiment with BEGIN blocks, to apply a value to $foo before the Inline module is invoked.

         "There are only two truly infinite things. The universe and stupidity, and I'm not too sure about the universe"- Albert Einstein

Re: Adding variables to Spew grammar
by merlyn (Sage) on May 05, 2005 at 11:07 UTC
    I don't believe you'll be able to do that. I never envisioned doing that, so if anything similiar to that "worked", it'd be quite by accident.

    Note my security bug in the manpage though... any code contained within the "double quote inline executors" will be executed, so you might be able to get a similar effect with something like:

    BEGIN { our $foo = "Matt" } # has to be BEGIN to be early enough use Inline Spew => q{ person: "@{[$foo]}" START: person | person person };
    Actually, that may even work without the extra layer of evaluation there. Dunno. Maybe I should add replaceable components to the spew language.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Adding variables to Spew grammar
by Anonymous Monk on May 05, 2005 at 03:09 UTC
    Hello again. The double quotes don't seem to work either. It just returns blanks. I've tried many variations of double and single quotes. hmmm. Thanks for the suggestions though. I appreciate it very much!