Jump to content
TheDcoder

Should Au3Check Count Global Variables Declared Inside Functions?

Recommended Posts

Hello everyone, I discovered a bug yesterday and I posted it at the bug tracker:

Quote

Au3Check is not able to recognise Global variables declared inside functions if the variables are used above the function declaration

I also made a simple script which can be used to reproduce the bug:

CreateVariable()

ConsoleWrite($sGlobalVariable & @CRLF)

Func CreateVariable()
    Global $sGlobalVariable = "Foobar"
EndFunc

The bug was closed by @BrewManNH:

Quote

This isn't a bug but poor programming.
Au3Check reads the script line by line from the first line to the last. It doesn't execute the script. Because you're trying to access a global variable that hasn't been declared yet (at that point in the script read), you get an error.

 
 

While I partially agree with the above statement, My code was not practical enough... so @mLipok advised me to create a thread on the forums with practical code (Thanks!). That is the point of this thread, I am going to provide the code where I experience this bug/problem :D.

I discovered this bug when I was working on one of my projects called "ProxAllium". When the main script finishes execution, Au3Check throws a nasty warning about "variable possibly used before declaration":
YJMj8db.png

As you can see, the variable is indeed being used after calling the function in which the variable is declared... The warning won't appear if I declare the function ABOVE the variable. As @BrewManNH said, Au3Check reads line by line... I think this should be changed, Au3Check should not throw warnings if the interpreter is able to run the code, at least most of the time anyway!

So what do you guys think? Is this a valid bug?... and I request those who participate in the discussion not to discuss the code being "poor", that is another thing/thread in itself ;)

P.S I had already written this once but the forum editor decided to mess up and when I undid (Ctrl + Z) something... This is a poorly written version of that article, I was very frustrated while writing this!


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

TheDcoder,

You do realise that you are not declaring $g_hTorOutPut anywhere before the line in which that variable is flagged as "possibly used before declaration"? I presume that variable is being declared within the GUI_CreateTorOutputWindow function and so after the line which is flagged - hence Au3Check's warning.

Au3Check is a fairly simple tool (although if you know anything about YACC you will appreciate it is far from simple to code) and only checks the script line-by-line. Asking it to act as the actual interpreter is a whole new game and would be a complete waste of time in my opinion. So: no bug - just a tool doing its best to help you from making a possible error. After all, it is only flagged as a warning, not as an error.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
1 minute ago, Melba23 said:

You do realise that you are not declaring $g_hTorOutPut anywhere before the line in which that variable is flagged as "possibly used before declaration"?

Yes :):

19 minutes ago, TheDcoder said:

As @BrewManNH said, Au3Check reads line by line

 

3 minutes ago, Melba23 said:

Asking it to act as the actual interpreter is a whole new game

I am aware that it is not worth to make it act as an interpreter but it could at least read the function's code if the function is being called.


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

TheDcoder,

As I said, Au3Check is only giving a warning, not an error - so you are quite at liberty to ignore it if you know that the variable has indeed been declared. But, despite your wish not to go there I would argue that not specifically declaring a Global variable before you first access it is not good coding practice as declaring it inside another function can lead to all sorts of problems when cutting and pasting modules between scripts.

And if you want Au3Check to do more than it does, you learn enough YACC to rewrite it - I am sure Jos and jpm would welcome your offer to take over development.

M23

 


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

No bug and has been explained often: Au3check read the whole script in one pass and gives basic warnings and errors, no more no less.

Jos

Share this post


Link to post
Share on other sites
10 minutes ago, Melba23 said:

As I said, Au3Check is only giving a warning, not an error - so you are quite at liberty to ignore it if you know that the variable has indeed been declared.

The only thing which bugs me is that SciTE automatically takes the cursor (the blinking position thingy)  to the line of the warning... it is quite annoying!

17 minutes ago, Melba23 said:

declaring it inside another function can lead to all sorts of problems when cutting and pasting modules between scripts

The function in which I am declaring the Global variable is supposed to be only called once per execution... so that would not cause any chaos :D.

