Tuesday, November 17, 2015

PowerShell - Basics by Example



To continue with the Basics by Example. today's version of the post written in PowerShell Enjoy!

You can copy and paste the code below in your favorite IDE/Editor, I use PowerShell ISE, and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

Beware that this is using the class keyword introduced on PowerShell version 5.0. If you're on Windows 10, you must have it already, otherwise you will need to download the Windows Management Framework 5.0 Production Preview . You can check your current version using the following command:






There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 



Greetings Program - Verbose
# PowerShell Basics

# Greeting Class  
class Greet {
    # Fields and Auto-Properties (PowerShell doesn't differentiate between fields & properties)
    [String] $message = ""
    [String] $name = ""
    [Int] $loopMessage = 0
    # Set-Property method 
    [void] setMessage([String] $value) {
        $this.message = $this.Capitalize($value)
    }
    [void] setName([String] $value) {
        $this.name = $this.Capitalize($value)
    }
    # Constructor 
    Greet() {
        $this.message = ""
        $this.name = ""
        $this.loopMessage = 1
    }
    # Overloaded Constructor  
    Greet([String] $message, [String] $name, [String] $loopMessage) {
        $this.message = $message
        $this.name = $name
        $this.loopMessage = $loopMessage
    }
    # Method 1
    [String] Capitalize([String] $val) {
        if ($val.Length -eq 0) {  
            return ""
        } else {
            return (Get-Culture).TextInfo.ToTitleCase($val)            
        }
        #Write-Host ("Hello {0}!`n" -f $this.name)
    }    
    # Method 2    
    [void] Salute() {
        # "for" statement (no need of ; if linebreak used) 
        for ($i = 0; $i -lt $this.loopMessage; $i++) {  
            Write-Host ("{0} {1}!" -f $this.message, $this.name)
        }  
    }  
    # Overloaded Method 2.1  
    [void] Salute([String] $message, [String] $name, [String] $loopMessage) {
        $i = 0
        # "while" statement  
        while ($i -lt $loopMessage) {  
            Write-Host ("{0} {1}!" -f $this.Capitalize($message), $this.Capitalize($name))
            $i++
        }       
    }  
    # Overloaded Method 2.2  
    [void] Salute([String] $name) {
        $dtNow = Get-Date
        switch -regex ($dtNow.Hour) {
            "^(10|11|[6-9])" {$this.message = "good morning,";break} 
            "^(1[2-7])" {$this.message = "good afternoon,";break} 
            "^(1[8-9]|2[0-2])" {$this.message = "good evening,";break}             
            "^(23|[0-5])" { $this.message = "good night,";break} 
            default {$this.message ="huh?"}
        }
        Write-Host ("{0} {1}!" -f $this.Capitalize($this.message), $this.Capitalize($name))
    }
}

# Console Program  

# Define object of type Greet and Instantiate. Call Constructor  
$g = [Greet]::new()
# Call Set (Methods)Properties and Property (PS is case-insensitive)
$g.setMessage("hello")
$g.setName("world")
$g.LoopMessage = 5 
# Call Method 2  
$g.Salute()  
# Call Overloaded Method 2.1 and Get Properties  
$g.Salute($g.Message, "powershell", $g.LoopMessage)
# Call Overloaded Method 2.2  
$g.Salute("carlos")
              
# Stop and Exit  
Read-Host "Press any key to exit..."

Greetings Program - Minimal
# No difference with the program above.

And the Output is:





Friday, November 13, 2015

OO Hello World - Nashorn



Hello world in Nashorn is here! An ECMAScript/JavaScript engine implemented by Oracle.

"The Nashorn engine is an implementation of the ECMAScript Edition 5.1 Language Specification. It was fully developed in the Java language as part of the Nashorn project. The code is based on the new features of the Da Vinci Machine, which is the reference implementation of Java Specification Request (JSR) 292: Supporting Dynamically Typed Languages on the Java Platform. 

The Nashorn engine is included in the Java SE Development Kit (JDK). You can invoke Nashorn from a Java application using the Java Scripting API to interpret embedded scripts, or you can pass the script to the jjs or jrunscript tool." Taken from https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/intro.html


By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.
function Greet(name) {
 this.name = name.charAt(0).toUpperCase() + name.slice(1, name.length); 
 this.salute = function() {
  print("Hello " + this.name + "!");
 }
}

// Greet the world!
var g = new Greet("world");
g.salute();

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.
function Greet(name) {
 this.name = name.charAt(0).toUpperCase() + name.slice(1, name.length); 
}

Greet.prototype.salute = function() {
 print("Hello " + this.name + "!");
}

var g = new Greet("world");
g.salute();

The Program Output:





  



Nashorn Info:
“Nashorn is a JavaScript engine developed in the Java programming language by Oracle. It is based on the Da Vinci Machine (JSR 292) and has been released with Java 8.” Taken from: https://en.wikipedia.org/wiki/Nashorn_(JavaScript_engine)

