Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ A matter of scope...

A matter of scope...

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Shadowfyr   USA  (1,792 posts)  Bio
Date Thu 28 Aug 2003 07:53 PM (UTC)
Message
First, for those not in the know, 'scope' refers to which part of a script has access to a variable or function. Variables can be 'global' or 'local'. Scope with respect to functions and subroutines are what you get with world.note.
The 'note' command is technically in the 'world' scope, but recent changes Nick made allow it in most of the scripts to be 'seen' on the same level as the script itself, thus making the 'world.' part unneeded. However, I am here to discuss variables.

The problem is this. When you use the 'send to script' option, it runs in the 'gobal' scope of the main world file. This means that if you then try to perform something like:

a = b + c

but a trigger or alias calls a real subroutine that does something like:

a = "Hi there, "%1"

The script will crash with an error when it tries to perform math on it. This is inconvenient to say the least. It would also probably happen if you used 'a' this way in two seperate triggers or aliases. There are a few ways to sort of fix this. The first is to use 'dim' or the equivalent to 'always' create any variable you are going to use that only exists in the sub you call. In other words, if you don't intentionally want it to be 'global', force the script to make on that is 'local'. The second option is to use some sort of unique naming for temporary variables in the 'send to script' code. Neither of these things solves the problem of what happens if you use the same variable in more than one trigger or alias, or what happens if you forget and use the name of something you intentionally made global, like a list of names that more than one sub may need to use.

So the question is, is it possible within a 'send to script' to do something like:

sub Dothis(a,b,c)
...
end sub
call Dothis("%1","%2","%3")

Would this cause some sort of error the second time it passed through or would such a sub exist, then vanish? Or alternatively, if it does appear, then vanish as needed, then couldn't it be set up so such a sub was created based on a similar design that was based off the name of the trigger or alias (though currently you don't have to give one...)? Having this stuff execute in the global script space is an accident waiting to happen, unless something can be done to corral the behaviour of the engine to save and try to reuse any variables you inadvertantly create in one. The code itself may or may not vanish once no longer needed, but any variables it creates don't.
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 29 Aug 2003 12:47 AM (UTC)
Message
Well, let's try it ...


<triggers>
  <trigger
   enabled="y"
   match="sage"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
sub blah
  dim a_local_variable
  Note "in blah"
end sub

call blah
</send>
  </trigger>
</triggers>


I know you meant to do it automatically (or should that be, automagically?) however just putting the sub in the trigger yourself will limit the scope of any internal variables.

VBscript doesn't seem to mind the sub being redefined each time.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Shadowfyr   USA  (1,792 posts)  Bio
Date Reply #2 on Fri 29 Aug 2003 02:17 AM (UTC)
Message
Ok. Nice to know. I wasn't sure how exactly the code got handled, but figured that it probably didn't persist in the way that variables do. Was a nasty surprise when I found out they did. ;)
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


9,070 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.