Click here to get back home

How to build the => in a statement?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
How to build the => in a statement? ds456 04-24-2008
Posted by ds456 on April 24, 2008, 6:50 pm
Please log in for more thread options
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

Posted by John W. Krahn on April 24, 2008, 7:08 pm
Please log in for more thread options
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

Posted by ds456 on April 24, 2008, 8:10 pm
Please log in for more thread options
> 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.

Posted by ds456 on April 24, 2008, 8:13 pm
Please log in for more thread options
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 ,
                }
        )->place(-x => somexpos, -y => someypos);

Thanks again

DS

Posted by John Bokma on April 25, 2008, 1:39 pm
Please log in for more thread options

If you morph again to keep trolling here, I'll contact your usenet
provider.

If you disagree with how some people post here, show them a better
example instead of trolling (again) this group. Sinan (IIRC) made a
valid point recently on people posting here:

* those who can help (even though it's not always in a nice way, you'll
get in the end help, and for free, even if you're too lazy to read
a book and/or documentation),
* those who can't just troll.

Based on your earlier trolls I would say that "can't" must be the story of
your life.

*ploink*

--
John

http://johnbokma.com/perl/

Similar ThreadsPosted
List of sample Build::Module Build.PL May 18, 2007, 12:51 pm
Best way to build up XHTML January 12, 2005, 1:12 am
ActivePerl build 813 broken? July 3, 2005, 9:02 pm
Curses won't build under Perl 5.8 August 23, 2005, 2:10 pm
Build Balanced Tree November 5, 2005, 7:25 pm
Can't get Module::Build to work January 13, 2006, 2:20 pm
5.8.8 build fails - no Makefile February 18, 2006, 1:40 pm
failure to build perl 5.8.8 September 28, 2006, 6:50 pm
Build of Perl 5.8.8 is failing December 27, 2006, 6:55 pm
Module build time January 18, 2008, 3:47 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap