# Is \$A = \$B = n; valid?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
•  Subject
• Author
• Posted on
Is the syntax \$A = \$B = \$C = number acceptable?
--
Joey

## Re: Is \$A = \$B = n; valid?

Depends on what you want to do; it *would* assign 'number' to all three
variables.

Can't say I like the construct though. It looks awfully hacky, IMO.

Mart

--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.

## Re: Is \$A = \$B = n; valid?

Explain? It's a standard construction in C-derived languages, and makes
perfect sense once you realise assignment is just an operator like any
other with a well-defined return value (and useful side-effects, of
course).

(It's not something I would use very often, because it's not very often
I want to assign the same value to multiple variables at the same time.
The most common case would probably be something like (using Catalyst)

sub foo :Local {
my (\$self, \$c) = @_;
my \$st = \$c->stash;

my \$item = \$st-> = \$c->model(...)->find(...);

though I can't say as I like even that much redundancy.)

Furthermore, in Perl (but not in C), assignment returns the LHS as an
lvalue, leading to standard idioms like

(my \$y = \$x) =~ s/foo/bar/;

Ben

## Re: Is \$A = \$B = n; valid?

I know it makes sense. In my eyes, although slightly redundant, I think
separate assignments look clearer.

This doesn't grate on my nerves as much; probably because, as you say,
it's standard idiom.

Mart

--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.

## Re: Is \$A = \$B = n; valid?

What happened when you tried it?

Ben

## Re: Is \$A = \$B = n; valid?

Ben Morrow wrote:

It assigned number to all three variables. I knew it worked, but I was
inquiring about its acceptability in practice. (In the same way one might
question the practice of omitting 'strict.' It works, but is it acceptable
practice?) Is it possible that employing this syntax could get my script
in trouble somehow?

--
Joey

## Re: Is \$A = \$B = n; valid?

No, it's perfectly standard.

The only exception is when one of the variables in question is a glob,
or a scalar holding a glob. Glob assignment does strange things, and the
results don't always make sense.

Ben

## Re: Is \$A = \$B = n; valid?

You may have intended to ask that, but that is not what you in fact asked.

You asked if the syntax is acceptable.

With a programming language, if the compiler/interpreter accepts it,
then it is, well, acceptable.

You have a learning opportunity, learn what the difference is
between "syntax" and "semantics":

http://www.google.com/#q=difference+between+syntax+and+semantics

Those are questions of *semantics*, not of syntax.

Pigs can fly.

The syntax of that is perfectly fine.

Its semantics are certainly questionable (ie. wrong).

--
Rest In Peace:
Jonah McClellan gave his life for his country in a
helicopter crash in Afghanistan on September 21,2010.
Please pray for his wife and three children.

## Re: Is \$A = \$B = n; valid?

TM>     Pigs can fly.

TM> The syntax of that is perfectly fine.

TM> Its semantics are certainly questionable (ie. wrong).

not according to pink floyd! :)

uri

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

## Re: Is \$A = \$B = n; valid?

Those wankers won't even let you have any pudding until
after you've finished your meat.

--
Rest In Peace:
Jonah McClellan gave his life for his country in a
helicopter crash in Afghanistan on September 21,2010.
Please pray for his wife and three children.

## Re: Is \$A = \$B = n; valid?

>>
TM> Pigs can fly.
>>
TM> The syntax of that is perfectly fine.
>>
TM> Its semantics are certainly questionable (ie. wrong).
>>
>> not according to pink floyd! :)

TM> Those wankers won't even let you have any pudding until
TM> after you've finished your meat.

it all depends on your definitions of pudding and meat. what about a
beef pudding?

uri

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

## Re: Is \$A = \$B = n; valid?

So what? He clarified it.

hp

## Re: Is \$A = \$B = n; valid?

So I got snarky.

By then, the snarkiness had already been released.

--
Rest In Peace:
Jonah McClellan gave his life for his country in a
helicopter crash in Afghanistan on September 21,2010.
Please pray for his wife and three children.

## Re: Is \$A = \$B = n; valid?

Ben Morrow wrote:

The trouble with trying things is you don't know
if the behaviour you observe is part of the defined
behaviour, or merely a quirk of the implementation.

It is "trying things" and using what works that led
to many Mac programmers creating software that failed
when Apple change non-documented parts of the OS.

the should have worked from the spec, not the implementation,
at least as far as possible.

IMHO the OP is quite correct to ask his question

(I find that while pretty much everything in Perl
*is* documented, finding the right part of the
documentation is hard, and I've been using Perl
for a few years now)

BugBear

## Re: Is \$A = \$B = n; valid?

It is awfully rude of you to ask people when a machine could be asked
instead.

perl -ce '\$A = \$B = \$C = 42'

-e syntax OK

--
Rest In Peace:
Jonah McClellan gave his life for his country in a
helicopter crash in Afghanistan on September 21,2010.
Please pray for his wife and three children.

## Re: Is \$A = \$B = n; valid?

Tad McClellan wrote:

Thanks, Tad.

Sorry for your loss.
--
Joey

## Re: Is \$A = \$B = n; valid?

JLi> Tad McClellan wrote:
>>
>>> Is the syntax \$A = \$B = \$C = number acceptable?
>>
>>
>> It is awfully rude of you to ask people when a machine could be asked
>> instead.
>>
>> perl -ce '\$A = \$B = \$C = 42'
>>
>> -e syntax OK

JLi> Thanks, Tad.

JLi> Sorry for your loss.

your loss as tad will likely not help you again. his advice is very
cogent. always let the computer help you first. human help should be
secondary.

uri

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

## Re: Is \$A = \$B = n; valid?

Read Tad's signature please - I don't think Joey was being snarky.

sherm--

--
Sherm Pendley
<http://camelbones.sourceforge.net
Cocoa Developer

## Re: Is \$A = \$B = n; valid?

writes:
>>
JLi> Tad McClellan wrote:
>> >>
>> >>> Is the syntax \$A = \$B = \$C = number acceptable?
>> >>
>> >>
>> >> It is awfully rude of you to ask people when a machine could be asked
>> >> instead.
>> >>
>> >> perl -ce '\$A = \$B = \$C = 42'
>> >>
>> >> -e syntax OK
>>
JLi> Thanks, Tad.
>>
JLi> Sorry for your loss.
>>
>> your loss as tad will likely not help you again.

SP> Read Tad's signature please - I don't think Joey was being snarky.

yeah my bad. i spoke to tad the other day about that. a distant cousin.

uri

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

## Re: Is \$A = \$B = n; valid?

\$ perl -c -Mstrict -Mwarnings -e '\$A = \$B = \$C = 42'
Global symbol "\$A" requires explicit package name at -e line 1.
Global symbol "\$B" requires explicit package name at -e line 1.
Global symbol "\$C" requires explicit package name at -e line 1.
-e had compilation errors.

\$ perl -c -Mstrict -Mwarnings -e 'my \$A = my \$B = my \$C = 42'
-e syntax OK

--
David Canzi        | "If you can't learn to do something well, learn
| to enjoy doing it poorly." -- Ashleigh Brilliant