what is the "=-" operator

M

Mr P

I "typoed" a line and entered:

my $l =- $_[0];

and it seemed to assign $_[0] to $l, and preprend '-' to it. I don't
see the "=-" operator in Camel index. I tried "=+" and it said I had
the "+=" operator backwards.

What did I "accidently" discover?

Mister P
 
A

A. Sinan Unur

I "typoed" a line and entered:

my $l =- $_[0];

and it seemed to assign $_[0] to $l, and preprend '-' to it. I don't
see the "=-" operator in Camel index. I tried "=+" and it said I had
the "+=" operator backwards.

Are you so lazy that you could not bring yourself to checking the result
of applying the operator?
What did I "accidently" discover?

You would not have had an accident had you read

perldoc perlop

See Assignment Operators.

Sinan
 
F

Fabian Pilkowski

* Mr P said:
I "typoed" a line and entered:

my $l =- $_[0];

and it seemed to assign $_[0] to $l, and preprend '-' to it. I don't
see the "=-" operator in Camel index. I tried "=+" and it said I had
the "+=" operator backwards.

My Perl gives also a warning if I use "=-", similiar to "=+".

Reversed -= operator at D:\home\news.pl line 6.
What did I "accidently" discover?

Perl detects an unusual notation of the "unary negation operator" and
warns you about that. The both lines

my $x = -$y; # usual
my $x =- $y; # with warning

are the same, semantically. Its behavior is described in `perldoc
perlop` in paragraph "Symbolic Unary Operators":

Unary ``-'' performs arithmetic negation if the operand is numeric.
If the operand is an identifier, a string consisting of a minus sign
concatenated with the identifier is returned. Otherwise, if the
string starts with a plus or minus, a string starting with the
opposite sign is returned. One effect of these rules is that
-bareword is equivalent to ``-bareword''.

Seems, this describes exactly what you discovered.

regards,
fabian
 
I

Ilmari Karonen

Mr P said:
I "typoed" a line and entered:

my $l =- $_[0];

and it seemed to assign $_[0] to $l, and preprend '-' to it. I don't
see the "=-" operator in Camel index. I tried "=+" and it said I had
the "+=" operator backwards.

What did I "accidently" discover?

The fact that perl isn't always very picky about whitespace. Your
code is exactly (except for warnings) equivalent to:

my $l = -$_[0];

You _should_ get a warning for both =- and =+, though. I don't know
why you only got one for =+.
 
I

Ilmari Karonen

A. Sinan Unur said:
I "typoed" a line and entered:
my $l =- $_[0];
and it seemed to assign $_[0] to $l, and preprend '-' to it. I don't
see the "=-" operator in Camel index. I tried "=+" and it said I had
the "+=" operator backwards.

Are you so lazy that you could not bring yourself to checking the result
of applying the operator?

I think you were a bit hasty this time, Sinan. The post you replied
to seems to me to be perfectly within the guidelines, and it looks
like the OP has in fact done his homework.

You would not have had an accident had you read
perldoc perlop
See Assignment Operators.

The OP mentions the Camel book, which (IIRC) contains that part of
perlop more or less verbatim. However, as he says, it indeed doesn't
contain a "=-" operator. One can't expect it to be immediately
obvious to everyone that the whitespace between assignment and
negation operators may be omitted.

You might've pointed him instead to the Symbolic Unary Operators
section of perlop, which mentions a somewhat obscure feature of the
unary negation operator:

Unary "-" performs arithmetic negation if the operand is numeric.
If the operand is an identifier, a string consisting of a minus sign
concatenated with the identifier is returned. Otherwise, if the
string starts with a plus or minus, a string starting with the
opposite sign is returned. One effect of these rules is that
-bareword is equivalent to "-bareword".

I think this may be what confused him in the first place, since one
wouldn't necessarily expect negating a non-numeric value to yield
anything meaningful (except zero and a warning, perhaps).
 
A

A. Sinan Unur

A. Sinan Unur said:
I "typoed" a line and entered:
my $l =- $_[0];
and it seemed to assign $_[0] to $l, and preprend '-' to it. I don't
see the "=-" operator in Camel index. I tried "=+" and it said I had
the "+=" operator backwards.

