Creating a Managed Exe Application

As our first exercise in using Visual Studio, we create a simple Managed Exe application. Our program Echo will prompt the user for her name and then display a personalized greeting. If you want to follow along on your PC as you read, you can use the Demos directory for this chapter. A final version can be found in the Echo directory.


Creating a Visual Perl Project

When you install Visual Perl, you will be able to create Visual Perl projects in Visual Studio as well as in Visual C#, Visual Basic .NET, and Visual C++ projects. There are different kinds of Visual Perl projects. We will illustrate with a Managed Exe project, which will build an executable file consisting of IL instructions, as discussed in Chapter 1.

1.
From Visual Studio main menu choose File | New | Project.... This will bring up the New Project dialog.

2.
For Project Types choose Visual Perl Projects and for Templates choose Managed Exe Project.

3.
Click the Browse button, navigate to Demos, and click Open.

4.
In the Name field, type Echo. See Figure A-6. Click OK.

Figure A-6. Creating a Managed Exe Visual Perl project.


Building the Starter Project

At this point you will have a Visual Perl project that has some starter code. You can examine the code file Echo.pl, as illustrated in Figure A-7. You will see code for using PerlNET followed by code to print out the message “Hello, World.”

Figure A-7. A Visual Perl starter project.


You can build the project by using one of the following:

  • Menu Build | Build

  • Toolbar

  • Keyboard shortcut Ctrl + Shift + B

Running the Program

You can run the program by using one of the following:

  • Menu Debug | Start Without Debugging

  • Toolbar

  • Keyboard shortcut Ctrl + F5

You will see output displayed in a special Run window that opens up, as illustrated in Figure A-8. When you are done inspecting the output, you can close this window in the usual way (click on the “x” at the top-right). If you run the project again, you will see the Run window displayed again, showing both the previous run and the current run.

Figure A-8. Run window shows output from a Visual Perl program.


Adding a File

The starter program prints out a fixed greeting message. We want to create a program that will prompt a user for her name, and then echo back her name in a greeting message. We could of course just write a little code inline to prompt for the name and read it in. But we want to illustrate a simple project with more than one source file, so we use the InputWrapper.pl file illustrated earlier.

1.
Copy the file InputWrapper.pl from TestInputWrapper to DemosEcho.

2.
In Solution Explorer, right-click over Echo and choose Add | Add Existing Item from the context menu.

3.
In the dialog that comes up, navigate to the file you want to add. (Because of the copy you did in Step 1, the desired file InputWrapper.pl should be immediately available. Double-click on the file to add it to the project.

You should now see the second file in Solution Explorer.

Using the Visual Studio Text Editor

In Solution Explorer double-click on Echo.pl. This will open up the generated file Echo.pl in the Visual Studio text editor. Type in the lines shown in bold, and notice things like color syntax highlighting as you type. (We have deleted some of the comments in the generated code, and we show the file name as the first comment.)

# Echo.pl
#

use strict;
use namespace "System";
use PerlNET qw(AUTOCALL);
require "InputWrapper.pl";

Console->WriteLine("Hello, World.
");
my $name = GetString("Your name: ");
						Console->WriteLine("Hello, {0}", $name);
					

Besides the color syntax highlighting, other features include automatic indenting and putting in a closing right curly brace to match the left curly brace you type. All in all, you should find the Visual Studio editor friendly and easy to use. If you run the program, you should see output in the Run window that is similar to what is shown:

Hello, World.

Your name: Jill
Hello, Jill

======================================================

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.183.24