CASE 1: Adding "0" (zero) to a Perl string variable is the fastest way that I know of to "delete leading zeroes". If I do that, I add a comment that explains it. This technique basically allows the use of a simple "print" instead of "printf" for output. But note that $x gets printed whether or not you immediately realize that the leading zeroes are gone or not.
CASE 2: Using '.' to force scalar context. This technique is seen far more often than any of the other two techniques. I don't have strong opinion about it one way or the other. I use both ways depending upon the situation. Sometimes shortening the line by removing "scalar" aides understandability.
CASE 3: This is different because the subroutine is_input_valid() is a more normal situation. I can tell that some Monks strongly disagree with me. Fine if you do. I argue that this is a situation where having an "if" statement is far more understandable and means nothing in terms of the execution efficiency of the code. The ease of understanding is worth the extra typing. !!$y is just not necessary for reasons of either understandability or efficiency.
As has been pointed out in other posts, there can be more complicated things for $y other than a simple scalar, and I would say all the more reason to have some extra logic.
#!/usr/bin/perl -w use strict; # CASE 1: # use +0 to "delete leading zeros" # avoids having to use a printf %d format # or some kind of slow substitution statement # my $x = "00012"; print "$x\n"; #prints 00012 printf "%d\n",$x; #prints 12 $x+=0; #delete leading zeroes print "$x\n"; #prints 12 # CASE 2: # use '.' concatentation to # avoid having to use an # explict scalar() function # print "Number values= ", scalar(return_array()),"\n"; # prints 2 print "Number values= " .return_array(),"\n"; # prints 2 # CASE 3: # use !! to avoid using an "if" statement # my $y = "abx"; print $y ? 'T': 'F', "\n"; #prints T print $y ? 1 : 0, "\n"; #prints 1 print !!$y, "\n"; #prints 1 ??good idea?? print ''.is_input_valid(),"\n"; #prints 1 sub return_array { my @x = (12,23); return @x; } sub is_input_valid { my $f_validated_input =0; # more code here... $f_validated_input = 1; return $f_validated_input; }
In reply to Re^2: Anybody use !!
by Marshall
in thread Anybody use !!
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |