|
Prev: use of DBI; I am getting multiple error messages mixed in with?the correct output.
Next: FAQ 2.5 I grabbed the sources and tried to compile but gdbm/dynamic loading/malloc/linking/... failed. How do I make it work?
From: ds456 on 24 Apr 2008 18:50 I am trying to build Perl/TK components from scratch inside another TK program. I am stumped on how to represent the " => " in a statement inside a variable. This is a normal statement and works ... $xw->Button(-text => "This is text")->place(-x => 1, -y => 1); So does this... $attrib = '-text'; $value = 'This is text' $xw->Button($attrib => $value)->place(-x => 1, -y => 1); Since the "equal/greater than" is a signal to the compiler rather than a literal string, this does not... $string = "-title => 'This is text'"; $xw->Button($string)->place(-x => 1, -y => 1); I am trying to programatically build the inside of the parens and add various options based on user input, but can't figure out how to represent the => . Suggestions anybody. Or is the above as clear as mud? DS
From: John W. Krahn on 24 Apr 2008 19:08 ds456 wrote: > I am trying to build Perl/TK components from scratch inside another TK > program. I am stumped on how to represent the " => " in a statement > inside a variable. > > This is a normal statement and works ... > $xw->Button(-text => "This is text")->place(-x => 1, -y => 1); > > So does this... > $attrib = '-text'; > $value = 'This is text' > $xw->Button($attrib => $value)->place(-x => 1, -y => 1); > > Since the "equal/greater than" is a signal to the compiler rather than a > literal string, this does not... > $string = "-title => 'This is text'"; > $xw->Button($string)->place(-x => 1, -y => 1); > > I am trying to programatically build the inside of the parens and add > various options based on user input, but can't figure out how to represent > the => . > > Suggestions anybody. Or is the above as clear as mud? Assuming that the attributes are unique you could use a hash: my %attributes = ( -title => 'This is text' ); $xw->Button( %attributes )->place( -x => 1, -y => 1 ); Or in either case you could use an array: my @attributes = ( -title => 'This is text' ); $xw->Button( @attributes )->place( -x => 1, -y => 1 ); John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall
From: A. Sinan Unur on 24 Apr 2008 19:09 ds456 <dsxxxxx(a)yahoo.com> wrote in news:AM-dnRU81ccyk4zVnZ2dnUVZ_vWdnZ2d(a)oco.net: > I am trying to build Perl/TK components from scratch inside another TK > program. I am stumped on how to represent the " => " in a statement > inside a variable. > > This is a normal statement and works ... > $xw->Button(-text => "This is text")->place(-x => 1, -y => 1); > > So does this... > $attrib = '-text'; > $value = 'This is text' > $xw->Button($attrib => $value)->place(-x => 1, -y => 1); > > Since the "equal/greater than" is a signal to the compiler rather than > a literal string, this does not... > $string = "-title => 'This is text'"; > $xw->Button($string)->place(-x => 1, -y => 1); > > I am trying to programatically build the inside of the parens Those are method arguments. Not run time strings. > and add > various options based on user input, but can't figure out how to > represent the => . This is a red herring. What you need to do is to pass the correct arguments to the method. So, if for some reason, you don't have the $attrib and $value separately but embedded in a string of the form "attrib => whatever" you need to extract those two values and pass them as arguments to the method call. Easiest way to do that is my ($attrib, $value) = split /\s*=>\s*/, $string; But, given that you had obtain these things from a user interface means at some point you have access to the attribute you want to set and the value you want to set it to separately, so just use them as arguments. Sinan -- A. Sinan Unur <1usa(a)llenroc.ude.invalid> (remove .invalid and reverse each component for email address) comp.lang.perl.misc guidelines on the WWW: http://www.rehabitation.com/clpmisc/
From: ds456 on 24 Apr 2008 20:10 > Or in either case you could use an array: > > my @attributes = ( -title => 'This is text' ); > > $xw->Button( @attributes )->place( -x => 1, -y => 1 ); > > > > John Thanks for the reply John, but that doesn't work for me either. Below is the actual code and part of the error message. my @attributes = ( -title => 'This is text'); $xw->Button( @attributes )->place(-x => 1, -y => 1); #Tk::Error: unknown option "-title" at /usr/lib/perl5/Tk/Widget.pm line 205.
From: ds456 on 24 Apr 2008 20:13
O > This is a red herring. What you need to do is to pass the correct > arguments to the method. > > So, if for some reason, you don't have the $attrib and $value separately > but embedded in a string of the form > > "attrib => whatever" > > you need to extract those two values and pass them as arguments to the > method call. Thanks for the reply Sinan, but I didn't make my question clear. I have the attributes and values already separate, either in an array or hash. Example $widget = Button; -text 'OK' -width 10 -state 'disabled' -command '\&okcommand' and so forth. I don't want to hard code those widget options since there may be from 1 to 20 or so of them, depending on what kind of widget it is and what the user wants. Rather I want to feed the attributes and values to a routine that will build the widget with whatever number options I pass to it. Sort of like so... foreach $key (keys @%hash) { #Some code to build a string from the current hash key... } #Finally, a string is built that looks good... $string = "-text => 'OK', -width => 10, -state => 'disabled', -command => '\&okcommand'"; $xw->$widget($string)->pack(); But this doesn't work because the => operator can't be imbedded in the string. To put it logically, rather than in code, I am trying to do this... $wx->$widget( foreach $key (keys %hash){ $key => $hash{$key} , } )->place(-x => somexpos, -y => someypos); Thanks again DS |