Further to haukex's post++, here's an untested way your original approach might have been made to work. Please understand that I only suggest this for the purpose of experimenting to gain a better understanding of eval when you meet it again.

The basic idea here is to completely build and possibly check the eval string before passing it to eval.

my $newfont = ...; my ($fg, $bg) = (..., ...); my $opt = "-fg => $fg, -bg => $bg"; $opt .= ", -font => $newfont" if length $newfont; my $eval_string = # note \$rw is escaped, $opt is NOT qq{\$rw->Label(-text => "Col. 1", $opt, -padx => 10)->grid( \$rw->Label(-text => "Col. 2", $opt, -padx => 11), \$rw->Label(-text => "Col. 3", $opt, -padx => 11)) }; print "eval string '$eval_string' \n" if $DEBUG; my $rw = ...; # tk object must exist before string is eval-ed eval $eval_string;
Again, Don't Do This! in your Tk app: you'll give yourself a terrible headache. eval is powerful and can be very useful, but there's a time and place for everything.

Just to pound this completely into the ground, if $opt had been something that made sense to Label (so you didn't get the "Odd number of args ..." or any other error), why couldn't
    eval($mw->Label(-text => "Col. 1", $opt, -padx => 10)->grid( ...));
work? It might, or at least it might appear to.

In the  eval EXPR invocation above, the expression is
    $mw->Label(-text => "Col. 1", $opt, -padx => 10)->grid( ...)
and the value passed to eval depends on whatever the call to  grid() returns, which, IIRC, is nothing, the empty list (but I haven't checked this). In this case, the call to Label might work as expected when it was invoked during construction of the eval argument list, but when the (I think) empty argument list was then passed to eval, a perhaps puzzling Use of uninitialized value in eval "string" ... warning would be printed (if you had warnings enabled, which, as a righteous Monk, you always have :).


Give a man a fish:  <%-{-{-{-<


In reply to Re^8: tk option+value in variable? by AnomalousMonk
in thread tk option+value in variable? by cniggeler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.