http://www.perlmonks.org?node_id=924446

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

Monks: I am tryng to understand the code for floor.pl that is part of the standard "widget" application that is part of Perl/Tk. In that file I am seeing constructs such as

(line 64)

floor${floor_number}

and

$active_floor is defined as a variable and I see reference to:

$$active_floor

Could anypne please shed light on these two cases? Thanks

Replies are listed 'Best First'.
Re: variable constructs that I do not understand
by toolic (Bishop) on Sep 06, 2011 at 19:02 UTC
    You should have posted at least the full line of code, but I think I found what you're referring to at: floor.pl
    my $floor_number; for $floor_number (1..3) { $c->bind("floor${floor_number}", '<1>' => # line 64 [\&floor_display, $floor_number, \%floor_labels, \%floor_items +, \%cinfo, \$active_floor, $c_entry], );
    The first time through the loop, floor${floor_number} will become the literal string floor1 because of interpolation. The curlies are used to separate a variable from the strings around it without the need for whitespace. See Scalar value constructors.

    For $$active_floor, see perlref

      Thank you very much. I know where to start looking...
Re: variable constructs that I do not understand
by wink (Scribe) on Sep 06, 2011 at 21:42 UTC

    Perl allows you to use the value of one scalar as the identifier for another. Rarely is it a good idea, but occasionally it is useful.

    $var = 'foo'; $foo = 'bar'; print $var; # Will print "foo" print $$var; # Will print "bar"
    Also, you can use curly braces around a variable when it might be confusing to the compiler what you mean.
    $foo = 'foo'; print "$food"; # Will print nothing (no scalar $food is defined) print "${foo}d"; # Will print 'food' as expected
Re: variable constructs that I do not understand
by Anonymous Monk on Sep 06, 2011 at 19:04 UTC

    There isn't any context there, but it looks to me like some evil code concocted by somebody who doesn't know that Perl allows you to use arrays... and they decided to have variables named $floor1 $floor2 and so on.

      Thanks... suddenly all makes sense

        It shouldn't, its pure out of context nonsense

Re: variable constructs that I do not understand
by kcott (Archbishop) on Sep 07, 2011 at 06:44 UTC

    I seem to recall first using the Widget Demo back in 1996 or 1997. It was written in a very early version of Perl5. [Disclaimer: This is my best recollection from about 15 years ago - corrections welcome.]

    It served its purpose to demonstrate Perl/Tk widgets; I still refer to it occasionally.

    I would not recommend this code for learning Perl today. Of course, if you click on the See Code button, you'll still need to understand it.

    I do recommend the book Mastering Perl/Tk. (Just checked my copy - it was published in 2002 and uses Perl 5.6 - so, while it's a little more recent, I'd hardly call it modern.)

    -- Ken

Re: variable constructs that I do not understand
by Anonymous Monk on Sep 06, 2011 at 21:01 UTC
    Could it have been concocted "a long, long time ago?"