Skip to content

Commit

Permalink
import Switch 2.07 from CPAN
Browse files Browse the repository at this point in the history
git-cpan-module:   Switch
git-cpan-version:  2.07
git-cpan-authorid: DCONWAY
git-cpan-file:     authors/id/D/DC/DCONWAY/Switch-2.07.tar.gz
  • Loading branch information
thoughtstream authored and schwern committed Dec 13, 2009
1 parent 7a2d0cd commit 87acfc3
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 138 deletions.
9 changes: 9 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,12 @@ Revision history for Perl extension Switch.
- Fixed the parsing of embedded POD (thanks Brent)

- Fixed bug encountered when -s or -m file test used (thanks Jochen)


2.07 Wed May 15 15:19:28 2002

- Corified tests

- Updated "Perl6" syntax to reflect current design
(as far as possible -- can't eliminate need to parenthesize
variables, since they're ambiguous in Perl 5)
4 changes: 2 additions & 2 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'Switch',
VERSION => '2.06',
NAME => q[Switch],
VERSION => q[2.07],
PREREQ_PM => { Filter::Util::Call => 0, Text::Balanced => 0 },

);
14 changes: 6 additions & 8 deletions README
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
==============================================================================
Release of version 2.06 of Switch
Release of version 2.07 of Switch
==============================================================================


Expand All @@ -26,16 +26,14 @@ COPYRIGHT

==============================================================================

CHANGES IN VERSION 2.06
CHANGES IN VERSION 2.07


- Fixed parsing of ternary operators in Switch'ed source code
(at the expense of no longer correctly parsing ?...? regexes)
(thanks Mark)
- Corified tests

- Fixed the parsing of embedded POD (thanks Brent)

