Skip to content

Commit c7da686

Browse files
committed
local: add test cases
1 parent c0dfd6e commit c7da686

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

FEATURE_MATRIX.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
## Scalars
3333
- ✔️ **`my` variable declaration**: Local variables can be declared using `my`.
3434
- ✔️ **`our` variable declaration**: Global variables can be declared using `our`.
35-
- **`local` variable declaration**: Support for temporary local variable changes is missing. `local` is also missing for typeglobs.
35+
- ✔️ **`local` variable declaration**: Dynamic variables are implemented.
3636
-**`state` variable declaration**: Support for state variable changes is missing.
3737
- ✔️ **Variable assignment**: Basic variable assignment is implemented.
3838
- ✔️ **Basic types**: Support for integers, doubles, strings, CODE, and undef is present.
@@ -49,6 +49,7 @@
4949
- ✔️ **Cached string/numeric conversions; dualvars**: Caching is implemented, but it doesn't use the Perl "dual variable" implementation.
5050
-**Unicode**: Support for non-Unicode strings is not implemented.
5151
-**Taint checks**: Support for taint checks is not implemented.
52+
-**`local` special cases**: `local` is missing for typeglobs and filehandles. Localization in for-loops is also missing.
5253

5354
## Objects
5455
- ✔️ **Objects**: Creating classes, method call syntax works.
@@ -138,6 +139,7 @@
138139
- ✔️ **`reset`** resetting one-time match is implemented
139140
- ✔️ **`@-`, `@+`, `%+`, `%-` variables**: regex special variables are implemented
140141
- ✔️ **`$&` variables**: `` $` ``, `$&`, `$'` special variables are implemented
142+
- ✔️ **Matching plain strings**: `$var =~ "Test"` is implemented.
141143
-**Perl-specific Regex Features**: Some features like `/xx` `/ee` are missing.
142144
-**Dynamically-scoped regex variables**: Regex variables are not dynamically-scoped.
143145
-**Code blocks**: `(?{ code })` in regex is not implemented.

src/main/java/org/perlonjava/runtime/RuntimeRegex.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ public static RuntimeScalar getReplacementRegex(RuntimeScalar patternString, Run
134134
* @return A RuntimeScalar or RuntimeList.
135135
*/
136136
public static RuntimeDataProvider matchRegex(RuntimeScalar quotedRegex, RuntimeScalar string, int ctx) {
137-
RuntimeRegex regex = (RuntimeRegex) quotedRegex.value;
137+
RuntimeRegex regex;
138+
if (quotedRegex.type != RuntimeScalarType.REGEX) {
139+
// not a regex: $var =~ "Test"
140+
regex = compile(quotedRegex.toString(), "");
141+
} else {
142+
regex = (RuntimeRegex) quotedRegex.value;
143+
}
138144
if (regex.replacement != null) {
139145
return replaceRegex(quotedRegex, string, ctx);
140146
}

src/test/resources/local.pl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ sub modify_global_var {
6868
}
6969
say $::package_var eq "package original" ? "ok # local package variable restored" : "not ok";
7070

71-
__END__
72-
73-
#----- TODO --------
74-
7571
# Special case: localizing special variables
7672
$@ = "";
7773
{
@@ -82,13 +78,17 @@ sub modify_global_var {
8278
}
8379
say $@ eq "" ? "ok # \$@ restored after eval <$@>" : "not ok";
8480

85-
## # Special case: localizing filehandles
86-
## open my $fh, "<", "/etc/passwd" or die "Cannot open file: $!";
87-
## {
88-
## local *FH = $fh;
89-
## while (<FH>) {
90-
## last if $. > 5; # Read only first 5 lines
91-
## }
92-
## }
93-
## say "ok # filehandle localized";
81+
__END__
82+
83+
#----- TODO --------
84+
85+
# Special case: localizing filehandles
86+
open my $fh, "<", "/etc/passwd" or die "Cannot open file: $!";
87+
{
88+
local *FH = $fh;
89+
while (<FH>) {
90+
last if $. > 5; # Read only first 5 lines
91+
}
92+
}
93+
say "ok # filehandle localized";
9494

0 commit comments

Comments
 (0)