Thread: Small plperl documentation patch
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 NotDashEscaped: You need GnuPG to verify this message Using $a and $b for temporary vars are bad form for our example, as they have special meaning in Perl. Index: plperl.sgml =================================================================== RCS file: /projects/cvsroot/pgsql/doc/src/sgml/plperl.sgml,v retrieving revision 2.46 diff -r2.46 plperl.sgml 121,124c121,124 < my ($a,$b) = @_; < if (! defined $a) { < if (! defined $b) { return undef; } < return $b; --- > my ($x,$y) = @_; > if (! defined $x) { > if (! defined $y) { return undef; } > return $y; 126,128c126,128 < if (! defined $b) { return $a; } < if ($a > $b) { return $a; } < return $b; --- > if (! defined $y) { return $x; } > if ($x > $y) { return $x; } > return $y; -- Greg Sabino Mullane greg@turnstep.com PGP Key: 0x14964AC8 200510181742 http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 -----BEGIN PGP SIGNATURE----- iD8DBQFDVWyGvJuQZxSWSsgRApOzAJ9edQHEDRysEx0nLlI0hV7v1bZikQCgyVnU Rdouv5dEUTrW9TmZ4DExwjs= =mi+E -----END PGP SIGNATURE-----
Patch applied, thanks. I actually told somebody off roundly about this not long ago. cheers andrew Greg Sabino Mullane wrote: > >Using $a and $b for temporary vars are bad form for our example, >as they have special meaning in Perl. > > [snip]
"Greg Sabino Mullane" <greg@turnstep.com> writes: > Using $a and $b for temporary vars are bad form for our example, > as they have special meaning in Perl. Please, tell me that isn't so. $a is a reserved name now? What was Larry thinking? regards, tom lane
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Tom Lane asked: > Please, tell me that isn't so. $a is a reserved name now? What was > Larry thinking? It's not that bad, really. $a and $b have special meaning inside of a sort subroutine as the two items being compared. Thus, you can write something like this: my @crooks = sort { lc $a cmp lc $b } @whitehouse; to create a list sorted by lowercase (albeit not in the most efficient matter). This will work even with "use strict" turned on, as $a and $b will not trip Perl's strict-mode checking of undeclared variables. So, the danger is very minor, but it's a good practice to not use them for temporary variables in a script. - -- Greg Sabino Mullane greg@turnstep.com PGP Key: 0x14964AC8 200510182220 http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 -----BEGIN PGP SIGNATURE----- iD8DBQFDVa2qvJuQZxSWSsgRAoMSAJ4i+TlQOYUw5w8VCLqsSKakfgaWfQCfeqGo n5Rn3XwUZZgwdHrs/7i3GNI= =/0mE -----END PGP SIGNATURE-----
Greg Sabino Mullane wrote: >Tom Lane asked: > > > >>Please, tell me that isn't so. $a is a reserved name now? What was >>Larry thinking? >> >> > >It's not that bad, really. $a and $b have special meaning inside of >a sort subroutine as the two items being compared. Thus, you can write >something like this: > >my @crooks = sort { lc $a cmp lc $b } @whitehouse; > > to create a list sorted by lowercase (albeit not in the most efficient >matter). This will work even with "use strict" turned on, as $a and $b >will not trip Perl's strict-mode checking of undeclared variables. So, >the danger is very minor, but it's a good practice to not use them for >temporary variables in a script. > > > > I recently lost quite some time tracking down a piece of spurious data persistence that eventually turned out to be due to use of an undeclared $a that strict mode failed to detect, so it's not so minor, really. In retrospect, the choice of names for sort comparison operands was poor, as I suspect Larry would agree, but it's been there ever since I have been using perl (around 12 years now, iirc). cheers andrew