I am looking at a pre-release version of this book. It stopped short of demonstrating uses of these 3 cases. Books says: In rare circumstances, I show some code below.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.