- Fixed bug encountered when -s or -m file test used (thanks Jochen)
- Updated "Perl6" syntax to reflect current design
(as far as possible -- can't eliminate need to parenthesize
variables, since they're ambiguous in Perl 5)


==============================================================================
Expand Down
51 changes: 29 additions & 22 deletions Switch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use strict;
use vars qw($VERSION);
use Carp;

$VERSION = '2.06';
$VERSION = '2.07';


# LOAD FILTERING MODULE...
Expand All @@ -22,7 +22,6 @@ my ($Perl5, $Perl6) = (0,0);

sub import
{
$DB::single = 1;
$fallthrough = grep /\bfallthrough\b/, @_;
$offset = (caller)[2]+1;
filter_add({}) unless @_>1 && $_[1] eq 'noimport';
Expand Down Expand Up @@ -92,6 +91,7 @@ sub filter_blocks
|| $Perl6 && $source =~ /when|given/;
pos $source = 0;
my $text = "";
$DB::single = 1;
component: while (pos $source < length $source)
{
if ($source =~ m/(\G\s*use\s+Switch\b)/gc)
Expand All @@ -116,15 +116,20 @@ sub filter_blocks
}

if ($Perl5 && $source =~ m/\G(\n*)(\s*)(switch)\b(?=\s*[(])/gc
|| $Perl6 && $source =~ m/\G(\n*)(\s*)(given)\b(?=\s*[(])/gc)
|| $Perl6 && $source =~ m/\G(\n*)(\s*)(given)\b(?=\s*[(])/gc
|| $Perl6 && $source =~ m/\G(\n*)(\s*)(given)\b(.*)(?=\{)/gc)
{
my $keyword = $3;
my $arg = $4;
print STDERR "[$arg]\n";
$text .= $1.$2.'S_W_I_T_C_H: while (1) ';
@pos = Text::Balanced::_match_codeblock(\$source,qr/\s*/,qr/\(/,qr/\)/,qr/[[{(<]/,qr/[]})>]/,undef)
or do {
die "Bad $keyword statement (problem in the parentheses?) near $Switch::file line ", line(substr($source,0,pos $source),$line), "\n";
};
my $arg = filter_blocks(substr($source,$pos[0],$pos[4]-$pos[0]),line(substr($source,0,$pos[0]),$line));
unless ($arg) {
@pos = Text::Balanced::_match_codeblock(\$source,qr/\s*/,qr/\(/,qr/\)/,qr/[[{(<]/,qr/[]})>]/,undef)
or do {
die "Bad $keyword statement (problem in the parentheses?) near $Switch::file line ", line(substr($source,0,pos $source),$line), "\n";
};
$arg = filter_blocks(substr($source,$pos[0],$pos[4]-$pos[0]),line(substr($source,0,$pos[0]),$line));
}
$arg =~ s {^\s*[(]\s*%} { ( \\\%} ||
$arg =~ s {^\s*[(]\s*m\b} { ( qr} ||
$arg =~ s {^\s*[(]\s*/} { ( qr/} ||
Expand Down Expand Up @@ -171,7 +176,7 @@ sub filter_blocks
$text .= " $code)";
}
elsif ($Perl5 && $source =~ m/\G\s*(([^\$\@{])[^\$\@{]*)(?=\s*{)/gc
|| $Perl6 && $source =~ m/\G\s*([^:;]*)()/gc) {
|| $Perl6 && $source =~ m/\G\s*([^;{]*)()/gc) {
my $code = filter_blocks($1,line(substr($source,0,pos $source),$line));
$text .= ' \\' if $2 eq '%';
$text .= " $code)";
Expand All @@ -180,8 +185,8 @@ sub filter_blocks
die "Bad $keyword statement (invalid $keyword value?) near $Switch::file line ", line(substr($source,0,pos $source), $line), "\n";
}
die "Missing colon or semi-colon after 'when' value near $Switch::file line ", line(substr($source,0,pos $source), $line), "\n"
unless !$Perl6 || $source =~ m/\G(\s*)(:|(?=;))/gc;
die "Missing opening brace or semi-colon after 'when' value near $Switch::file line ", line(substr($source,0,pos $source), $line), "\n"
unless !$Perl6 || $source =~ m/\G(\s*)(?=;|\{)/gc;
do{@pos = Text::Balanced::_match_codeblock(\$source,qr/\s*/,qr/\{/,qr/\}/,qr/\{/,qr/\}/,undef)}
or do {
Expand Down Expand Up @@ -487,8 +492,8 @@ Switch - A switch statement for Perl
=head1 VERSION
This document describes version 2.06 of Switch,
released November 14, 2001.
This document describes version 2.07 of Switch,
released May 15, 2002.
=head1 SYNOPSIS
Expand Down Expand Up @@ -739,23 +744,25 @@ Perl 6 will provide a built-in switch statement with essentially the
same semantics as those offered by Switch.pm, but with a different
pair of keywords. In Perl 6 C<switch> with be spelled C<given>, and
C<case> will be pronounced C<when>. In addition, the C<when> statement
will use a colon between its case value and its block (removing the
need to parenthesize variables.
will not require switch or case values to be parenthesized.
This future syntax is also available via the Switch.pm module, by
This future syntax is also (largely) available via the Switch.pm module, by
importing it with the argument C<"Perl6">. For example:
use Switch 'Perl6';
given ($val) {
when 1 : { handle_num_1(); }
when $str1 : { handle_str_1(); }
when [0..9] : { handle_num_any(); last }
when /\d/ : { handle_dig_any(); }
when /.*/ : { handle_str_any(); }
when 1 { handle_num_1(); }
when ($str1) { handle_str_1(); }
when [0..9] { handle_num_any(); last }
when /\d/ { handle_dig_any(); }
when /.*/ { handle_str_any(); }
}
Note that you can mix and match both syntaxes by importing the module
Note that scalars still need to be parenthesized, since they would be
ambiguous in Perl 5.
Note too that you can mix and match both syntaxes by importing the module
with:
use Switch 'Perl5', 'Perl6';
Expand Down
Loading

0 comments on commit 87acfc3

Please sign in to comment.