18 minutes ago, Melba23 said:

And if you want Au3Check to do more than it does, you learn enough YACC to rewrite it - I am sure Jos and jpm would welcome your offer to take over development.

I would love to do that but I still have to a lot to learn... and the very limiting TIME factor :(.

 

P.S I see that Jos has replied before I finish this post, will reply to his post in another post.


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

I get you @Jos, we should at least have something by which we can "ignore" some errors. Do you agree with me?


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites
9 minutes ago, TheDcoder said:

The only thing which bugs me is that SciTE automatically takes the cursor (the blinking position thingy)  to the line of the warning... it is quite annoying!

My thinking in my head: I find it quite annoying that forum members tell me features are quit annoying without checking whether this is a configurable feature!

My political correct answer: No that is a feature which I actually like as that is the starting point for clearing the shown warnings and errors. Did you assume this is not configurable or did you actually check the SciTE helpfile to see whether you could switch that feature off? ((hint:#AutoIt3Wrapper_Jump_To_First_Error)

Jos

Edited by Jos
typos

Share this post


Link to post
Share on other sites
Just now, TheDcoder said:

we should at least have something by which we can "ignore" some errors. Do you agree with me?

No I do not: You either use au3check and adhere to its standards or simply don't use it. ;)

Jos

Share this post


Link to post
Share on other sites
7 minutes ago, Jos said:

Did you assume this is not configurable or did you actually check the SciTE helpfile to see whether you could switch that feature off?

I remember visiting the SciTE help file, did not find anything there... not even #AutoIt3Wrapper_Jump_To_First_Error! I might have missed it, I am not much of a reader but a skimmer


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Hey and I understand... the trigger for my response in the way I did it was was the word annoying.
Do you understand it sometimes can be pretty frustrating when spending a lot of time to make these type of feature for the greater good and someone comes along and dumps his shortsighted response like you did?

Jos  

Share this post


Link to post
Share on other sites
1 minute ago, Jos said:

Do you understand it sometimes can be pretty frustrating when spending a lot of time to make these type of feature for the greater good and someone comes along and dumps his shortsighted response like you did?

 
 

I sincerely apologise for using the word "annoying" but it is/was not my intention to hurt anyone :). I am very grateful for the efforts which you have put in the community and I mean it! It is not my intention to sadden anyone, but circumstances force me to use terrible word choices...

I am currently looking into possible workarounds, I will shortly reply about my workaround if I find it.


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites
16 minutes ago, TheDcoder said:

I am currently looking into possible workarounds

The workaround is don't write bad code that causes issues like this. If you REALLY want a work around that will compensate for your bad programming style, put the function at the TOP of the script and the rest of the code below it.

Func CreateVariable()
    Global $sGlobalVariable = "Foobar"
EndFunc
CreateVariable()

ConsoleWrite($sGlobalVariable & @CRLF)

This is what happens when you include another file in your script using #include, that's why you don't get these warnings when using Global variables from include files.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I don't want to move the functions to the top... It's not the place where a function should be. Now I am thinking of changing the style, any recommendations on how should I proceed?


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Put the function into a separate file and #include it in your main script would be one way.

Also, a function can be anywhere in the script, as long as its not inside another function or loop. Just because most people put them at the bottom of the code doesn't mean you have to do it that way. When you include another UDF in your code, everything in that UDF is inserted into your script at the point where you put the #include line. So putting an include at the start of your script means all those functions are placed at the top.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
1 minute ago, BrewManNH said:

Put the function into a separate file and #include it in your main script would be one way.

A great idea actually... I will try this one ;)

And yes, I know that #include tells the interpreter to think of the included file as if it has been copied over at the #inlude line :D


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites
13 hours ago, TheDcoder said:

I get you @Jos, we should at least have something by which we can "ignore" some errors. Do you agree with me?

We get this from time to time, forum members who ask for a workaround to permit them to write crap code; I will never understand it. @Jos has more patience than I in his continued pleasant responses to such inane requests. :)

 

Share this post


Link to post
Share on other sites

and to recap:  this behavior is due to both awful scripting and a stubborn preference for functions at the bottom, and the most suitable mitigation is certainly not found in altering the way AU3check behaves.

@Jos was downright saintly in this thread. 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
10 hours ago, JLogan3o13 said:

forum members who ask for a workaround to permit them to write crap code;

Maybe those people can't think of any better way to do it? ¯\_(ツ)_/¯
They will, of course, find a better way after asking at the forums :P (Same thing with me)

10 hours ago, iamtheky said:

this behavior is due to both awful scripting and a stubborn preference for functions at the bottom, and the most suitable mitigation is certainly not found in altering the way AU3check behaves.

 

I totally agree with this one.

 

As for my workaround, after taking useful input from here, I decided to separate the GUI #Regions into 2... GUI Creation (functions) at the top and the GUI Event Handlers at the bottom. It works perfectly :D.

Thanks to everyone who participated in this thread!


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

Share this post


Link to post
Share on other sites

Booo, I was hoping you would have a decent defense for declaring global within functions.  

Concessions when I am trying to be provocative make me look even more like a dick than I already am.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • By mlazovjp
      I am going through the AutoIt Wiki and soaking up a lot of great information.  On the “Best Coding Practices” page at https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables there seems to be a minor detail missing.  In the “Variable Initialization” section at https://www.autoitscript.com/wiki/Best_coding_practices#Variable_Initialization , there are references to a data type $o, but in the table listing prefixes and their data types, there does not appear to be a prefix “o”.  I assume that this is for Object?
    • By BetaLeaf
      What is Rollbar?
      Rollbar provides real-time error alerting & debugging tools for developers. Learn more about it at https://rollbar.com/product/
      Demo: https://rollbar.com/demo/demo/
      Screenshot:
      Instructions: (RollbarTest.au3)
      ; Include RollbarSDK #include "RollbarSDK.au3" ;Turns on ConsoleWrite debugging override. ;Global $Rollbar_Debug=False ; Initialize RollbarSDK with the project's API key. ; Parameters ....: $__Rollbar_sToken - [Required] Go to https://rollbar.com/<User>/<ProjectName>/settings/access_tokens/ for your project. Use the token for post_server_item. _Rollbar_Init("eaa8464a4082eeabd9454465b8f0c0af") ; Write code that causes an error you want to catch, then call ; _Rollbar_Send ; Parameters ....: $__Rollbar_sErrorLevel - [Required] Must be one of the following values: Debug, Info, Warning, Error, Critical. ; $__Rollbar_sMessage - [Required] The message to be sent. This should contain any useful debugging info that will help you debug. ; $__Rollbar_sMessageSummary - [Optional] A string that will be used as the title of the Item occurrences will be grouped into. Max length 255 characters. If omitted, Rollbar will determine this on the backend. _Rollbar_Send("Debug", "This is an debug message. If you received this, you were successful!", "Debug Message") _Rollbar_Send("Info", "This is a test message. If you received this, you were successful!", "Info Message") _Rollbar_Send("Warning", "This is an warning message. If you received this, you were successful!", "Warning Message") _Rollbar_Send("Error", "This is an error message. If you received this, you were successful!", "Error Message") _Rollbar_Send("Critical", "This is an critical message. If you received this, you were successful!", "Critical Message") _Rollbar_Send("Info", "This is a test message. If you received this, you were successful!") ;No Message ; Rollbar_Send's helper functions ; Parameters ....: $__Rollbar_sMessage - [Required] The message to be sent. This should contain any useful debugging info that will help you debug. ; $__Rollbar_sMessageSummary - [Optional] A string that will be used as the title of the Item occurrences will be grouped into. Max length 255 characters. If omitted, Rollbar will determine this on the backend. _Rollbar_SendDebug("This is an debug message. If you received this, you were successful!", "Debug Message") _Rollbar_SendInfo("This is a test message. If you received this, you were successful!", "Info Message") _Rollbar_SendWarning("This is an warning message. If you received this, you were successful!", "Warning Message") _Rollbar_SendError("This is an error message. If you received this, you were successful!", "Error Message") _Rollbar_SendCritical("This is an critical message. If you received this, you were successful!", "Critical Message") ; Usable Example Local $sImportantFile = "C:\NOTAREALFILE_1234554321.txt" Switch FileExists($sImportantFile) Case True MsgBox(0, "Example Script", "An important file was found. Continuing...") Case Else _Rollbar_SendCritical('An important file was missing. Halting... File: "' & $sImportantFile & '"', 'Important file "' & $sImportantFile & '" is missing.') EndSwitch Notes: Please comment your feedback, advice, & suggestions below. While this is only a proof of concept, I will expand its feature set for everyone to use. 
      Right now, it is fully functional but not tested in production.
       
       
      Changelog:
      RollbarSDK.au3
      RollbarTest.au3
      v0.2
       
      v0.1.1
       
    • By ThePoro
      I have this code 
      It gets variable input from GUI to run a loop and write to a text file then copy it to the clipboard
      But the thing is code runs normally with $px and $py <100 or both > 100 or with $px <10 and $py>100 but It's wrong with 10<$px <100 and $py >100
      Can anyone tell me why?
      Func clone() Local $px = GUICtrlRead($input1) Local $py = GUICtrlRead($input2) Local $temp = "C:\temp.csv" FileDelete($temp) While $px <= $py Local $name = "P"&$px FileWrite($temp, " & $name & ""& @CRLF) $px+= 1 WEnd $a=FileRead($temp) ClipPut($a) MsgBox($MB_ICONINFORMATION,"","Copied to Clipboard") EndFunc  
    • By qsek
      Im not sure if this is intended but normally Autoit variables are always passed as copies (except objects i think).
      But below i observed an unconsistency when copying maps with nested maps inside.
      Issue:
      If you create a nested map1 and copy it to a new map2, changing a nested value in map2 will also change the nested value in map1
      Dim $player[] Dim $sub[] $player.test1 = 1 $player.test2 = $sub $player.test2.child1 = "org" $player.test2.childext = $sub $player.test2.childext.child1 = "org2" $playerold = $player ; make a copy of the whole map ConsoleWrite("player.test2.child1 : "& $player.test2.child1 & @CRLF); original nested value in $player $playerold.test2.child1 = "changed" ; edit a nested value in $playerold ConsoleWrite("player.test2.child1 : "& $player.test2.child1 & @CRLF) ; original nested value in $player changed ConsoleWrite("---------------------" & @CRLF) ConsoleWrite("player.test2.childext.child1 : "& $player.test2.childext.child1 & @CRLF); original level2 nested value in $player $playerold.test2.childext.child1 = "changed2" ; edit a level2 nested value in $playerold ConsoleWrite("player.test2.child1 : "& $player.test2.child1 & @CRLF); original level1 nested value in $player stayed the same ConsoleWrite("player.test2.childext.child1 : "& $player.test2.childext.child1 & @CRLF); original level2 nested value in $player changed  
    • By BetaLeaf
      As the title says, when a script that is stored on a Google Drive File Stream drive is ran or compiled, it fails to work at all.
      Basically, Google Drive File Stream creates a G:\ drive where you can access all your files. The difference between this and Google Backup and Sync is your files are downloaded as needed rather than they always be downloaded and taking up storage. 
      How to Reproduce Bug
      1. Download and Install Google Drive File Stream
      2. Sign into Google Drive File Stream with a G Suite account. 
      3. Create a AutoIt Script and save it to Google Drive File Stream. (See attached file)
      4. Attempt to run or compile this AutoIt Script. 
       
      AutoIt appears to act like the script is (incorrectly) empty and ends immediately. (You can kind of tell based on file sizes from a successful and failed compile).
      I've also attached a Process Monitor log file. Hopefully someone can figure this out, because having to move the script out of the drive just to run or compile it is super annoying and I lose version revisioning Google Drive provides me.
      test.au3
      Logfile.PML
×
×
  • Create New...