Variable treated differently - why?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Tate83
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 30
Joined: Fri Mar 01, 2013 1:11 am

Variable treated differently - why?

Post by Tate83 » Thu Mar 21, 2013 9:45 pm

Hi guys

I am getting started with LiveCode and will write a wee test app to check my server status. Basically I wanna analyse the http header to see if it is 200 or something different. However, when testing the script runs fine with vServer19 and vServer22, but when I put vServer10 into the ShellCode generated, it does not work, the variable does not get a value assigned..

Can u help me?
(had to exchange the URLs with URL1-3 since this forum doesn't allow me to post links. but even when using the same URL in vServer10 and vServer19 it did not behave the same way)

Stack Script:

Code: Select all

on preOpenStack 

# Define Variables

global vServer10
global vServer19
global vServer22


put  "URL1" into vServer10 # Server 10
put  "URL2" into vServer22 # Server 22
put  "URL3" into vServer19   # Server 19

end preOpenStack
and my button script:

Code: Select all

on mouseUp
   # get globals from stack
   global vServer10
   global vServer19
   global vServer22
   
   # Create Shell Command
   
   # Create ShellCode to execute
   # Change server here manually for now
   put "curl -sL -w '%{http_code}' " &  vServer10 & " -o /dev/null" into tShellCode
   
   #execute shell code and put into result variable
   answer tShellCode
   
   put shell (tShellCode) into tResultHeader
   
   answer tResultHeader
   
   #evaluate result header
   if tResultHeader is "200" then
answer "All fine. Header code " & tResultHeader
else
   answer "Error: " & tResultHeader & ". Please correct"
end if

end mouseUp
Thanks,

Pascal

Tate83
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 30
Joined: Fri Mar 01, 2013 1:11 am

Re: Variable treated differently - why?

Post by Tate83 » Thu Mar 21, 2013 9:57 pm

Ok, I found that when I close the whole LiveCode the variables are re-initalised and then it behaves as expected.
Is there a way of resetting the globals without restarting everything?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Variable treated differently - why?

Post by Simon » Thu Mar 21, 2013 10:13 pm

Hi Pascal,
Not sure why you are using curl:

Code: Select all

on mouseUp
   get url "http://www.google.com"
   answer libURLLastRHHeaders()
end mouseUp
Does the same, I think.

As for resetting globals, you put them in the preOpenStack they only get filled when you actually open/restart your stack. If you are changing them on the fly for testing it's better just to put them into button.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Variable treated differently - why?

Post by sturgis » Thu Mar 21, 2013 10:35 pm

EDIT: and as simon mentions, if you're not doing recursive grabs with curl you can probably just use the liburl stuff built in to lc to do what you want. If you need special headers you can set those using lc before you grab the URL too.

To make things easier you might consider a few options. First, you can put most of your handlers into stack script or card script or the stack script of a library stack that you put into use, then your mouseup can just have

on mouseup
mySpecialHandler
end mouseup


Then in whatever script has your handlers you can use a local variable declared at the top


local mylocalvar1,mylocalvar2

Then have your handlers below the declarations, that way every handler or function in that script can see the same local variables. (they are declared outside any handlers, right at the top of the script)

You'll still need to initialize your variables of course but that can be done in a preopen card or stack or in opencard or openstack, or even have a separate handler to do the init. This way if something goes wrong and your values get munged you can just run the handler that does the init from the message box. (you can call opencard close card etc from the message box too, but breaking things out into smaller chunks lets you have better control of which things you want to reset)

You might also read up on custom properties, they can be very handy.

Also if you have unchanging data you can declare them as constants, again at the top of the script outside of handlers, then any handler/function in that script can use the constant.


You might also look at the merge function. It can be a real time saver when building url strings.

Say you have a variable tUrl which has "http://whateveraddress.com/file.php?" and you need to tack parameters onto the end. YOu can set up the whole string similar to this

put merge("http://whateveraddress.com/file.php?firstparam=[[tVariable1]]&secondparam=[[tVariable2]]") into tURL
Where merge will replace tVariable1 and tVariable 2 with the contents of the variable. You can have pretty much any evaluatable thing between the double brackets. Even something like [[the specialproperty of field 1]] or [[random(1000)]] It can still get a little complicated if you need double quoting but another thing you can do is create a constant named q that = tab. Then in your string you can put [[q]] to mark a quote.

As for re-initializing a global the same thing applies. Set up a handler that does the init, and during dev you can just call that handler from the message box.

Tate83
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 30
Joined: Fri Mar 01, 2013 1:11 am

Re: Variable treated differently - why?

Post by Tate83 » Thu Mar 21, 2013 11:11 pm

Hey,
Thanks for the quick reply!
I will try your ideas later, it looks way nicer using included functions instead of curl - even though that part was not the troublemaker yet ;-)

Currently looking into the array stuff on how to best run a repeat for my 3 or so server I want to check..

Have a nice evening.

Pascal

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Variable treated differently - why?

Post by sturgis » Thu Mar 21, 2013 11:24 pm

If there aren't very many you want to check you might also consider just using them as items (or lines)

so if you have "vserver10,vserver11,vserver19" in a variable (or constant, or property or field or....) you can

Code: Select all

repeat for each item tItem in myVariable -- comma is the default item delimiter. 
     do whatever you need to with tItem
end repeat

Tate83
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 30
Joined: Fri Mar 01, 2013 1:11 am

Re: Variable treated differently - why?

Post by Tate83 » Sat Mar 23, 2013 10:44 pm

Hey
Thanks to your input I was able to build the first wee app doing exactly what I wanted on my Mac. Now I will dig into bringing it onto my Nexus..

Have a great weekend

Pascal

Post Reply