Search code examples
perlswitch-statementmason

Switch-Case syntax in Mason


I am looking for the switch-case syntax in Mason, but haven't been able to find one.

Could someone please assist with a sample here?

Thanks in advance.


Solution

  • Saket, the following works fine on my Mason system. It is HTML::Mason ver. 1.48 running on Perl 5.14 and Apache 2.2.22.

    The switch-like statements given/when are available after Perl 5.10.1, (see here), as has been pointed out already in the other posting from Matteo.

    test.html (rename to test.mas if your server configuration requires this).

    <html>
      <head></head>
      <body>
    % my $name = 'Wittig';
      <h3>Hello <% $name %>! Ist this your <% swordname($name) %>?</h3>
      </body>
    </html>
    
    <%perl>
     sub swordname  {
       my $hero = shift;
       my $sword;
       given($hero) {
          when (/^Luno/)      { $sword = 'Fingal'   }
          when (/^Ecke/)      { $sword = 'Eckesax'  }
          when (/^Artus/)     { $sword = 'Excalibur'}
          when (/^Wittig/)    { $sword = 'Mimung'   }
          when (/^Ortnit/)    { $sword = 'Rosen'    }
          when (/^Sigurd/)    { $sword = 'Gram'     }
          when (/^Siegmund/)  { $sword = 'Notung'   }
          when (/^Siegfried/) { $sword = 'Balmung'  }
          when (/^Dietrich/)  { $sword = 'Nagelring'}
          default:            { $sword = 'Sword'    }
       }
       return $sword;
     }
    </%perl>
    
    <%once>
     use feature "switch";
    </%once>
    


    The module Switch.pm which belonged to the Perl core modules before 5.14.0 (as David Cross noted in comment), did provide a more "switch-like" syntax but used a source filter. It can be still used after beeing installed by hand (cpan install Switch) but is not recommended. The example can be rewritten using Switch.pm:

    <html>
      <head></head>
      <body>
    % my $name = 'Wittig';
      <h3>Hello <% $name %>! Ist this your <% swordname($name) %>?</h3>
      </body>
    </html>
    
    <%perl>
     sub swordname  {
       my $hero = shift;
       switch($hero) {
          case 'Luno'      { return 'Fingal'   }
          case 'Ecke'      { return 'Eckesax'  }
          case 'Artus'     { return 'Excalibur'}
          case 'Wittig'    { return 'Mimung'   }
          case 'Ortnit'    { return 'Rosen'    }
          case 'Sigurd'    { return 'Gram'     }
          case 'Siegmund'  { return 'Notung'   }
          case 'Siegfried' { return 'Balmung'  }
          case 'Dietrich'  { return 'Nagelring'}
          else             { return 'Sword'    }
       }
     }
    </%perl>
    
    <%once>
     use Switch;
    </%once>
    

    But the most simple "equivalent" of a switch that depends only on a fixed key, would, of course, be a %hash:

    <html>
      <head></head>
      <body>
    % my $name = 'Wittig';
      <h3>Hello <% $name %>! Ist this your <% $NameSword{$name} %>?</h3>
      </body>
    </html>
    
    <%once>
     my %NameSword = (
       Luno      => 'Fingal',
       Ecke      => 'Eckesax',
       Artus     => 'Excalibur',
       Wittig    => 'Mimung',
       Ortnit    => 'Rosen',
       Sigurd    => 'Gram',
       Siegmund  => 'Notung',
       Siegfried => 'Balmung',
       Dietrich  => 'Nagelring'
     );
    </%once>
    

    All three variants produce the same output. Maybe I mixed up the sword names (will correct if necessary).