Friday, August 9, 2013

Intro to Brogramming - Logic, Flow Control and Functions

What would a programming language be without conditional statements and loops? Pretty, lame. Lucky for you, Bro isn't lame.

Code within Bro script files can be within functions, events (both of which we'll get to later), or neither. This is similar to code in a Perl/Ruby/Python script can be nicely placed within a subroutine/function or just written haphazardly in the file.
Code placed in functions and events is not run until the function gets called or the event is triggered. Code that is placed in a script but in neither a function nor an event is executed sequentially. To allow the brogrammer to control the flow of a script, scriptland has the following commands
  • if, else if, else  - Code following these statements execute given the defined condition is met.
  • ternary - Ternary statements are shorthand if statements and behave the same way.
  • when - Code blocks following these statements execute asynchronously. The code blocks are queued and are executed when the condition is met, indipendent of sequential execution happening in the rest of the script.
  • schedule - Schedule a block of code to execute after a user defined duration of time.
  • for - In Bro, for loops are used to iterate over something - a vector, table or set. For loops do NOT guarantee order of the structure they iterate over (random access). For loops require a local variable to be  assignment the current value of the complex data structure being iterated over.
  • break - A command to end execution of the nearest loop.
  • next - A command that begins the next iteration of the nearest loop, skipping any commands after the next.
  • return - Usually the last command in a function, it returns a value from a block of code to its caller

Run this script and be sure you understand everything that is happening internally.

This is what the script does
#scheduling and when
  • creates a user defined event 'e' that prints the count value, 'c' passed to it and sets the global count variable equal to 'c'
  • schedules the 'e' event to run 10 seconds after Bro initializes
  • schedules the 'e' event to run 15 seconds after Bro initializes
  • Asynchronously queues a print statement to execute when the global variable 'x' is equal to ten (this should occur 15 seconds after Bro initializes)
#conditionals
  • Define two boolean values, one to true and one to false
  • Test if 'b2' is true (it's not) and if so print something
  • Test if 'b2' is true (it's not) and if so print something
  • If the previous two tests both failed (which they did) print something
  • Declare an undefined string variable
  • Use the ternary operator to 
    • If 'b1' is true set 's' equal to "b1 is true"
    • Otherwise set 's' equal to "b1 was false"
  • print the value of 's' to STDOUT
# loops
  • Define a set of strings name 'ss'
  • Loop over each string within 'ss', setting 's' to one of the values
  • Print 's' to STDOUT
Chances are you see print output from the conditionals section of code before you see the output from the scheduled section of code. This is because Bro schedules the code to run in the schedules section and continues executing the script sequentially. That is the meaning of asynchronous.

No comments:

Post a Comment