tags:

views:

435

answers:

11

Recently on the Perl5 Porters list a spelling error in a doc patch made me laugh: use warnins; I now feel compelled to write a "warnins" pragma which such sage advice as

Yer usin' a variable that ain't got a value in addition (+) at z.pl line 8.

instead of

Use of uninitialized value in addition (+) at z.pl line 8.

What other warning messages would you like to see modified, and what should they say?

Version 0.0.3 of the warnin's pragma is now available.
Version 0.0.4 is now available through github.

A: 

Not PHP related, but Checkstyle reports on our code and gives us warnings like:

'101' is a magic number.

I'd like to see something like:

'101' is a magical number and may only be used by the most careful of wizards.

I think it would make me smile.

Greg Noe
This is a Perl question, not a PHP question. :P
Chris Lutz
Oh, I read p5p as php. So much for all them years of schoolin'.
Greg Noe
I did the same thing, and then checked the link and saw Perl in the URL. It happens to the best of us (which I am not).
Chris Lutz
+4  A: 

Unquoted string "haha" may clash with future reserved word at z.pl line 8.

Change to:

I got dibs on "haha". Pick yer own name at z.pl line 8.

Chris Lutz
+3  A: 

I got another one! This fits in with the name of everyone's favorite website, too!

Deep recursion on subroutine "main::haha" at z.pl line 8.

Change to:

Man, I'm gettin' a headache in subroutine "main::haha" at z.pl line 8.

(The code to produce this error is sub haha { &haha } &haha; What's everybody's favorite error?)

Chris Lutz
I'd go with "Yer gettin' in way too deep in that there subroutine ..."
runrig
+4  A: 

You can git your list o' warnings at perldoc perldiag.

Deep recursion on subroutine "%s"

becomes:

Whoa there "%s"! Don't be running in circles like chickin with it's head cut off.

And:

Bareword found in conditional

becomes:

Might wanna put your clothes on. Maybe.

Jon Ericson
+1  A: 

doing this right would involve creating a gettext catalog for perl warnings, so "use warnins" would simply load it. Maybe perl::critic could go gettext first, and warnins could be a critic catalog?

There is doing it right, and there is doing it for fun. Once the right groundwork is done, doing it right will be an option. For now: http://codepad.org/WaTjx0SU
Chas. Owens
+1  A: 
package warnins;

use strict;
no  warnings;
use Scalar::Util qw'reftype';

*import   = warnings::import;
*unimport = warnings::unimport;

my %jokes = (
  '^Use of uninitialized value'     => "Yer usin' a variable that ain't got a value",
  '^Argument (".*") isn\'t numeric' => [
    'Suffering sukkatash! Ya used da strin\' "$1"',
    'Even I know that "$1" ain\'t no number'
  ],
);


$SIG{__WARN__} = sub {
  my $message = join '', @_;

  for my $joke (keys %jokes) {
    if( $message =~ /$joke/ ){
      my $replace = $jokes{$joke};

      if( reftype $replace eq 'ARRAY' ){
        $replace = $replace->[ rand(@$replace) ];
      }

      $message =~ s/$joke/$replace/ee;

      last;
    }
  }

  warn $message;
};
Brad Gilbert
Out of curiosity, is there any reason to use shuffle() instead of just using rand() to get a random element from the array? Like: ${$replace}[rand(@$replace)]
Chris Lutz
Also, why use reftype() instead of just the builtin ref()?
Chris Lutz
reftype is better for this sort of thing because it returns the type of the variable, whereas ref returns the type of unblessed variables and the class of blessed ones. I have no idea why he is using shuffle instead of rand.
Chas. Owens
Although, now that I look at it, we don't really care if it is an array or not, just whether it is reference or not.
Chas. Owens
I used reftype mainly to future proof the code, which is probably overkill.
Brad Gilbert
Basically I wanted to be able to have more than one joke for each warning. Which it appears as though it is in the current release, so my work here is done.
Brad Gilbert
+3  A: 

If you call the pragma warnin::s, then you can use it like this:

use warnin's;

The old package delimiter is only good for bad jokes.

daotoad
I hadn't thought of that, but you have a good point. And it will prevent people from using it by accident; however, it can wait until I get the rest of the module put together.
Chas. Owens
it would be funnier if someone would use this by accident :)
Alexandr Ciornii
+5  A: 

Where I think some of these are going wrong is that they don't actually convey the semantic content of the original warning. IMO it should be more like:

Warning: something’s wrong

becomes

Warning: WHAT the HECK


Bareword "%s" refers to nonexistent package

becomes

I got no damn idear what this "%s" sposta be


\1 better written as $1

becomes

do it the Perl way, son: $1, not \1


close() on unopened filehandle %s

becomes

Yer tryna close() %s what ain't never been opened
chaos
As soon as I get the module to pass tests I will add these
Chas. Owens
I can't get "\1 better written as $1" to trip in my tests, I tried $s =~ s/(foo)/\1\1/; which is what I would expect to trip it, but that did not throw a warning.
Chas. Owens
Odd. Triggered for me on this: my $x = 'foo'; $x =~ s/f(.)/x\1/;
chaos
+2  A: 

Change something's wrong to somethin' ain't right.

Change BEGIN failed--compilation aborted to I can't even get started. Screw it.

Change Deep recursion on subroutine "%s" to We're goin' t'hell in a handbasket named "%s"

Change Died to Kicked the bucket.

Change Out of memory! to What was I talking about again?

Change panic to Oh sh*t!

Michael Carman
A: 

Yer forgot a semicolon, I've added it for ya. Try and get it right next time.

Meski
What warning message is this supposed to replace?
Chas. Owens
A: 

Attempt to set length of freed array
--> Yer tryin' to set the length of an array dat's been freed

Can't call method "%s" in empty package "%s"
--> Ain't no way you gonna call method "%s" in this empty package "%s"

Died
--> Ach, I am slain

Document contains no data
--> Ain't nothing in the document, pardner

And similarly all the other "Can't call...", "Can't do..." messages can be phrased similarly:

Can't do inplace edit: %s would not be unique
--> Ain't no way you can do an inplace edit here: %s wouldn't be special no more

Can't do setuid
--> Ain't no way you can setuid

Ether
'Ach, I am slain"? That sounds like a line from a bad Wagner imitation.
Michael Myers