Cute caps

jdavidb on 2010-08-19T21:55:41

I'm doing some quick code generation (the output is Java), and I found myself writing the below routine. I like it because of the names I picked for the variables. Not exactly self-documenting (although it is when you think about it), but this is throwaway. You can probably tell what the code is doing and why I named the variables as I did, and you might be entertained.

sub uc_prop
{
  my($prop) = @_;
  my $p = substr($prop, 0, 1);
  my $P = uc($p);
  my $rop = substr($prop, 1);
  return "$P$rop";
}


You do know Perl has ucfirst, right?

xsawyerx on 2010-08-19T22:50:39

$prop = ucfirst $prop;

Re:You do know Perl has ucfirst, right?

jdavidb on 2010-08-20T16:18:52

No, I didn't. Thanks!

Whoa

ank on 2010-08-23T22:50:10

LOL!

And you were leaving comments on perl 5 and 6 replying to my criticisms of the language? Maybe its time to change your sig:

"J. David works really hard reimplementing bult-in functions"

Third-rate programmers, unite!

Re:Whoa

jdavidb on 2010-08-25T12:34:43

J. David writes a lot of one-off crap and wishes he wrote Perl full-time...

My signature was a quote from someone here about me that was just so incredibly complimentary that I grabbed it and kept it. I suppose keeping it was a little pompous, but you know, it really built me up. And reminds me to try to be a good programmer even when circumstances aren't always conducive.

To that end, even my one-off crap used strict and warnings, but I guess it still doesn't get me out of the guilt from reinventing a wheel. Has ucFirst been around forever, or was it something that came around after I quit doing Perl full-time in 2007? (Hoping for a little reprieve. :P :) )

Re:Whoa

ank on 2010-08-25T13:02:04

Hey, I think its a really nice quote; personally I wouldn't use it in my sig - maybe just quote it in my website or something, dunno. I would treasure it just like you did, though.

ucfirst() has been around for ages, afaik. At least a decade, possibly much more.

Why did you quit doing Perl full time? Was it a decision or did the market push you to it? Just curious. Do you actually want to go back to writing Perl full-time?

I am currently doing Objective-C most of the time, and even though I miss some of the ease with which you can process strings in Perl, I'm quite happy with the way things are.

-- ank

Re:Whoa

jdavidb on 2010-08-26T15:03:25

I changed jobs in 2007 in order to be able to move my family out close to my inlaws. In my previous job I was doing Perl with pressure to switch to Java. I traded that for doing Java after all, but closer to where I wanted to be, in a business environment that is somewhat saner.

Re:Whoa

ank on 2010-08-27T02:31:12

Similar to what I went through around 2005/2006. Except that what ended up deciding was how much more Java programmers were able to make, especially if they sported the magic "J2EE" incantations. Boy, was I wrong. When I first saw EJBs I knew it was time to get a new job.

And for fun:

Aristotle on 2010-08-26T15:18:42

sub uc_prop { # inefficient
  my $t = reverse shift;
  my $h = uc chop $t;
  return $h . reverse $t;
}

(Of course you can do the same thing more efficiently – and boringly:)

sub uc_prop {
  my $t = shift;
  my $h = uc substr $t, 0, 1, '';
  return $h . $t;
}

(The following one might be more efficient. Or it may not be.)

sub uc_prop {
  my $s = shift;
  my $h = uc substr $s, 0, 1, '';
  substr $s, 0, 0, $h;
  return $s;
}

(The first one is cool though.)