Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to build the => in a statement?

2 views
Skip to first unread message

ds456

unread,
Apr 24, 2008, 6:50:23 PM4/24/08
to
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

John W. Krahn

unread,
Apr 24, 2008, 7:08:19 PM4/24/08
to

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

A. Sinan Unur

unread,
Apr 24, 2008, 7:09:14 PM4/24/08
to
ds456 <dsx...@yahoo.com> wrote in
news:AM-dnRU81ccyk4zV...@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 <1u...@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/

ds456

unread,
Apr 24, 2008, 8:10:34 PM4/24/08
to
> 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.

ds456

unread,
Apr 24, 2008, 8:13:18 PM4/24/08
to
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

A. Sinan Unur

unread,
Apr 24, 2008, 8:26:30 PM4/24/08
to
ds456 <dsx...@yahoo.com> wrote in
news:SPWdnfNw-qCDv4zV...@oco.net:

> 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'

That is neither an array nor a hash. Have you seen the posting
guidelines for this group?

Post code. Code we can see and run. Not vague descriptions.

> Sort of like so...
>
> foreach $key (keys @%hash) {
> #Some code to build a string from the current hash key...

Yeah, where on God's green earth under God's lovely blue sky is that
code???

> #Finally, a string is built that looks good...

Beauty is in the eye of the beholder.

I do not understand you. The hash keys are the attributes (I am
assuming) and the hash contains the values for those attributes.

Use them

> $string = "-text => 'OK',
> -width => 10, -state => 'disabled', -command => '\&okcommand'";

From this, am I to assume that the hash originally contained

my %hash = (


-text => 'OK',
-width => 10,
-state => 'disabled',

-command => \&okcommand,
);

> $xw->$widget($string)->pack();

$xw->$widget(%hash)->pack;

> But this doesn't work because the => operator can't be imbedded
> in the string.

It can be. The run time string is not source code though, so it does not
mean the same thing. The method call expects a list of arguments not a
single stringified version of it.

> 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);

I am afraid your logic does not seem suitably matched to Perl's
semantics. You cannot embed a for loop in an argument list.

$wx->$widget( %hash )->place( ... );

ds456

unread,
Apr 24, 2008, 9:01:37 PM4/24/08
to
> That is neither an array nor a hash. Have you seen the posting
> guidelines for this group?
>
> Post code. Code we can see and run. Not vague descriptions.

Sorry, but the last time I posted (quite a while ago) I got ripped by two
"experts" who, in effect, said "Stop posting all the damn code that we
don't have time to figure out - just ask the question!"

Anyway, you supplied the answer. The below works. The => operator CAN be
embedded in a variable despite my proving to myself in about 4 dozen
ways that it can't.

>
>
> my %hash = (
> -text => 'OK',
> -width => 10,
> -state => 'disabled',
> -command => \&okcommand,
> );
>

> $xw->$widget(%hash)->pack;
>

I just have to figure out why yours worked and mine doesn't. But it is a
lot easier to start from something that works, than trying to figure out
why something doesn't.

Thanks
DS

A. Sinan Unur

unread,
Apr 24, 2008, 9:23:26 PM4/24/08
to
ds456 <dsx...@yahoo.com> wrote in
news:M5OdncJMt5nssIzV...@oco.net:

>> That is neither an array nor a hash. Have you seen the posting
>> guidelines for this group?
>>
>> Post code. Code we can see and run. Not vague descriptions.
>
> Sorry, but the last time I posted (quite a while ago) I got ripped by
> two "experts" who, in effect, said "Stop posting all the damn code
> that we don't have time to figure out - just ask the question!"

Please read the posting guidelines. They explain how best to help others
help you by posting an *appropriate* amount of code.

> Anyway, you supplied the answer. The below works. The => operator
> CAN be embedded in a variable despite my proving to myself in about 4
> dozen ways that it can't.

Because => (fat comma) is not really being embedded anywhere below. You
misunderstood.

>> my %hash = (
>> -text => 'OK',
>> -width => 10,
>> -state => 'disabled',
>> -command => \&okcommand,
>> );
>>
>> $xw->$widget(%hash)->pack;
>>
>
> I just have to figure out why yours worked and mine doesn't. But it
> is a lot easier to start from something that works, than trying to
> figure out why something doesn't.

The version above passes a list of arguments rather than a single
stringfied version of that.

Gerry Ford

unread,
Apr 24, 2008, 9:30:11 PM4/24/08
to

"ds456" <dsx...@yahoo.com> wrote in message
news:M5OdncJMt5nssIzV...@oco.net...

DS,

These people were toilet-trained at gunpoint or have some other definciency
to make up for with mean-spiritedness.

With the red herring bit, he was actually alleging that you obfuscated your
trail deliberately, as opposed to were dealing with a new syntax whose
compiler comments are riddles.

I'm on my way to find a better place to talk about scripting languages and
leaving Dr. Unur, the cbfalconer of c.l.p.misc, and people whose constant
thumping about posting rules simply point away from their own ferret-like
behavior, behind.

