in reply to setting up boolean parameters from keywords

$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.

Replies are listed 'Best First'.
Re^2: setting up boolean parameters from keywords
by rand0mmm (Acolyte) on Mar 18, 2006 at 04:02 UTC
    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.

        I am not sure yet how to add an additional function to my module. I get the shift idea, tho the syntax is new.

        Instead, 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;
      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;
        my %control = map { $_ => 'false' } split ' ', 'play ctrl loop' my %control = map { $_ => 'true' } split ' ', $4;
        Nice logic, but you will clobber all those 'false' key/value pairs by assigning a new list to %control. Replace the second line with something like the following and you should be set:

        $control{$_} = 'true' foreach split ' ', $4;

        Some people feel the long form is more maintainable:

        foreach ( split ' ', $4 ) { $control{$_} = 'true'; }