Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl flawed or my fault

Reply
Thread Tools

perl flawed or my fault

 
 
paul
Guest
Posts: n/a
 
      10-03-2006
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.

 
Reply With Quote
 
 
 
 
DJ Stunks
Guest
Posts: n/a
 
      10-03-2006
paul wrote:
> 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

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-03-2006
paul wrote:
>
> 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
--
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
 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      10-03-2006
"John W. Krahn" <(E-Mail Removed)> writes:
> 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
 
Reply With Quote
 
paul
Guest
Posts: n/a
 
      10-03-2006
Thank you everyone, I see the mistake already.

John W. Krahn wrote:
> paul wrote:
> >
> > 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
> --
> 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


 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      10-03-2006
Eric Schwartz <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> "John W. Krahn" <(E-Mail Removed)> writes:


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


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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-03-2006
Eric Schwartz wrote:
> "John W. Krahn" <(E-Mail Removed)> writes:
>>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';
> }


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

perldoc perlstyle



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
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      10-03-2006
<(E-Mail Removed)-berlin.de> wrote in comp.lang.perl.misc:
> Eric Schwartz <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > "John W. Krahn" <(E-Mail Removed)> writes:


[...]

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


Five!

> I can never bring myself to use it.

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-03-2006
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de wrote:
> Eric Schwartz <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>"John W. Krahn" <(E-Mail Removed)> writes:

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

>
> 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
--
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
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      10-03-2006
John W. Krahn <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Most Java apps are flawed on my system stax Java 45 02-10-2006 04:09 PM
2.0 Processiing Model Seriously Flawed? clintonG ASP .Net 9 09-05-2005 02:59 PM
Flawed logic =?Utf-8?B?YnN0b2RkYXJ0?= ASP .Net 4 08-02-2004 01:43 PM
All Versions of Windows are Seriously Flawed -Microsoft Tech Computer Support 0 07-21-2004 08:18 PM
MSDN Examples flawed Stamen Gortchev ASP .Net 11 09-29-2003 08:34 AM



Advertisments