Lukas Mai > Switch-Plain-0.02 > Switch::Plain

Download:
Switch-Plain-0.02.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.02   Source  

NAME ^

Switch::Plain - a simple switch statement for Perl

SYNOPSIS ^

  use Switch::Plain;
  
  # string version
  sswitch (get_me_a_string()) {
    # return value of get_me_a_string() is bound to $_ in this block
  
    case 'foo': {
      # runs if $_ equals 'foo'
    }
  
    case 'bar': {
      # runs if $_ equals 'bar'
    }
  
    case 'melonlord' if $DEBUG: {
      # runs if $_ equals 'melonlord' and $DEBUG is true
    }
  
    default if $VERBOSE > 1: {
      # runs if nothing else matched so far and $VERBOSE is greater than 1
    }
  
    default: {
      # runs if nothing else matched so far
    }
  }
  
  # number version
  nswitch (get_me_a_number()) {
    # return value of get_me_a_number() is bound to $_ in this block
  
    case 1: {
      # runs if $_ equals 1
    }
  
    case 2: {
      # runs if $_ equals 2
    }
  
    case 99 if $DEBUG: {
      # runs if $_ equals 99 and $DEBUG is true
    }
  
    default if $VERBOSE > 1: {
      # runs if nothing else matched so far and $VERBOSE is greater than 1
    }
  
    default: {
      # runs if nothing else matched so far
    }
  }

DESCRIPTION ^

This module provides (yet another) switch statement for Perl. Differences between this module and Switch include:

Syntax

This module understands the following grammar:

  switch_statement := switch_keyword switch_scrutinee switch_body

  switch_keyword := 'sswitch' | 'nswitch'

  switch_scrutinee := '(' EXPR ')'

  switch_body := '{' case_clause* '}'

  case_clause := case_pattern+ BLOCK

  case_pattern := case_keyword case_modifier? ':'

  case_keyword := 'default' | 'case' EXPR

  case_modifier := 'if' EXPR | 'unless' EXPR

*, +, and ? have their usual regex meaning; BLOCK and EXPR represent standard Perl blocks and expressions, respectively.

Semantics

The meaning of a switch statement is given by the following translation rules:

Here's an example demonstrating all combinations:

  sswitch (SCRUTINEE) {
    case FOO0: {
      BODY0
    }

    case FOO1:
    case FOO2 if BAR1:
    case FOO3 unless BAR2:
    default if BAR3:
    default unless BAR4: {
      BODY1
    }

    default: {
      BODY2
    }
  }

This is equivalent to:

  do {
    # temporarily alias $_ to SCRUTINEE within this block:
    local *_ = \SCRUTINEE;

    if ($_ eq FOO0) {
      BODY0
    }
    elsif (
        $_ eq FOO1 ||
        ($_ eq FOO2 && BAR1) ||
        ($_ eq FOO3 && !BAR2) ||
        BAR3 ||
        !BAR4
    ) {
      BODY1
    }
    elsif (1) {
      BODY2
    }
  };

Differences between Switch::Plain and C's switch

Scoping

This module is a lexical pragma, i.e. the effects of use Switch::Plain (turning sswitch and nswitch into keywords) are scoped to the innermost enclosing block (or the whole file if there is no enclosing block).

If you are a module author who wants to wrap Switch::Plain from another module, simply call Switch::Plain->import from your own import method. It will affect whatever scope is currently being compiled (i.e. your caller).

AUTHOR ^

Lukas Mai, <l.mai at web.de>

COPYRIGHT & LICENSE ^

Copyright 2012-2013 Lukas Mai.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

syntax highlighting: