You are on page 1of 2

18/09/13

Chapter 1. The First YCP Program

Chapter 1. The First YCP Program The YaST Programming Language - YCP

Chapter 1. The First YCP Program


Table of Contents 1.1. YCP Source 1.2. The YCP com piler 1.3. Running YCP Probably the best w ay to get into the matter is by means of a simple example.

1.1. YCP Source


The follow ing little program opens a w indow that displays the string Hello, World! and provides a push button for termination. Exam ple 1.1. Hello World in YCP { s t r i n gm e s s a g e=" H e l l o ,W o r l d ! " ; U I : : O p e n D i a l o g ( ` V B o x ( ` L a b e l (m e s s a g e) , ` P u s h B u t t o n ( " & O K " ) ) ) ; U I : : U s e r I n p u t ( ) ; U I : : C l o s e D i a l o g ( ) ; }

In the follow ing this code w ill be explained shortly in a line-by-line manner thereby touching some topics w e w ill examine in detail later on. { The opening curly opens a so-called block in Y C P . Blocks are used to glue several Y C P -statements together to form an entity that can be handled just like a single statement. s t r i n gm e s s a g e=" H e l l o ,W o r l d ! " ; In this line w e define a variable named message that is of type string. In Y C Pany variable definition must imply a value assignment to avoid all errors that might occur due to uninitialized variables. Here w e assign the constant string Hello, World!. Furthermore the terminating semicolon is mandatory in Y C Pto indicate the end of a statement (just like C). U I : : O p e n D i a l o g ( This command opens a dialog on screen. Because w e w ant to display something, the code describing our dialog has to be sent to the UI. This is being done by the leading name space identifier U I : : . The (single) parameter that is supplied here determines the content of the dialog. ` V B o x ( This is a UI-statement related to the geometry of the dialog to be defined. As the name indicates it opens a (virtual) vertical box that displays all content in a column-w ise manner. (Geometry management is described in more detail in Chapter 9, Controlling The User Interface). The leading back-quote introduces a Y C P -feature that stems from the functional programming paradigm. In Y C P -speak the ` V B o x ( )is a term. In Y C P , terms are used as a structured constants and are typically passed to functions provided by YaST infrastructure as parameters as is done here w ith O p e n D i a l o g ( ) . ` L a b e l (m e s s a g e) , Displaying strings in Y C Pis done by means of Labels . This statement gets one parameter, the string variable w e defined in the beginning. Because it is the first of tw o parameters passed to ` V B o x ( )this line is not
doc.opensuse.org/projects/YaST/openSUSE11.3/tdg/id_ycp_first.html#id_ycp_hello 1/2

18/09/13

Chapter 1. The First YCP Program

terminated w ith a semicolon but w ith a comma. As in most programming languages commas are used to separate parameters in Y C P . ` P u s h B u t t o n ( " & O K " ) This statement displays a labeled push button. Since it is the next element in the enclosing ` V B o x ( ) , it is displayed immediately below the preceding label. The & in the label string is a Y a S Tfeature declaring the subsequent character to be a key-shortcut. As a result the button can not only be clicked w ith the mouse but also be activated by typing ALT- O. )and ) ; The next tw o lines first close the open ` V B o x ( )and then the open O p e n D i a l o g ( ) . Because ` V B o x ( ) is passed as a parameter to O p e n D i a l o g ( )there is no need to terminate the statement w ith a semicolon. O p e n D i a l o g ( )on the other hand is a statement in the UI and hence must be terminated w ith a semicolon. U I : : U s e r I n p u t ( ) ; Here w e hand over control to the UI w hich then aw aits some sort of user input. In this case it simply w aits for the push button to be pressed by the user. Consequently our program blocks at this point until the user really does it. U I : : C l o s e D i a l o g ( ) ; After all the UI-related action has finished, i.e. w hen U I : : U s e r I n p u t ( )returns, w e w ant to remove the dialog w e just created. This is done here. } Indicating the end of the block, the closing curly bracket ends our little Y C P -program.

The YaST Programming Language - YCP

1.2. The YCP compiler

doc.opensuse.org/projects/YaST/openSUSE11.3/tdg/id_ycp_first.html#id_ycp_hello

2/2