Are you so lazy that you could not bring yourself to checking the
result of applying the operator?

I think you were a bit hasty this time, Sinan.
....

If the operand is an identifier, a string consisting of a minus sign
concatenated with the identifier is returned.
....

I think this may be what confused him in the first place, since one
wouldn't necessarily expect negating a non-numeric value to yield
anything meaningful (except zero and a warning, perhaps).

I agree. I did misinterpret the OP's question, and did point to the
wrong section in the docs.

Sincere apologies, and thank you for pointing out my error.

Sinan
 
G

Glenn Jackman

At 2005-06-23 04:34PM said:
I "typoed" a line and entered:

my $l =- $_[0];

and it seemed to assign $_[0] to $l, and preprend '-' to it. I don't
see the "=-" operator in Camel index. I tried "=+" and it said I had
the "+=" operator backwards.

What did I "accidently" discover?

In addition to all the other advice you've received, the 'diagnostics'
module is helpful too:
perl -w -Mdiagnostics -e 'my $x =- 4; print $]'
gives:
Reversed -= operator at -e line 1 (#1)
(W syntax) You wrote your assignment operator backwards. The = must
always comes last, to avoid ambiguity with subsequent unary operators.

5.006001
 
X

xhoster

Glenn Jackman said:
At 2005-06-23 04:34PM said:
I "typoed" a line and entered:

my $l =- $_[0];

and it seemed to assign $_[0] to $l, and preprend '-' to it. I don't
see the "=-" operator in Camel index. I tried "=+" and it said I had
the "+=" operator backwards.

What did I "accidently" discover?

In addition to all the other advice you've received, the 'diagnostics'
module is helpful too:
perl -w -Mdiagnostics -e 'my $x =- 4; print $]'
gives:
Reversed -= operator at -e line 1 (#1)
(W syntax) You wrote your assignment operator backwards. The =
must always comes last, to avoid ambiguity with subsequent unary
operators.

5.006001


Perhaps that should be "Possibly reversed -= operator". The original
warning is given with such certainty, it implies (to me anyway) that it is
just going ahead and interpreting it as an assignment operator for you.

Xho
 
A

Anno Siegel

[...]
In addition to all the other advice you've received, the 'diagnostics'
module is helpful too:
perl -w -Mdiagnostics -e 'my $x =- 4; print $]'
gives:
Reversed -= operator at -e line 1 (#1)
(W syntax) You wrote your assignment operator backwards. The =
must always comes last, to avoid ambiguity with subsequent unary
operators.

5.006001


Perhaps that should be "Possibly reversed -= operator". The original
warning is given with such certainty, it implies (to me anyway) that it is
just going ahead and interpreting it as an assignment operator for you.

ITYM "interpret it as a decrement-and-assign operator"; "=" is an assignment
too. You may be right, but I don't think P5P will be convinced to change
a system warning on that grounds. Code may rely on it.

While we're at it, "must always comes last" could use improvement too.
That should be easier.

Anno
 
A

axel

Anno Siegel said:
In addition to all the other advice you've received, the 'diagnostics'
module is helpful too:
perl -w -Mdiagnostics -e 'my $x =- 4; print $]'
gives:
Reversed -= operator at -e line 1 (#1)
(W syntax) You wrote your assignment operator backwards. The =
must always comes last, to avoid ambiguity with subsequent unary
operators.
Perhaps that should be "Possibly reversed -= operator". The original
warning is given with such certainty, it implies (to me anyway) that it is
just going ahead and interpreting it as an assignment operator for you.
ITYM "interpret it as a decrement-and-assign operator"; "=" is an assignment
too. You may be right, but I don't think P5P will be convinced to change
a system warning on that grounds. Code may rely on it.

The compiler is reasonably clever with regard to generating the
warning message, since it generates it for:

my $x =- 4;

where it might reasonably indicate a typo but not for:

my $x =-4

where it may well assume that someone is using a 300 baud modem :)

Axel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,941
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top