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

I'm a programmer, new to perl.
My first project is making a new extension module for a perl based wiki.
It uses \G to create a chain of text replacement patterns.

My new rule is for a new text pattern like so:
[[*.mov x y options_list #bg_color]]
or more structurally [[*.mov int int (play loop ctrl) #int]]

Using:
 if (/\G\[\[(.*?)\.mov (\d+) (\d+) (.*?) #(.*?)\]\]/cgi)
I am gathering up all the params.

My primary question is all about the (play loop control) options list in now in $4. This should be 'play ctrl loop' or some lesser unordered variant of this list. (I want to pass in these optional params as words, not booleans. Setting them true if they are present. )

So how do I process $4 and set local boolean variables (my $play, my $loop, my $ctrl) based on the absence/presence of these keywords?Essentially false unless the keyword is present.

My humble code, that should set $loop to be true or false, now returns empty regardless:
 my $loop = ($4 eq 'loop');

How do I do this correctly? I feel so close to insight.

My second question has to do with also wanting the color parameter to be optional, now I even ignore the fact it's a number, and just look after the # sign. And how to set it by default to #000000 if the value isn't present. I just can't see it yet.



Here's the working module. Everything is good but my booleans.
# Copyright (C) 2004 Phillip Reay # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. $ModulesDescription .= '<p>$Id: mov.pl,v 1.14 2005/10/09 11:57:52 as E +xp $</p>'; push(@MyRules, \&movRule); $RuleOrder{\&movRule} = -10; # run before default rules! sub movRule { # [[src.mov x:320 y:240 play:true ctrl:true loop:tru +e bg:#000000]] if (/\G\[\[(.*?)\.mov (\d+) (\d+) (.*?) #(.*?)\]\]/cgi) { # [[src.mov x y play ctrl loop bg:#000]] my $src = $1; my $x = $2; my $y = $3; my $auto = ($4 eq 'play'); my $ctrl = ($4 eq 'ctrl'); my $loop = ($4 eq 'loop'); my $bg = $5; if ($1 eq 'http://') { return qq { <OBJECT classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6 +B' width="$x" height="$y" codebase='http://www.apple.com/qtactivex/qtplugin.cab' +> <param name='src' value="$src.mov"> <param name='autoplay' value="$auto"> <param name='controller' value="$ctrl"> <param name='loop' value="$loop"> <EMBED src="$src.mov" width="$x" height="$y" autoplay= +"$auto" controller="$ctrl" loop="$loop" bgcolor="#$bg" pluginspage='http://www.apple.com/quicktime/download/' +></EMBED></OBJECT> } } return qq { <OBJECT classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' w +idth="$x" height="$y" codebase='http://www.apple.com/qtactivex/qtplugin.cab'> <param name='src' value="../movies/$src.mov"> <param name='autoplay' value="$auto"> <param name='controller' value="$ctrl"> <param name='loop' value="$loop"> <EMBED src="../movies/$src.mov" width="$x" height="$y" aut +oplay="$auto" controller="$ctrl" loop="$loop" bgcolor="#$bg" pluginspage='http://www.apple.com/quicktime/download/'></E +MBED></OBJECT> } } return undef; }

Replies are listed 'Best First'.
Re: setting up boolean parameters from keywords
by Tanktalus (Canon) on Mar 18, 2006 at 02:03 UTC

    $4 eq 'play' is true only if $4 exactly matches 'play' - no more, no less. You may be thinking of index: my $auto = (-1 != index $4, 'play'). But even that isn't how I'd do this. A bit more perlish is:

    my %control = map { $_ => 1 } split ' ', $4;
    This creates a hash, %control, with the keys that are specified. Instead of $auto, you would use $control{play}. Instead of $loop, you would use $control{loop}, etc.

      Cool.
      I am following idea of hash (tho not construction. I will read about map. I guess split divides $4 into hash names using the space char and map stuffs 1 into it) I see how to access, but I must assume the result is 1 or 0, not 'true' or 'false', which are actual values I need in my $play (or $control{play} ). Maybe I can do:  my %control = map { $_ => 'true' } split ' ', $4; but how do I deal with my 'false' nonpresent options that don't get a hash made for them?

        Ah, that changes things a fair bit. I was merely going by the behaviour you had originally - $4 eq 'play' doesn't return 'true' and 'false' but '1' and undef. One alternative is to use a function that converts, such as:

        sub bool2str { shift ? 'true' : 'false' }
        Then you could use bool2str($control{play}) to get the strings you want.

        could I just intialize the hash with a full list of options set to false, then set them true based on $4?
        my %control = map { $_ => 'false' } split ' ', 'play ctrl loop' my %control = map { $_ => 'true' } split ' ', $4;