--
"Life in Lubbock, Texas, taught me two things: One is that God loves you
and you're going to burn in hell. The other is that sex is the most
awful, filthy thing on earth and you should save it for someone you love."

~~ Butch Hancock


Uri Guttman

unread,
Apr 24, 2008, 11:59:41 PM4/24/08
to
>>>>> "d" == ds456 <dsx...@yahoo.com> writes:

>> Or in either case you could use an array:
>>
>> my @attributes = ( -title => 'This is text' );
>>
>> $xw->Button( @attributes )->place( -x => 1, -y => 1 );
>>
>>
>>
>> John

d> Thanks for the reply John, but that doesn't work for me either. Below is
d> the actual code and part of the error message.


d> my @attributes = ( -title => 'This is text');
d> $xw->Button( @attributes )->place(-x => 1, -y => 1);

d> #Tk::Error: unknown option "-title" at /usr/lib/perl5/Tk/Widget.pm line
d> 205.

ever think to rtfm? your code works with -text. john mistakenly used
-title as an example. but his concept is perfectly valid and works. just
use the correct attribute keys as the error told you.

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Ben Morrow

unread,
Apr 24, 2008, 11:49:07 PM4/24/08
to

Quoth "A. Sinan Unur" <1u...@llenroc.ude.invalid>:
> > 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);
>
> I am afraid your logic does not seem suitably matched to Perl's
> semantics. You cannot embed a for loop in an argument list.
>
> $wx->$widget( %hash )->place( ... );

You can. It's called 'map':

$wx->widget(
map { $_ => $hash{$key} } keys %hash
)->place(...);

The fact that that particular map statement is equivalent to simply
evaluating the hash in list context doesn't mean the construction isn't
useful in general.

Ben

--
don't get my sympathy hanging out the 15th floor. you've changed the locks 3
times, he still comes reeling though the door, and soon he'll get to you, teach
you how to get to purest hell. you do it to yourself and that's what really
hurts is you do it to yourself just you, you and noone else ** b...@morrow.me.uk

A. Sinan Unur

unread,
Apr 25, 2008, 12:16:13 AM4/25/08
to
Ben Morrow <b...@morrow.me.uk> wrote in
news:ju08e5-...@osiris.mauzo.dyndns.org:

>
> Quoth "A. Sinan Unur" <1u...@llenroc.ude.invalid>:
>> ds456 <dsx...@yahoo.com> wrote in
>> news:SPWdnfNw-qCDv4zV...@oco.net:
>>
>> > 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);
>>
>> I am afraid your logic does not seem suitably matched to Perl's
>> semantics. You cannot embed a for loop in an argument list.
>>
>> $wx->$widget( %hash )->place( ... );
>
> You can. It's called 'map':

No, a for loop and map are different beasts. The fact that map is a one-
to-possibly many transformation applied to the argument list and each
invocation to map could be translated into a loop equivalent and vice
versa does not make map and for equivalent.

>
> $wx->widget(
> map { $_ => $hash{$key} } keys %hash
> )->place(...);
>
> The fact that that particular map statement is equivalent to simply
> evaluating the hash in list context doesn't mean the construction
> isn't useful in general.

Of course, map is useful. However, the OP was transforming the whole
hash into a single string that looked like how a method call would look
like in the source and was surprised when that did not do what he
thought it would do. Clearly, there is a mismatch between how the OP
understands Perl constructs and what they mean. What is the point of
throwing the identity transformation into the argument list?

After all, I could also do the following:

#!/usr/bin/perl

use strict;
use warnings;

my %hash = qw( a 1 b 2 c 3 );

yabadabadooo(
do {
my @args;
for my $k ( sort keys %hash ) {
push @args, $k, $hash{$k};
}
@args;
}
);

sub yabadabadooo { print "@_\n"; }

__END__

and thereby place a for loop in the function call but that is just as
irrelvant as your response.

Ron Ford

unread,
Apr 25, 2008, 4:17:34 AM4/25/08
to

"A. Sinan Unur" <1u...@llenroc.ude.invalid> wrote in message
news:Xns9A8B2C03345...@127.0.0.1...

and he misappropiated the number of phony figs in our new 100-acre real
estate version. Would he care to speak to that?

Abigail

unread,
Apr 25, 2008, 5:43:31 AM4/25/08
to
_
ds456 (dsx...@yahoo.com) wrote on VCCCLI September MCMXCIII in
<URL:news:SPWdnfBw-qDnvIzV...@oco.net>:
^^ > 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 your original post:

>> 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);


You start with '-text', and then go to '-title'.


Guess where the problem lies.

Abigail
--
perl -MLWP::UserAgent -MHTML::TreeBuilder -MHTML::FormatText -wle'print +(
HTML::FormatText -> new -> format (HTML::TreeBuilder -> new -> parse (
LWP::UserAgent -> new -> request (HTTP::Request -> new ("GET",
"http://work.ucsd.edu:5141/cgi-bin/http_webster?isindex=perl")) -> content))
=~ /(.*\))[-\s]+Addition/s) [0]'

