skinnymofo has asked for the wisdom of the Perl Monks concerning the following question:

Taking a look at DBI.pm (v. 1.14), in the dump_results subroutine at or around line 612, there is the following line of code:
...snip...
print $fh $lsep if $rows++ and $lsep;
...snip...
My question is, what does this say?

Thanks,
skinny

Replies are listed 'Best First'.
•Re: How should I read print .. if .. and ?
by merlyn (Sage) on Aug 19, 2002 at 19:17 UTC
    $ perl -MO=Deparse,-p -e 'print $fh $lsep if $rows++ and $lsep;' ((($rows++) and $lsep) and print($fh $lsep)); -e syntax OK $
    So, increment $rows, and if that was previously true, and $lsep is also true, then print $lsep to filehandle $fh.

    Deparse is your friend. Learn to use deparse.

    -- Randal L. Schwartz, Perl hacker

Re: How should I read print .. if .. and ?
by Anonymous Monk on Aug 19, 2002 at 19:08 UTC
    The form of the statement in question is
    EXPR_1 if EXPR_2 and EXPR_3;

    EXPR_1 will be evaluated if both EXPR_2 and EXPR_3 evaluate to true values.

Re: How should I read print .. if .. and ?
by higle (Chaplain) on Aug 19, 2002 at 19:11 UTC
    Basically, it's Perl shorthand for this:
    if ($rows++ and $lsep) { print $fh $lsep; }
    Meaning, if the two conditions are true, print $fh and $lsep print $lsep to filehandle $fh.

      higle

    Update: Doh! Wasn't paying attention at all ($fh should have tipped me that it was a filehandle :c\). Thanks for the correction, RMGir!
      ...print $fh and $lsep.

      Close. It means print $lsep to the file pointed to by filehandle $fh.

      Printing $fh and $lsep would be:

      print $fh, $lsep; # note the comma!

      --
      Mike