Community discussions

MikroTik App
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

[BUG] Parameters having a string value ending in \$ need to be initialised with parenthesis but variables don't.

Sun May 22, 2016 1:47 pm

It looks like when you initialise a parameter having a string value ending in $ you not only need to escape it, but also surround it with parenthesis but that is not needed for local variables.

Why is that?

If you forget the parenthesis, then you see it fail.
It doesn't give me a comfortable feel about the scripting language.

Just try running the below code on the console (examples from https://gist.github.com/jpluimers/9b239 ... efc02d9861 )
{
  :local showValue do={
    :local logLine "value='$value'"
    :put $logLine
  }

  ## fails each line that ends after \$" with a `syntax error (line 12 column 4)` (the line numbers vary)

  :put "-----------------------------start"
  $showValue value="^scripts.Function..*"
  $showValue value="^scripts.Function..*\$"
  $showValue value=("^scripts.Function..*\$")
  $showValue value="\$"
  $showValue value=("\$")
  :put "-----------------------------finish"
}
This one works though:
# showValuesFromParameters
{
  :local showValue do={
    :local logLine "value='$value'"
    :put $logLine
  }

  :put "-----------------------------start"
  $showValue value="^scripts.Function..*"
  $showValue value=("^scripts.Function..*\$")
  $showValue value=("\$")
  :put "-----------------------------finish"
}
And it will give you this output:
-----------------------------start
value='^scripts.Function..*'
value='^scripts.Function..*$'
value='$'
-----------------------------finish
The odd thing is that it works fine for `:local` variables, whether they are directly initialised at declaration time or later using a `:set` statement:
{
  :local showValuesFromDirectInitilisation do={
    :local value1 "^scripts.Function..*"
    :local logLine1 "value='$value1'"
    :put $logLine1

    :local value2 "^scripts.Function..*\$"
    :local logLine2 "value='$value2'"
    :put $logLine2

    :local value3 ("^scripts.Function..*\$")
    :local logLine3 "value='$value3'"
    :put $logLine3

    :local value4 "\$"
    :local logLine4 "value='$value4'"
    :put $logLine4

    :local value5 ("\$")
    :local logLine5 "value='$value5'"
    :put $logLine5
  }
  :put "-----------------------------start"
  $showValuesFromDirectInitilisation
  :put "-----------------------------finish"
}
and
{
  :local showValuesFromSetInitilisation do={
    :local value
    :local logLine

    :set $value "^scripts.Function..*"
    :set logLine "value='$value'"
    :put $logLine

    :set $value "^scripts.Function..*\$"
    :set logLine "value='$value'"
    :put $logLine

    :set $value ("^scripts.Function..*\$")
    :set logLine "value='$value'"
    :put $logLine

    :set $value "\$"
    :set logLine "value='$value'"
    :put $logLine

    :set $value ("\$")
    :set logLine "value='$value'"
    :put $logLine
  }
  :put "-----------------------------start"
  $showValuesFromSetInitilisation
  :put "-----------------------------finish"
}
both give this result:
-----------------------------start
value='^scripts.Function..*'
value='^scripts.Function..*$'
value='^scripts.Function..*$'
value='$'
value='$'
-----------------------------finish
So again: why the difference for parameters?

And what about assignment patterns: should you *always* initialise parameters and variables using parenthesis around them?

--jeroen
Last edited by jeroenp on Sun May 22, 2016 3:50 pm, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Why do parameters having a string value ending in \$ need to be initialised with parenthesis but variables don't?

Sun May 22, 2016 3:10 pm

I'd call that a bug, but as far as
And what about assignment patterns: should you *always* initialise parameters and variables using parenthesis around them?
You don't always "have to", but you may, and it is the universally safer option, so yeah. If in doubt, do use parenthesis.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6110
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: [BUG] Parameters having a string value ending in \$ need to be initialised with parenthesis but variables don't.

Mon May 23, 2016 11:11 am

Works fine if you add ; at the end of line.
{
  :local showValue do={
    :local logLine "value='$value'";
    :put $logLine
  }
  :put "-----------------------------start";
  $showValue value="^scripts.Function..*";
  $showValue value="^scripts.Function..*\$";
  $showValue value=("^scripts.Function..*\$");
  $showValue value="\$";
  $showValue value=("\$");
  :put "-----------------------------finish"
}
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: [BUG] Parameters having a string value ending in \$ need to be initialised with parenthesis but variables don't.

Mon May 23, 2016 1:24 pm

Works fine if you add ; at the end of line.[/code]
So it's still a bug that needs to be solved: the 'end-of-commandline' isn't always correctly adhered to. http://wiki.mikrotik.com/wiki/Manual:Sc ... mmand_line

But now I at least understand why some people put semicolons at the end of every line.

I never understood in the first place why you'd have both semicolon and various newline terminate commands (and even have exceptions when you don't need those).

A clear language specification should make that a lot easier to cope with.

--jeroen

Who is online

Users browsing this forum: No registered users and 10 guests