Dan Mercer

unread,
Apr 25, 2008, 1:11:35 PM4/25/08
to

"ds456" <dsx...@yahoo.com> wrote in message
news:AM-dnRU81ccyk4zV...@oco.net...

I think you have a fundamental misunderstanding of what => does. It's just
a
comma with the added advantage of quoting the previous word. So

$xw->Button(-text => "This is text")->place(-x => 1, -y => 1);

is equivalent to

$xw->Button("-text", "This is text")->place("-x", 1, "-y", 1);

You can always just put the pairs in an array and pass the array.
The alternate comma operator can add clarity. For instance,
the kill command takes a list of values as parameters, the first value of
which identifies the signal. That value can be either a number or symbolic
text.

kill 15, $pid1, $pid2;

can also be represented as

kill "TERM, $pid1, $pid2;

But I find it a little clearer to use

kill TERM => $pid1, $pid2;

Conceptually we write

kill SIGNAL, LIST

as a synopsis, but the kill function simply receives and processes a list.
So while you might be thinking there's some Tk special magic in the use of
'=>' in its
parameters, remember the function is just getting a paired list. And why
is it a paired list?
Because the data will be turned into a hash in the object.

So, build an array and pass the array or build a hash and pass the hash,
it's six of one and half dozen of another and Tk won't know the difference
anyhow.

Dan Mercer


>
> DS

Dan Mercer

unread,
Apr 25, 2008, 1:05:05 PM4/25/08
to

"ds456" <dsx...@yahoo.com> wrote in message
news:AM-dnRU81ccyk4zV...@oco.net...

I think you have a fundamental misunderstanding of what => does. It's just

a
comma with the added advantage of quoting the previous word. So

$xw->Button(-text => "This is text")->place(-x => 1, -y => 1);

is equivalent to

Dan Mercer

unread,
Apr 25, 2008, 1:08:32 PM4/25/08
to

"ds456" <dsx...@yahoo.com> wrote in message
news:AM-dnRU81ccyk4zV...@oco.net...

I think you have a fundamental misunderstanding of what => does. It's just

a
comma with the added advantage of quoting the previous word. So

$xw->Button(-text => "This is text")->place(-x => 1, -y => 1);

is equivalent to

John Bokma

unread,
Apr 25, 2008, 1:39:31 PM4/25/08
to
"Ron Ford" <r...@nowhere.ford> wrote:

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/

Ben Morrow

unread,
Apr 25, 2008, 2:05:43 PM4/25/08
to

Quoth "Dan Mercer" <dame...@comcast.net>:

>
> I think you have a fundamental misunderstanding of what => does. It's just
> a
> comma with the added advantage of quoting the previous word. So
>
> $xw->Button(-text => "This is text")->place(-x => 1, -y => 1);
>
> is equivalent to
>
> $xw->Button("-text", "This is text")->place("-x", 1, "-y", 1);

Strictly, it is equivalent to

$xw->Button( ( - "text" ), "This is text" )->place(...);

which is (due to a special case in unary minus added for Tk's benefit)
equivalent to

$xw->Button( "-text", "This is text" )->place(...);

This is important because '-' is not part of \w, so something like

( foo-bar => 1 )

will *not* be autoquoted.

Ben

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ b...@morrow.me.uk ~ Jorge Luis Borges, 'The Babylon Lottery'
--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
b...@morrow.me.uk (Kate Rusby)

Peter J. Holzer

unread,
Apr 26, 2008, 4:33:49 AM4/26/08
to
On 2008-04-25 01:01, ds456 <dsx...@yahoo.com> wrote:
>> That is neither an array nor a hash. Have you seen the posting
>> guidelines for this group?
>>
>> Post code. Code we can see and run. Not vague descriptions.
>
> Sorry, but the last time I posted (quite a while ago) I got ripped by two
> "experts" who, in effect, said "Stop posting all the damn code that we
> don't have time to figure out - just ask the question!"

This is a discussion group. If you post 200 lines of code where only 2
lines are relevant to the point you want to discuss, people will likely
not take the time to figure out what the point of your posting is.
Similarly, if you post a long, rambling text of 200 lines, where only
two sentences are relevant, people won't take the time to figure that
out, either.

So, make your postings as short as possible.

But you also need to be clear. If nobody understands what your question
is, nobody can help you. And since this is a discussion group about a
programming language, often the clearest way to state a problem is to
post a short script or code fragment which demonstrates it. This is
especially true of data structures. A code fragment showing the
initialization of a data structure like this:

>> my %hash = (
>> -text => 'OK',
>> -width => 10,
>> -state => 'disabled',
>> -command => \&okcommand,
>> );

is not longer than the corresponding textual description, but it is
completely unambiguous and therefore much clearer.

hp

0 new messages