-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVars.java
More file actions
74 lines (65 loc) · 2.93 KB
/
Vars.java
File metadata and controls
74 lines (65 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.perlonjava.runtime.perlmodule;
import org.perlonjava.runtime.runtimetypes.*;
/**
* The Vars class is responsible for creating variables in the current package.
* It mimics the behavior of Perl's vars module, allowing variables to be declared.
*/
public class Vars extends PerlModuleBase {
public Vars() {
super("vars");
}
/**
* Static initializer to set up the Vars module.
*/
public static void initialize() {
Vars vars = new Vars();
try {
vars.registerMethod("import", "importVars", ";$");
// Set $VERSION so CPAN.pm can detect our bundled version
GlobalVariable.getGlobalVariable("vars::VERSION").set(new RuntimeScalar("1.05"));
} catch (NoSuchMethodException e) {
System.err.println("Warning: Missing vars method: " + e.getMessage());
}
}
/**
* Creates the specified variables in the current package.
*
* @param args The arguments specifying the variables to create.
* @param ctx The context in which the variables are being created.
* @return A RuntimeList representing the result of the variable creation.
* @throws PerlCompilerException if there are issues with the variable creation process.
*/
public static RuntimeList importVars(RuntimeArray args, int ctx) {
RuntimeArray.shift(args);
// Determine the caller's namespace
RuntimeList callerList = RuntimeCode.caller(new RuntimeList(), RuntimeContextType.SCALAR);
String caller = callerList.scalar().toString();
// Create the specified variables in the caller's namespace
for (RuntimeScalar variableObj : args.elements) {
String variableString = variableObj.toString();
String fullName = caller + "::" + variableString.substring(1);
if (variableString.startsWith("$")) {
// Create a scalar variable
GlobalVariable.getGlobalVariable(fullName);
GlobalVariable.declareGlobalVariable(fullName);
} else if (variableString.startsWith("@")) {
// Create an array variable
GlobalVariable.getGlobalArray(fullName);
GlobalVariable.declareGlobalArray(fullName);
} else if (variableString.startsWith("%")) {
// Create a hash variable
GlobalVariable.getGlobalHash(fullName);
GlobalVariable.declareGlobalHash(fullName);
} else if (variableString.startsWith("&")) {
// Create a code variable
GlobalVariable.getGlobalCodeRef(fullName);
} else if (variableString.startsWith("*")) {
// autovivify the bareword handle
GlobalVariable.getGlobalIO(fullName);
} else {
throw new PerlCompilerException("Invalid variable type: " + variableString);
}
}
return new RuntimeList();
}
}