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

Hello, I am looking
if ($type eq 'bitmap') { my $bg = ($c->itemconfigure(qw/current -background/))[4]; if (defined $bg) { $iinfo->{restore_cmd} = "\$c->itemconfigure('current', -background => '$bg');"; } else { $iinfo->{restore_cmd} = "\$c->itemconfigure('current', -background => undef);"; } $c->itemconfigure(qw/current -background SteelBlue2/); return; }
and I simply do not understand the construct
my $bg = ($c->itemconfigure(qw/current -background/))[4];
What is 4?? I thought itemconfigure was used to set a property but seemingly it is being used here to retrieve a value? Thanks for the wisdom

Replies are listed 'Best First'.
Re: Strange construct
by davido (Cardinal) on Sep 07, 2011 at 19:38 UTC

    It's a subscript, like used with an array, or in this case, a list.

    my $value = ('a', 'b', 'c', 'd')[1]; say $value;

    ...prints b

    Very convenient as a quick and dirty tool in situations like this:

    my $keep = ( split /\s+/, $string )[2];

    ...although as with most things in Perl, that's only one of many ways to grab the third space-delimited item in a string.


    Dave

Re: Strange construct
by halley (Prior) on Sep 07, 2011 at 19:46 UTC

    This method must return a list, and the fifth element was the only thing useful to the caller. Expanding it out, it may make more sense.

    my $bg = ($c->itemconfigure(qw/current -background/))[4];
    my @values = $c->itemconfigure(qw/current -background/); my $bg = $values[4];

    You often see this for builtin functions like stat() or localtime(). They return lists of potentially useful stuff, but you may only need one thing. The documentation for each one clearly shows the meaning of each returned value in a list of returned values.

    --
    [ e d @ h a l l e y . c c ]

      ... you may only need one thing.

      And if you need more than one thing, that can be handled, too:

      >perl -wMstrict -le "my @ra = (qw(start a b c e l p r y z end))[-1, 6, 4, 7, 5, 1..3, 0]; ;; printf qq{'$_' } for @ra; " 'end' 'p' 'e' 'r' 'l' 'a' 'b' 'c' 'start'
Re: Strange construct
by ikegami (Pope) on Sep 07, 2011 at 20:24 UTC

    It's (part) of a list slice. List slices are documented in perldata.

    [ Of the three existing posts, neither of those facts were mentioned!?! ]

    A list slice has the following form:

    ( EXPR )[ EXPR ]

    Of the scalars returned by the left EXPR, the slice returns those identified by the indexes returned by the right EXPR.

    ( 'foo', 'bar', 'baz' )[ 0, 2, 4 ] # 'foo', 'baz', undef

    The scalars are returned in the order identified by the indexes. Negative indexes index from the end like with arrays. (-1 = last, -2 = second last, etc)

      Thank you to all who replied. I am always surprised to see the various syntaxes/constructs in perl that are not used regularly or even mentioned in books...
Re: Strange construct
by kcott (Bishop) on Sep 08, 2011 at 05:31 UTC

    The itemconfigure() method is documented in Tk::Canvas (see the WIDGET METHODS section).

    You'll also find it in the book Mastering Perl/Tk (see Chapter 9: The Canvas Widget).

    -- Ken