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

I want to add multiple tags to a widget.
Using text the syntax is –tags => “arc”,”tall”
However I want to pass the multiple tag string as a variable to a subroutine.
I have tried various things but so far nothing has worked.
I am sure it is simple but then may I am too! Can someone help?

Replies are listed 'Best First'.
Re: Multiple tag syntax
by pc88mxer (Vicar) on Jun 06, 2008 at 15:13 UTC

    To specify multiple tags you need to use an array ref:

    my $tags = [ "arc", "tall" ]; ... $widget->configure(-tags => $tags);
    Also, see the example in the perlTk documentation Canvases and tags.

    In this example, $tags is a scalar, so you can pass it to a subroutine like you would pass a string:

    $tags = [ "arc", "tall" ]; build_widget($frame, $tags); ... sub build_widget { my ($frame, $tags) = @_; ... $widget->configure(-tags => $tags); }
      Thanks - that sorted it fine!!
Re: Multiple tag syntax
by moritz (Cardinal) on Jun 06, 2008 at 15:25 UTC
    The syntax isn't specific to the TK::* tags stuff, it's a general syntax for array references.

    There are basically two ways to create such a beast. On is to use an array, and pass a reference to it:

    my @array = (1, 2, 3); my $array_ref = \@array; # the \ takes the reference

    The other way is to use square brackets to create an anonymous array reference:

    my $array_ref = [1, 2, 3];

    More on this, and the difference between the two, can be found in perlreftut.