SPUG: How to use database variable in place of other variable

dancerboy dancerboy at strangelight.com
Fri Mar 15 00:21:02 CST 2002

At 12:10 PM -0800 3/14/02, Matt Tucker wrote:
>Another comment I had was on the sample code:
>     my $email = "$row[12]";
>I see this sort of thing done frequently, and I consider it bad style.
>I'm not sure how much extra work it creates for Perl (probably just a
>string copy, and maybe it's optimized out), but there's really no point
>in including those double quotes, since you'll get exactly the same
>thing without them.


I agree that this is bad style.  However, it is not *strictly* true 
that "you'll get exactly the same thing without them".  If $row[12] 
is undefined, then the semantics will be slightly different depending 
on whether you're interpolating into a double-quoted string or not. 
Consider this:

     undef @row;
     my $email_1 = $row[12];
     my $email_2 = "$row[12]";

$email_1 will be undefined, but $email_2 will contain the empty 
string.  There is, as most of you know, a difference.

But again, I do agree that this is bad style.  If your intent is to 
make sure that undef values get turned into empty strings, then you 
should do so more explicitly, e.g.:

     my $email = $row[12];
     $email = '' unless defined $email;

or perhaps:

     my $email = ( defined($row[12]) ? $row[12] : '' );

(And yes, I know that the parentheses in the above statement are 
unnecessary.  But personally, one of my biggest pet peeves is 
developers who won't use extra parentheses to clarify their code.  I 
consider the following to be atrocious style:

     my $email = defined $row[12] ? $row[12] : '';

IMO, you should always, in all but the most trivial and obvious of 
cases, use parentheses to make the order of operations explicit. 
Real developers have better things to do with their brain-cells than 
memorize operator precedence.)


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

More information about the spug-list mailing list