perl flawed or my fault

P

paul

Hello all,

please help me the check the following code

use strict;

my $something = 'phal';
my $otherthing = 'paul';

my $note_whom = 'szmuzu\'mi';

defined($something) ? $note_whom = $something : $otherthing =
'singapore';

print "$note_whom \n $otherthing \n";

The result is: singapore
singapore

I don't know why the scalar $note_whom get the value that is assigned
to $otherthing.

Please give me the explaination, and thank you.
 
D

DJ Stunks

paul said:
Hello all,

please help me the check the following code

use strict;

my $something = 'phal';
my $otherthing = 'paul';

my $note_whom = 'szmuzu\'mi';

defined($something) ? $note_whom = $something : $otherthing =
'singapore';

print "$note_whom \n $otherthing \n";

The result is: singapore
singapore

I don't know why the scalar $note_whom get the value that is assigned
to $otherthing.

Please give me the explaination, and thank you.

note precendence...

C:\tmp>perl -MO=Deparse,-p tmp.pl
use warnings;
use strict 'refs';
(my $something = 'phal');
(my $otherthing = 'paul');
(my $note_whom = q[szmuzu'mi]);
((defined($something) ? ($note_whom = $something) : $otherthing) =
'singapore');
print("$note_whom \n $otherthing \n");

HTH,
-jp
 
J

John W. Krahn

paul said:
please help me the check the following code

use strict;

my $something = 'phal';
my $otherthing = 'paul';

my $note_whom = 'szmuzu\'mi';

defined($something) ? $note_whom = $something : $otherthing =
'singapore';

print "$note_whom \n $otherthing \n";

The result is: singapore
singapore

I don't know why the scalar $note_whom get the value that is assigned
to $otherthing.

Please give me the explaination, and thank you.

Because of precedence you have:

(defined($something) ? $note_whom = $something : $otherthing) = 'singapore';

You need to add parentheses:

defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');



John
 
E

Eric Schwartz

John W. Krahn said:
Because of precedence you have:

(defined($something) ? $note_whom = $something : $otherthing) = 'singapore';

You need to add parentheses:

defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');

Or better yet, write it out:

if (defined($something)) {
$note_whome = $something;
} else {
$otherthing = 'singapore';
}

I always recommend against the ?: operator unless it's for something
small and self-contained. I avoid using it except for rvalues, unless
not using it will make things unacceptably ugly. Yes, this is a vague
standard. Here' an exampe of what is acceptable:

my $foo = $bar ? 0 : 1;

Here's something that isn't:

($var == 'foo' and $bar < 6 or $x > $y) ? ($this = something($else, 'maybe') or die "um, I dunno") : call_cows($potatoes);

-=Eric
 
P

paul

Thank you everyone, I see the mistake already.
Because of precedence you have:

(defined($something) ? $note_whom = $something : $otherthing) = 'singapore';

You need to add parentheses:

defined($something) ? ($note_whom = $something) : ($otherthing = 'singapore');



John
 
A

anno4000

Eric Schwartz said:
Or better yet, write it out:

if (defined($something)) {
$note_whome = $something;
} else {
$otherthing = 'singapore';
}

I always recommend against the ?: operator unless it's for something
small and self-contained. I avoid using it except for rvalues, unless
not using it will make things unacceptably ugly. Yes, this is a vague
standard. Here' an exampe of what is acceptable:

my $foo = $bar ? 0 : 1;

Here's something that isn't:

($var == 'foo' and $bar < 6 or $x > $y) ? ($this = something($else,
'maybe') or die "um, I dunno") : call_cows($potatoes);

One point about ternary ?: is that it is an expression that has a
value, not primarily a means of flow control. The value should be
the main point if the construct is used, not the side-effects of
the code in the branches. In my view, small and self-contained
enters the image only in so far as it can be hard to find a readable
format for arbitrarily nested ?: with big complex branches. If
such a format can be found, there is no limit to the complexity.

I am of a split mind about using ?: in lvalue context. It *is*
kind of obscure, but there are situations that simply call for
it. One is the loop of a binary search, for example

while ( $low < $high - 1 ) {
my $mid = ($low + $high)/2;
$list->[ $mid] le $targ ? $low : $high = $mid;
}

The if-else equivalent to ?: is four lines, even cuddling the else.
I can never bring myself to use it.

Anno
 
J

John W. Krahn

Eric said:
Or better yet, write it out:

if (defined($something)) {
$note_whome = $something;
} else {
$otherthing = 'singapore';
}

Or better yet, don't use "cuddled else". :)

perldoc perlstyle



John
 
J

John W. Krahn

Eric Schwartz said:
Or better yet, write it out:

if (defined($something)) {
$note_whome = $something;
} else {
$otherthing = 'singapore';
}

I always recommend against the ?: operator unless it's for something
small and self-contained. I avoid using it except for rvalues, unless
not using it will make things unacceptably ugly. Yes, this is a vague
standard. Here' an exampe of what is acceptable:

my $foo = $bar ? 0 : 1;

Here's something that isn't:

($var == 'foo' and $bar < 6 or $x > $y) ? ($this = something($else,
'maybe') or die "um, I dunno") : call_cows($potatoes);

One point about ternary ?: is that it is an expression that has a
value, not primarily a means of flow control. The value should be
the main point if the construct is used, not the side-effects of
the code in the branches. In my view, small and self-contained
enters the image only in so far as it can be hard to find a readable
format for arbitrarily nested ?: with big complex branches. If
such a format can be found, there is no limit to the complexity.

I am of a split mind about using ?: in lvalue context. It *is*
kind of obscure, but there are situations that simply call for
it. One is the loop of a binary search, for example

while ( $low < $high - 1 ) {
my $mid = ($low + $high)/2;
$list->[ $mid] le $targ ? $low : $high = $mid;
}

The if-else equivalent to ?: is four lines, even cuddling the else.
I can never bring myself to use it.

I assume that you meant to write:

( $list->[ $mid] le $targ ? $low : $high ) = $mid;

because your example never assigns a value to $low.



John
 
A

anno4000

John W. Krahn said:
(e-mail address removed)-berlin.de wrote:
while ( $low < $high - 1 ) {
my $mid = ($low + $high)/2;
$list->[ $mid] le $targ ? $low : $high = $mid;
}

The if-else equivalent to ?: is four lines, even cuddling the else.
I can never bring myself to use it.

I assume that you meant to write:

( $list->[ $mid] le $targ ? $low : $high ) = $mid;

because your example never assigns a value to $low.

Hmmm?

perl -le '$_ ? $low : $high = 123 for 0, 1; print "$low $high"'

prints

123 123

showing that both are set.

Anno
 
E

Eric Schwartz

John W. Krahn said:
Or better yet, don't use "cuddled else". :)

perldoc perlstyle

Feh. I like it that way; perlstyle can go hang. I think it's
ugly. (Unless, of course, it's house style, in which case I follow
house style.)

-=Eric
 
E

Eric Schwartz

I am of a split mind about using ?: in lvalue context. It *is*
kind of obscure, but there are situations that simply call for
it. One is the loop of a binary search, for example

while ( $low < $high - 1 ) {
my $mid = ($low + $high)/2;
$list->[ $mid] le $targ ? $low : $high = $mid;
}

The if-else equivalent to ?: is four lines, even cuddling the else.
I can never bring myself to use it.

I don't object to that so much, but I'd still use the if-else
equivalent (and cuddle the else too! Bwahahahah!) just because I
think it's more readable.

-=Eric
 
J

John W. Krahn

John W. Krahn said:
(e-mail address removed)-berlin.de wrote:
while ( $low < $high - 1 ) {
my $mid = ($low + $high)/2;
$list->[ $mid] le $targ ? $low : $high = $mid;
}

The if-else equivalent to ?: is four lines, even cuddling the else.
I can never bring myself to use it.
I assume that you meant to write:

( $list->[ $mid] le $targ ? $low : $high ) = $mid;

because your example never assigns a value to $low.

Hmmm?

perl -le '$_ ? $low : $high = 123 for 0, 1; print "$low $high"'

prints

123 123

showing that both are set.

Yes, interesting, even though the Fine Manual states:

<QUOTE>
The operator may be assigned to if both the 2nd and 3rd arguments are
legal lvalues (meaning that you can assign to them):

($a_or_b ? $a : $b) = $c;

Because this operator produces an assignable result, using assignments
without parentheses will get you in trouble.
</QUOTE>



John
 
A

anno4000

John W. Krahn said:
John W. Krahn said:
(e-mail address removed)-berlin.de wrote:

while ( $low < $high - 1 ) {
my $mid = ($low + $high)/2;
$list->[ $mid] le $targ ? $low : $high = $mid;
}

The if-else equivalent to ?: is four lines, even cuddling the else.
I can never bring myself to use it.
I assume that you meant to write:

( $list->[ $mid] le $targ ? $low : $high ) = $mid;

because your example never assigns a value to $low.

Hmmm?

perl -le '$_ ? $low : $high = 123 for 0, 1; print "$low $high"'

prints

123 123

showing that both are set.

Yes, interesting, even though the Fine Manual states:

<QUOTE>
The operator may be assigned to if both the 2nd and 3rd arguments are
legal lvalues (meaning that you can assign to them):

($a_or_b ? $a : $b) = $c;

Because this operator produces an assignable result, using assignments
without parentheses will get you in trouble.
</QUOTE>

The parentheses in the Fine Manual are bogus then. It's the other
way 'round, you have precedence problems when you want assignments
in the branches of the expression

$a_or_b ? $a = 123 : $b = 456;
is
($a_or_b ? $a = 123 : $b) = 456;

so the intended value of 123 for $a will be overwritten by 456 in
the "true" case. At least the second assignment (to $b) needs
parens. For symmetry I'd probably write

$a_or_b ? ( $a = 123) : ( $b = 456);

if at all. After all, it's a case where only the side-effects
count.

Anno
 
T

Tad McClellan

Eric Schwartz said:
I'd still use the if-else
equivalent (and cuddle the else too! Bwahahahah!) just because I ^
think it's more readable.
^^^^^

If you are the only person who reads your code, then that is
a very sensible criterion.

You are in an enviable position. Most of us have to consider
other programmers when we write programs, where the criterion
is what do most people think is more readable.
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
John W. Krahn
I am of a split mind about using ?: in lvalue context. It *is*
kind of obscure, but there are situations that simply call for
it. One is the loop of a binary search, for example

while ( $low < $high - 1 ) {
my $mid = ($low + $high)/2;
$list->[ $mid] le $targ ? $low : $high = $mid;
}
The if-else equivalent to ?: is four lines, even cuddling the else.
I can never bring myself to use it.
I assume that you meant to write:

( $list->[ $mid] le $targ ? $low : $high ) = $mid;

because your example never assigns a value to $low.

This discussion shows my RULE ONE of Perl:

Do not ever try to memorize the relative precedence of operators.

If you religeously follow this advice, then the following RULE TWO
will help you a lot:

If in any doubt, put parentheses.

Myself, I still manage to follow the rule one. I DO NOT know relative
precedence of + vs &, or of = vs &, or of =~ vs =. This is a useless
knowledge with very dear consenquences if you misremember...

What is important to have clean readable text of a program:

while vs or/and;
or/and vs everything else;
the "logical" part of arithetic operations.

All the rest deserves blissfulness. What is (length 8 + 2)?

Hope this helps,
Ilya
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top