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

Saw something interesting, I think someone must be able to enlighten me. When I use Tk, I can say:
MainWindow->new(background => "red");
It gives me a red window, good. If I say,
MainWindow->new(-background => "red");

It gives me a red window, good. Minutes ago, I said to myself, let's try this,
MainWindow->new(+background => "red");
It gives me a red window, this start to be interesting. Then I went,
MainWindow->new(+background => "red", +background => "green");
It gives me a green window, this is good. Because the value for key "backgound" was assigned twice, obviously the second time overwrites the first assignment. I thought, okay, why not try something more fun,
MainWindow->new(+background => "red", -background => "green");
It gives me a red window, Hm, it is getting more interesting.

For a moment, I was wondering whether this has something to do with hash, so I tried $a = {"a" => 1, "-a" => 2}, but it turned out there are two key-value pairs, one of the key is "-a". So it has nothing to do with hash.
Then what is this? Did they do this purposely in Tk? Probably I can try IO::Socket::INET->new(Proto => "tcp", -Proto => "udp"), and see what will happen, but I decided to ask my fellow monks.

Replies are listed 'Best First'.
Re: - and + sign before keys, when pass parms to Tk functions
by diotalevi (Canon) on Nov 17, 2002 at 03:42 UTC

    This is part serendipity on the part of the Tk developer and part misunderstanding on your part. That leading '+' is a unary plus and from reading perlop you'll see "Unary "+" has no effect whatsoever, even on strings.". Reading slightly above that you'll see "a string consisting of a minus sign concatenated with the identifier is returned". All this means is "+background" becomes "background" and "-background" becomes "-background".

    From here it's up to the Tk developers to do something useful with the arguments. In general you just aren't supposed to pass a parameter in using both the bare and '-' prefixed form. You've just managed to highlight that Tk will take the bare parameter in preference to the '-' prefixed parameter. That's probably just the order that Tk does something with the parameters but /maybe/ that's also just related to the order that 'background' and '-background' occur inside a hash. Just do something sane and pick a style, ok? (and remember to read perlop)

    Just for giggles I did a demo of your examples but in generalized form because this really has nothing to do with Tk or any other modules.

    use Data::Dumper; { my %x = (background => 'red'); print Dumper(\%x), "\n" } # $VAR1 = { 'background' => 'red' }; { my %x = (-background => 'red'); print Dumper(\%x), "\n" } # $VAR1 = { '-background' => 'red' }; { my %x = (+background => 'red'); print Dumper(\%x), "\n" } # $VAR1 = { 'background' => 'red' }; { my %x = (+background => 'red', +background => 'green'); print Dumper(\%x), "\n" } # $VAR1 = { 'background' => 'green' }; { my %x = (+background => 'red', -background => 'green'); print Dumper(\%x), "\n" } # $VAR1 = { '-background' => 'green', 'background' => 'red' };
    __SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;
Re: - and + sign before keys, when pass parms to Tk functions
by nothingmuch (Priest) on Nov 17, 2002 at 04:38 UTC
    Some modules allow prepending field names with - signs for a shell switch feel thingy.
    It doesn't really do anything, except pass "-foo" instead of "Foo".

    What you're interested in is how the module constructor takes care of the key/value pairs.

    I snooped a bit in Tk, but since I've never used it (MacOS Classic here) I've only to tell you that it hashes it's arguments. Take it as if you're assigning to hash.
    I know Net::Telnet treats -foo and Foo as synonyms, so I'm assuming Tk doesn't fuss over it either.

    In short - Perl or perl don't make any special meaning for them...

    -nuffin
    zz zZ Z Z #!perl