Appeared:
2014
Current Version:
Nashorn 1.8 JavaScript 5.1  (latest version in "Languages" page
Developed by:
Oracle
Creator:
Oracle
Influenced by:
JavaScript (Brendan Eich)
Predecessor Language
Rhino
Predecessor Appeared
1997
Predecessor Creator
Oracle
Runtime Target:
JDK
Latest Framework Target:
8
Mono Target:
N/A
Allows Unmanaged Code:
No
Source Code Extension:
“.js”
Keywords:

Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
Yes
Standard:
JSR292 ECMA-262
Latest IDE Support:
Any text editor for JavaScript
https://netbeans.org
Language Reference:
Extra Info:


Thursday, November 12, 2015

OO Hello World - PowerShell



The Hello World in PowerShell is finally here!

"Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework." Its been around for ~8 years (2006), but it is until now, with the release of version 5.0, that they finally introduce classes as a built-in feature of the language.

Now that the language have classes, properties, methods, constructors, static members, inheritance and so, we can compare its syntax with all other languages on this blog.

By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.
class Greet {
    [String] $name;
    Greet([String] $name) {
        $this.name = ([String]$name[0]).ToUpper() + $name.Substring(1);
    }
    [void] Salute() {
        Write-Host ("Hello {0}!`n" -f $this.name)
    }    
}

$g = [Greet]::new("World")
$g.Salute()

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.
class Greet {
    [String] $name;
    Greet([String] $name) {
        $this.name = ([String]$name[0]).ToUpper() + $name.Substring(1);
    }
    [void] Salute() {
        Write-Host ("Hello {0}!`n" -f $this.name)
    }    
}

$g = [Greet]::new("World")
$g.Salute()



Both version are completely similar.

The Program Output:









PowerShell Info:
“PowerShell is an automation platform and scripting language for Windows and Windows Server that allows you to simplify the management of your systems. Unlike other text-based shells, PowerShell harnesses the power of the .NET Framework, providing rich objects and a massive set of built-in functionality for taking control of your Windows environments. ” Taken from: https://msdn.microsoft.com/en-us/mt173057.aspx

Appeared:
2006
Current Version:
Developed by:
Microsoft
Creator:
Jeffrey Snover, Bruce Payette, James Truher
Influenced by:
C# and Perl, Tcl, Cmd
Predecessor Language
~WSH (VBScript, JScript)
Predecessor Appeared
1998
Predecessor Creator
Microsoft
Runtime Target:
CLR 4.x
Latest Framework Target:
CLR 4.x
Mono Target:
No
Allows Unmanaged Code:

Source Code Extension:
".ps1", "ps*"
Keywords:
35
Case Sensitive:
No
Free Version Available:
Yes
Open Source:
No
Standard:
No
Latest IDE Support:
PowerShell ISE
Language Reference:
More Info:


Sunday, November 8, 2015

OO Hello World - Silver



Hi-yo Silver! Away!

The Hello World in Silver is here! Silver is a free implementation of Apple's Swift programming language being actively developed by RemObjects, the company behind the exceptional language Oxygene.

As mentioned on their website: "Silver is a truly native Swift compiler for the .NET CLR, the Java/Android JVM and the Cocoa runtime. Built on over ten years of solid compiler knowledge and technology, Silver is a truly native Swift compiler for the .NET CLR, the Java/Android JVM and the Cocoa runtime." http://www.elementscompiler.com/elements/silver

By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.
class Greet {
 var name: String

 init(name: String) {
        self.name = Char.ToUpper(name.0) + name.Substring(1)
    }

 func salute() {
  println("Hello \(name)!")
 }
}

let g = Greet(name: "world")
g.salute()

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.
import System
class Greet {
 private var name: String

 public init(name: String) {
        self.name = Char.ToUpper(name.0) + name.Substring(1)
    }

 public func salute() {
  println("Hello \(name)!")
 }
}

let g = Greet(name: "world")
g.salute()

The Program Output:









Swift (Silver) Info:
“Swift is a new programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, and more fun..” Taken from: https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

Appeared:
2014
Current Version:
Silver 8.2 beta, Swift 2.1 (latest version in "Languages" page
Developed by:
Silver by RemObjects, Swift by Apple Inc.
Creator:
Silver by RemObjects, Swift by Chris Lattner and Apple Inc.
Influenced by:
Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, D 
Predecessor Language

Predecessor Appeared

Predecessor Creator

Runtime Target:
CLR and JVM
Latest Framework Target:
CLR 4.x JDK 8
Mono Target:
No
Allows Unmanaged Code:
Yes
Source Code Extension:
“.swift”
Keywords:
Silver 89, Swift 80
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
Swift Yes, Silver No.
Standard:
No
Latest IDE Support:
Visual Studio 2013
Language Reference:
More Info: