Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • No, I wouldn't use a switch-like construct there. What's the point of a label when there's a much cleaner solution? I would probably use a dispatch table like the following:

    my %dispatch = (
      foo => \&foo,
      bar => \&bar,
      baz => \&baz


    if ( exists $dispatch{$function} ) {
    else {
      die "No such function ($function)";

    I don't know, maybe it's just me, but that seems much cleaner and

    • by KM (4) on 2003.01.29 19:36 (#16515) Journal
      Well, I use dispatch tables. But it hasn't caught on with most co-workers. Anyways, the point is trying to get a good way to explain to these people that the way they use labels is wrong. The examples I showed, although not the best way to do things (aside the nested loop), are more 'valid' ways to use labels. Dispatch tables are better than 'goto LABEL', but I show that as an example of a more proper usage. Basically, because people at my work seem to think that when I show them good programming practices, I only do it because I personally think it is better style, as opposed to better programming practices. So, that's a barrier I am dealing with.

      I need a good way to show that how they use them (around regular if/elsif conditionals) is Bad. I tell them over and over, I explain it over and over, but no dice. Normally, I wouldn't care if they don't get it. The more piss poor Perl programmers out there means more chances of me getting jobs ;-) But, I have to also maintain this ugly cargo-cult code :-(

      • I humbly submit an example of when SWITCH can go horribly wrong. The temptation to use labels for control flow when they are not required makes bugs like this more likely.

         while ( my $data = $t_sth->fetchrow_arrayref ) {
           my ( $amt, $id ) = @$data;
           $amt /= PRECISION;

           SWITCH: {
             $id == $CASH        && ($tcash        += $amt) && last SWITCH;
             $id == $ACCOUNT

        • Yes, I can try this one. I saw the bug when I saw the code. Maybe this one will show why they should avoid labels (although labels are good in deeply nested loops, they don't do that anyways).

          Maybe I can turn this into a "THAT IS FIRE, DO NOT TOUCH THE FIRE" ;-)

        • $id == $CASH && ($tcash += $amt) && last SWITCH;

          I saw the "bug" when I saw the code, but then I thought "the programmer wouldn't have used that construct if ($tcash += $amt) could ever yield zero!".

          I like pie.