1

Is it possible, in ABAP, to evaluate string templates dynamically?

Normally, you will have some string template in code that will be checked by the compiler. (The variables in the curly brackets are checked by the compiler at compile time).

However, is it possible to have a string evaluated at runtime?

So, instead of:

data(val) = |System ID: { sy-sysid }|.

I would like the string to be interpolated to come from elsewhere, for example:

parameter: p_file type string lower case default '/mnt/{ sy-sysid }/file.txt'.

In this case, I would like to have the value of p_file to be evaluated at runtime to substitute the variable (sy-sysid) with the runtime value.

You could, of course, program your own substitution by finding all occurrences of variables with curly brackets with a regex expression, then evaluate the variable values with ASSIGN and substitute them back into the string, but I am looking for a built-in way to do this.

Sorry, this is maybe a stupid example, but hopefully you understand what I mean. (If not, please let me know in the comments and I will try and clarify).

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
mydoghasworms
  • 18,233
  • 11
  • 61
  • 95
  • Is this the actual use case? – vwegert Dec 19 '16 at 11:36
  • No - it is not. – mydoghasworms Dec 19 '16 at 12:10
  • Good, because you'd be opening a huge security hole there. What would the actual use case be, I wonder? – vwegert Dec 19 '16 at 13:25
  • I don't have access to an SAP system right now but I think the following code might work: parameter: p_file type string lower case default `\`/mnt/\` && sy-sysid && \`/file.txt\``. – Gert Beukema Dec 19 '16 at 14:50
  • @GertBeukema: Yes, that would probably work, but that is not what I am after. The question is whether, from a *constructed* or *external* string, one could leverage the built-in facility to interpolate strings with variables which may be unknown at compile time (i.e. dynamic). As I point out, it should be possible to code such a solution, but I am looking for something standard. – mydoghasworms Dec 20 '16 at 04:06
  • No, ``parameter: p_file type string lower case default `/mnt/` && sy-sysid && `/file.txt``.` causes the syntax error `Unable to interpret "&&". Possible causes of error include incorrect spellings or comma errors`. Best solution is given by suncatcher. PS: [`PARAMETER` is deprecated](https://help.sap.com/doc/abapdocu_753_index_htm/7.53/en-US/index.htm?file=abapparameter.htm), replaced with `PARAMETERS` (with a final S). – Sandra Rossi Jul 20 '19 at 19:00

2 Answers2

1

The problem in your snippet is not with string template but with PARAMETER behavior. It does not allow dynamics in DEFAULT clause.

To achieve what you want you should use INITIALIZATION and set path value in runtime:

parameter: p_file type string lower case.

INITIALIZATION.
p_file = | /mnt/{ sy-sysid }/file.txt |.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
0

Unfortunately, the example you gave, does not make any sense to me. ABAP String templates are evaluated at run-time and type-checked at compile-time. In your example, it is always the run-time value of SY-SYSID that will be written to the variable.

I guess what you want to do is circumvent compile-time checks for expressions inside a string template. Please try to give us your actual use case, so maybe we find an even better solution to your problem.

However, here is what I think could help you:

Personally, I do not recommend to write code like the one below, because it is extremely error-prone likely to mislead other programmers and because there is very likely a better solution.

Given that you know the name of a variable at run-time, try this:

".. say LV_VARNAME is a charlike variable that contains a
"   variable name at runtime.

"NOTE that the variable LV_VARNAME must be visible in the scope of the
"following code.

FIELD-SYMBOLS: <my_var> TYPE any.

ASSIGN (lv_varname) TO <my_var>.

DATA(lv_str) = |The value is { <my_var> }|.
Max O
  • 91
  • 5
  • Yes, thanks for the clarification. You are right indeed in your assessment that the fields in string templates are *type-checked* at compile time. Sorry, my explanation was poor/wrong. The problem with the solution you give is that it still relies on some *known* string template at the time the code is written. What if I want to build a string template at runtime by concatenating some elements together, and then evaluating the fields inside those? Again, coding one's own interpolation seems to be the only way. I suppose in the end it is more of an academic point than a practical one, really. – mydoghasworms Dec 21 '16 at 04:10
  • I guess I got you: do you think of scenarios like storing a string template inside a data base column? Therefore you could rely on run-time report generation and compilation with INSERT REPORT (and probably following GENERATE REPORT), see this posting http://stackoverflow.com/questions/35053253/generation-of-abap-report-in-runtime#35290145 You could generate a FORM subprogram and invoke it using PERFORM ... IN PROGRAM ... and pass your arguments to the string template inside the form routine as TYPE REF TO data. But I fear that would not be multi user / thread-safe. – Max O Dec 21 '16 at 12:38