perl is (now) more DWIMmy

geoff on 2004-07-07T01:17:05

I just found out (via Stas) that more recent perl's have gotten a bit more DWIMmy.



a common mod_perl error used to look like this:

  $r->custom_response(SERVER_ERROR => 'yikes');


the (difficult to spot) problem is that => makes the SERVER_ERROR constant (500) into the string "SERVER_ERROR". so, you needed to use a comma instead, making the code look like this:

$r->custom_response(SERVER_ERROR, 'yikes');


well, with mod_perl 2.0 and perl-5.8.0 and later, you can use the => operator and still get your constant, so

$r->custom_response(Apache::SERVER_ERROR => 'yikes');


will now work in mod_perl 2.0-land. well, most of the time...



as it turns out, there is a caveat: the constant needs to be defined at least one level deep, meaning that if you imported the constants into your own namespace instead of the Apache:: namespace (as mod_perl 2.0 will let you) this

$r->custom_response(SERVER_ERROR => 'yikes');


still doesn't work. oh, well.



for those that don't give a hoot about mod_perl, (or are otherwise thus far confused), give this script a whirl with a few different perl versions:

package My;
                                                                                                                             
use constant FOO => 0;  # My::FOO
                                                                                                                             
package main;
                                                                                                                             
use constant FOO => 1; # main::FOO
                                                                                                                             
args(FOO => 'arrow');
args(FOO, 'comma');
                                                                                                                             
args(main::FOO => 'arrow');
args(main::FOO, 'comma');
                                                                                                                             
args(My::FOO => 'arrow');
args(My::FOO, 'comma');
                                                                                                                             
sub args {
  print join ':', @_, "\n";
}


no constant

perrin on 2004-07-07T16:28:08

Perl's constants are just a total pain. There are so many ways to accidentally break them: using them as a hash key, putting them in a quoted string, etc. It's just not worth it in my opinion. I just use regular old scalars, all caps.

However, you can make the arrow operator stuff work by calling SERVER_ERROR as SERVER_ERROR().