On Mon, Nov 24, 2008 at 08:15, Mr. Shawn H. Corey <[EMAIL PROTECTED]> wrote: > On Mon, 2008-11-24 at 18:41 +0530, suresh kumar wrote: >> Thanks for the response. >> >> but is there anyother way to do this? >> > > You just got three answer all essentially the same. What's wrong with > them? snip
Well, because you are returning 1 or 0 you could use the bitwise and operator (&) since it doesn't use short circuit logic, but that would not be very maintainable (3 & 1 is true, but 2 & 1 is false). I would suggest using the example many other people gave you over this. sub a { print "i am a\n"; return 0; } sub b { print "i am b\n"; return 1; } if (a() & b()) { print "yes\n"; } else { print "no\n"; } Hmm, now that I think about it, it might be safer to say sub a { print "i am a\n"; return 0; } sub b { print "i am b\n"; return 1; } if (!!a() & !!b()) { print "yes\n"; } else { print "no\n"; } This at least tests the truthiness of a() and b(), but it is still not a nice thing to do to someone who reads your code later. There isn't even a performance gain (logical is faster than bitwise, who knew). bitwise 0 broken 0 logical 0 Rate bitwise broken logical bitwise 4858803/s -- -22% -43% broken 6199351/s 28% -- -27% logical 8498074/s 75% 37% -- bitwise 0 broken 0 logical 0 Rate bitwise broken logical bitwise 5687007/s -- -6% -33% broken 6053143/s 6% -- -29% logical 8495407/s 49% 40% -- bitwise 1 broken 1 logical 1 Rate bitwise broken logical bitwise 4626070/s -- -4% -35% broken 4828968/s 4% -- -32% logical 7126245/s 54% 48% -- bitwise 1 broken 0 logical 2 Rate bitwise broken logical bitwise 4077795/s -- -34% -46% broken 6175150/s 51% -- -18% logical 7502893/s 84% 22% -- #!/usr/bin/perl use strict; use warnings; use List::Util; use Benchmark; my %subs = ( broken => sub { my $bool = $a & $b; return $bool }, bitwise => sub { my $bool = !!$a & !!$b; return $bool }, logical => sub { my $bool = $a && $b; return $bool }, ); $a = 0; $b = 0; for my $sub (sort keys %subs) { print "$sub ", $subs{$sub}->(), "\n"; } Benchmark::cmpthese(-1, \%subs); print "\n"; $a = 0; $b = 1; for my $sub (sort keys %subs) { print "$sub ", $subs{$sub}->(), "\n"; } Benchmark::cmpthese(-1, \%subs); print "\n"; $a = 1; $b = 1; for my $sub (sort keys %subs) { print "$sub ", $subs{$sub}->(), "\n"; } Benchmark::cmpthese(-1, \%subs); print "\n"; $a = 1; $b = 2; for my $sub (sort keys %subs) { print "$sub ", $subs{$sub}->(), "\n"; } Benchmark::cmpthese(-1, \%subs); print "\n"; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/