Skip to content

Commit bd10255

Browse files
committed
add vars pragma
1 parent 913c82e commit bd10255

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

FEATURE_MATRIX.md

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@
282282
- ✔️ **constant** pragma
283283
- ✔️ **if** pragma
284284
- ✔️ **lib** pragma
285+
- ✔️ **vars** pragma
285286
- 🚧 **utf8** pragma: utf8 is always on. Disabling utf8 might work in a future version.
286287
- ✔️ **feature** pragma
287288
- ✔️ Features implemented: `fc`, `say`, `current_sub`, `isa`, `state`, `try`, `bitwise`, `postderef`.

MILESTONES.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,14 @@
101101
- **v1.12.0**: Concurrency and Security Features
102102
- Planned release date: 2024-12-10
103103
- Added unmodified core Perl modules `File::Basename`, `File::Find`, `Data::Dumper`, `Term::ANSIColor`, `Time::Local`, `HTTP::Date`, `HTTP::CookieJar`.
104-
- Added `Cwd`, `File::Spec`, `File::Spec::Functions` modules.
104+
- Added `Cwd`, `File::Spec`, `File::Spec::Functions`, `HTTP::Tiny` modules.
105105
- "use feature" implemented: `fc`, `say`, `current_sub`, `isa`, `state`, `try`, `bitwise`, `postderef`.
106106
- Stash can be accessed as a hash like `$namespace::{entry}`.
107107
- Added stash constants: `$constant::{_CAN_PCS} = \$const`;
108108
- Added `exists &sub`, `defined &sub`.
109109
- Added `builtin` pragma: `true`, `false`, `is_bool`.
110110
- Added `re` pragma: `is_regexp`.
111+
- Added `vars` pragma.
111112
- Added `SUPER::method` method resolution.
112113
- Added `AUTOLOAD` default subroutine.
113114
- Added `stat`, `lstat` operators. Some fields are not available in JVM and return `undef`.
@@ -123,11 +124,10 @@
123124
- lexical utf8 source code.
124125
- `truncate`, `seek` operators.
125126
- preprocessor `# line` directive.
126-
- `use subs`, `use vars`.
127+
- `use subs`.
127128
- `Getopt::Long`.
128129
- subroutine prototypes.
129130
- `JSON` module.
130-
- `HTTP::Tiny` module.
131131
- Stretch goals
132132
- Add support for concurrency and parallelism, such as threads and async/await.
133133
- Enhance security features, including sandboxing and input validation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.perlonjava.perlmodule;
2+
3+
import org.perlonjava.runtime.*;
4+
5+
/**
6+
* The Vars class is responsible for creating variables in the current package.
7+
* It mimics the behavior of Perl's vars module, allowing variables to be declared.
8+
*/
9+
public class Vars extends PerlModuleBase {
10+
11+
public Vars() {
12+
super("vars");
13+
}
14+
15+
/**
16+
* Static initializer to set up the Vars module.
17+
*/
18+
public static void initialize() {
19+
Vars vars = new Vars();
20+
try {
21+
vars.registerMethod("import", "importVars", ";$");
22+
} catch (NoSuchMethodException e) {
23+
System.err.println("Warning: Missing vars method: " + e.getMessage());
24+
}
25+
}
26+
27+
/**
28+
* Creates the specified variables in the current package.
29+
*
30+
* @param args The arguments specifying the variables to create.
31+
* @param ctx The context in which the variables are being created.
32+
* @return A RuntimeList representing the result of the variable creation.
33+
* @throws PerlCompilerException if there are issues with the variable creation process.
34+
*/
35+
public static RuntimeList importVars(RuntimeArray args, int ctx) {
36+
args.shift();
37+
38+
// Determine the caller's namespace
39+
RuntimeList callerList = RuntimeScalar.caller(new RuntimeList(), RuntimeContextType.SCALAR);
40+
String caller = callerList.scalar().toString();
41+
42+
// Create the specified variables in the caller's namespace
43+
for (RuntimeScalar variableObj : args.elements) {
44+
String variableString = variableObj.toString();
45+
46+
if (variableString.startsWith("$")) {
47+
// Create a scalar variable
48+
GlobalVariable.getGlobalVariable(caller + "::" + variableString.substring(1));
49+
} else if (variableString.startsWith("@")) {
50+
// Create an array variable
51+
GlobalVariable.getGlobalArray(caller + "::" + variableString.substring(1));
52+
} else if (variableString.startsWith("%")) {
53+
// Create a hash variable
54+
GlobalVariable.getGlobalHash(caller + "::" + variableString.substring(1));
55+
} else {
56+
throw new PerlCompilerException("Invalid variable type: " + variableString);
57+
}
58+
}
59+
60+
return new RuntimeList();
61+
}
62+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public static void initializeGlobals(ArgumentParser.CompilerOptions compilerOpti
9898
// Initialize built-in Perl classes
9999
DiamondIO.initialize(compilerOptions);
100100
Universal.initialize();
101+
Vars.initialize();
101102
Builtin.initialize();
102103
Exporter.initialize();
103104
Base.initialize();

0 commit comments

Comments
 (0)