Pike (programming language): Difference between revisions
m Dating maintenance tags: {{Single source}} |
|||
Line 42: | Line 42: | ||
=== Hello World === |
=== Hello World === |
||
:''For an explanation of the tradition of programming "Hello World", see [[Hello world program]].'' |
:''For an explanation of the tradition of programming "Hello World", see [[Hello world program]].'' |
||
<source lang=" |
<source lang="pike"> |
||
int main() { |
int main() { |
||
write("Hello world!\n"); |
write("Hello world!\n"); |
||
Line 53: | Line 53: | ||
* The write function sends a [[string literal]] to the standard output buffer, which in most cases is a [[command line interface]]. |
* The write function sends a [[string literal]] to the standard output buffer, which in most cases is a [[command line interface]]. |
||
* The third line returns a number to the caller of a function, in this case the [[command line interface]] where zero usually means the program executed without error. |
* The third line returns a number to the caller of a function, in this case the [[command line interface]] where zero usually means the program executed without error. |
||
* The curly brackets enclose the function and the semicolon separates statements (meaning the [[function (programming)|function]] could have been written on a single line: |
* The curly brackets enclose the function and the semicolon separates statements (meaning the [[function (programming)|function]] could have been written on a single line: <source lang="pike" enclose="none">int main(){write("Hello world!\n");return 0;}</source> |
||
* The "\n" after "Hello world!" is a [[newline]] character. |
* The "\n" after "Hello world!" is a [[newline]] character. |
||
Line 75: | Line 75: | ||
Pike requires explicit type definitions for all variables. It uses this information to report type errors at compile time. The following code will cause a compile error because the value of the variable "number" must be an integer but the code is attempting to assign floating point and string values to it. |
Pike requires explicit type definitions for all variables. It uses this information to report type errors at compile time. The following code will cause a compile error because the value of the variable "number" must be an integer but the code is attempting to assign floating point and string values to it. |
||
<source lang=" |
<source lang="pike"> |
||
int number; // integer variable, it only accepts integers |
int number; // integer variable, it only accepts integers |
||
number = 5.5; // 5.5 is a floating point value, error |
number = 5.5; // 5.5 is a floating point value, error |
||
Line 83: | Line 83: | ||
The following demonstrates a variable that can hold either an integer or a floating point number. |
The following demonstrates a variable that can hold either an integer or a floating point number. |
||
<source lang=" |
<source lang="pike"> |
||
int|float number; // integer OR float variable |
int|float number; // integer OR float variable |
||
number = 5; // this is legal |
number = 5; // this is legal |
||
Line 89: | Line 89: | ||
</source> |
</source> |
||
Because a variable can be declared as holding many different data types, functions are provided to determine what type of data is currently stored. These functions are all of the form typenamep, as in intp, floatp, stringp, etc. |
Because a variable can be declared as holding many different data types, functions are provided to determine what type of data is currently stored. These functions are all of the form typenamep, as in intp, floatp, stringp, etc. |
||
<source lang=" |
<source lang="pike"> |
||
int|float number; |
int|float number; |
||
number = 5; |
number = 5; |
||
Line 98: | Line 98: | ||
</source> |
</source> |
||
Additionally, there is a special "mixed" data type. That definition allows a variable to hold any kind of data type. |
Additionally, there is a special "mixed" data type. That definition allows a variable to hold any kind of data type. |
||
<source lang=" |
<source lang="pike"> |
||
mixed anything; |
mixed anything; |
||
anything = 5; // anything is now the integer value 5 |
anything = 5; // anything is now the integer value 5 |
||
Line 105: | Line 105: | ||
</source> |
</source> |
||
In order to convert a value from one type to another, Pike can use an explicit cast: |
In order to convert a value from one type to another, Pike can use an explicit cast: |
||
<source lang=" |
<source lang="pike"> |
||
mixed anything; |
mixed anything; |
||
anything = (int)5.5; // anything is now the integer value 5 |
anything = (int)5.5; // anything is now the integer value 5 |
Revision as of 17:16, 3 September 2013
This article relies largely or entirely on a single source. (June 2013) |
Pike logo | |
Paradigm | multi-paradigm: object-oriented, functional, procedural |
---|---|
Designed by | Fredrik Hübinette |
Developer | Pike development team supported by the Software and Systems division of the Department of Computer and Information Science (IDA) at Linköping University |
First appeared | 1994 |
Stable release | 7.8.700
/ September 16, 2012 |
Typing discipline | static, dynamic, manifest |
OS | Any Unix-like, Windows |
License | GPL/LGPL/MPL |
Website | pike |
Major implementations | |
Pike | |
Influenced by | |
LPC, C, C++ |
Pike is an interpreted, general-purpose, high-level, cross-platform, dynamic programming language, with a syntax similar to that of C. Unlike many other dynamic languages, Pike is both statically and dynamically typed, and requires explicit type definitions. It features a flexible type system that allows the rapid development and flexible code of dynamically typed languages, while still providing some of the benefits of a statically typed language.
Pike features garbage collection, advanced data types, and first-class anonymous functions, with support for many programming paradigms, including object-oriented, functional and imperative programming. Pike is free software, released under the GPL, LGPL and MPL licenses.
History
Pike has its roots in LPC, which was a language developed for MUDs. Programmers at Lysator in Linköping, Sweden, most notably Fredrik Hübinette and Per Hedbor, separated the language and virtual machine from the rest of the MUD driver, and used it as a rapid prototyping language for various applications, calling it LPC4.
LPC's license did not allow use for commercial purposes, and so a new GPL implementation was written in 1994, called µLPC (micro LPC).
In 1996, µLPC was renamed to Pike in order to provide a more commercially viable name. Although the name of the company has changed over the years, the company now known as Roxen Internet Software employed many Pike developers, and provided resources for Pike's development. Roxen is also the name of a web server developed by the company in Pike. In 2002, the programming environment laboratory at Linköping University took over maintenance of Pike from Roxen. Several Pike programmers have found their way to the Linköping office of Opera Software, where the language plays a central role in the server/gateway parts of the Opera Mini application.[1]
Syntax highlights
Hello World
- For an explanation of the tradition of programming "Hello World", see Hello world program.
int main() {
write("Hello world!\n");
return 0;
}
The syntax above requires some explanation. Those who are familiar with C or C++ should pick it up right away.
- The first line contains the main function. This is the first function executed when a program starts. The "int" in front of it tells that a number of type Integer will be returned when the function ends.
- The write function sends a string literal to the standard output buffer, which in most cases is a command line interface.
- The third line returns a number to the caller of a function, in this case the command line interface where zero usually means the program executed without error.
- The curly brackets enclose the function and the semicolon separates statements (meaning the function could have been written on a single line:
int main(){write("Hello world!\n");return 0;}
- The "\n" after "Hello world!" is a newline character.
Data types
The following list shows all the standard data types that Pike provides. Advanced data types such as sequences, queues, heaps, stacks, etc. are available in the ADT module which is included with Pike.
Basic data types:
- int
- float
- string
Container types:
- array
- mapping
- multiset
Other types:
- program (the compiled representation of a class)
- object (an instance of a class)
- function
Pike requires explicit type definitions for all variables. It uses this information to report type errors at compile time. The following code will cause a compile error because the value of the variable "number" must be an integer but the code is attempting to assign floating point and string values to it.
int number; // integer variable, it only accepts integers
number = 5.5; // 5.5 is a floating point value, error
number = "5"; // "5" is a string, not the integer value 5, error
That kind of behavior is traditionally considered restrictive and limiting by proponents of dynamically typed languages. However unlike C, C++, and Java, Pike uses a more flexible type system—specifically, a system of tagged unions. The system allows programmers to declare variables that may contain values of multiple types, something impossible in most of the C-family languages without straying from the bounds of safe usage.
The following demonstrates a variable that can hold either an integer or a floating point number.
int|float number; // integer OR float variable
number = 5; // this is legal
number = 5.5; // this is legal also
Because a variable can be declared as holding many different data types, functions are provided to determine what type of data is currently stored. These functions are all of the form typenamep, as in intp, floatp, stringp, etc.
int|float number;
number = 5;
intp(number); // returns true because number holds an int
floatp(number); // returns false
number = 5.5;
floatp(number); // returns true because number now holds a float
Additionally, there is a special "mixed" data type. That definition allows a variable to hold any kind of data type.
mixed anything;
anything = 5; // anything is now the integer value 5
anything = 5.5; // anything is now the float value 5.5
anything = "5"; // anything is now the string value "5"
In order to convert a value from one type to another, Pike can use an explicit cast:
mixed anything;
anything = (int)5.5; // anything is now the integer value 5
anything = (string)anything; // anything is now the string value "5"
See also
References
- ^ Lextrait, Vincent (2010). "The Programming Languages Beacon, v10.0". Retrieved 14 March 2010.
{{cite web}}
: Unknown parameter|month=
ignored (help)