Jump to content
TheDcoder

Should Au3Check Count Global Variables Declared Inside Functions?

Recommended Posts

TheDcoder

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!


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

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

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
Melba23

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
TheDcoder
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.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

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

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
Melba23

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
Jos

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


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
TheDcoder
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.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

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

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
TheDcoder

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


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

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

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
Jos
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Jos
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

  • Like 1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
TheDcoder
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


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

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

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
Jos

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  


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
TheDcoder
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.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

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

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
BrewManNH
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.

  • Like 1

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
TheDcoder

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?


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

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

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
BrewManNH

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
TheDcoder
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


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

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

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
JLogan3o13
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. :)

 


√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
iamtheky

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. 

  • Like 1

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

Share this post


Link to post
Share on other sites
TheDcoder
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!


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

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

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
iamtheky

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

    • qsek
      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  
    • BetaLeaf
      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
    • Skeletor
      By Skeletor
      Hi Guys,
      Is it possible to get a variable on your For..Next loop? 
       
      Local $Lines1 = _FileCountLines(C:\temp\test.txt) Local $linesToCount2 = $Lines1 + 2 $var = Number($linesToCount2) For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = $var To $count Next ;Code does stuff here Next Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.
      This code I posted above does not move to the next value.
      But the code below does... why is that?
      For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = 2 To $count Next ;Code does stuff here Next  
      Why is the For loop resetting itself?
      Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?
      If so , how would you make this variable static?

       
    • Miliardsto
      By Miliardsto
      I got that func
      Func makeHelpImgGUI($title,$width,$height,$img) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, "HERE") EndFunc and I call this func like that
      makeHelpImgGUI("Image",1190, 800,$SETTINGS_JPG)  
      so what is the problem in the parameter where is - "HERE" I need value of img but passed as string
      so $img = $SETTINGS_JPG and how make it "SETTINGS_JPG"
       
      I tried something like that but not work
      Func makeHelpImgGUI($title,$width,$height,$img) $name_str = String($img) $name_str = StringTrimLeft ($name_str, 1 ) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, $name_str) EndFunc  
    • xiantez
      By xiantez
      This script used to work on an older version of AutoIT. Currently I am running AutoIT v3.3.14.5 and it's failing.
      Func PublicIP() ;Post public facing IP address Local $url = 'https://www.google.com/search?client=opera&q=what+is+my+ip&sourceid=opera&ie=UTF-8&oe=UTF-8' Local $getIPaddress = BinaryToString(InetRead($url)) Local $sStart = 'clamp:2">' Local $sEnd = '</div>' Local $ipaddress = _StringBetween($getIPaddress, $sStart, $sEnd For $i In $ipaddress MsgBox(0, 'External IP', "Your public IP address is " & $i) Next EndFunc ;==>PublicIP The console output shows:
      "C:\Users\user\Documents\AutoIT\Scripts\WSI Tools.au3" (197) : ==> Variable must be of type "Object".: For $i In $ipaddress For $i In $ipaddress^ ERROR ->14:12:16 AutoIt3.exe ended.rc:1 +>14:12:16 AutoIt3Wrapper Finished. >Exit code: 1 Time: 9.811
×