Oily Rag 1: CD Part 3: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
(New page: {{Uc nav oily rag 1 CD}} '''Uniform Server running from a CD - Part 3''' My sole intention of Parts 1 and 2 was to show how easy it is to run Uniform Server from a CD. The method I used ...)
(No difference)

Revision as of 21:43, 8 July 2008

MPG UniCenter

MPG UniCenter

Oily Rag: Be prepared to get your hands dirty.

Uniform Server running from a CD - Part 3

My sole intention of Parts 1 and 2 was to show how easy it is to run Uniform Server from a CD. The method I used was to redirect any functions requiring read/write access to a folder located on C drive. A problem arises when you want to use a different drive or folder name, every file modified requires changing to that new path. It's not a major problem all you need to do is perform a global search and replace, none the less a minor irritation.

Mode switching problems

I introduced the concept of mode switching allowing Uniform Server to be run in either of two modes. Although it works it is sadly flawed, not the concept (makes it user-friendlier) but the implementation. Using fixed file substitution (swapping) does not cater for any new changes made to working files. These changes are not reflected in either of the switching mode files. Selecting a mode will destroy any changes because files are overwritten.

In Part 1 I avoided this issue with this statement “an alternative to hard coding is to use search and replace however that's just added complexity keep it simple is my motto” neat trick because I was looking for a batch file solution knowing full well search and replace is extremely difficult using batch files.

Path to a real plugin

To produce a real plugin this article addresses the above issues. It uses a mix of batch files and Perl scripts. I don’t know why Perl has a reputation for being difficult to use but it does and was my prime reason for not using it in the previous article. Perl like any other scripting language can be as simple or as complex as you like to make it. I hope at the end of this tutorial you will view Perl in a new light, it really is a powerful friend.

Top

Starting point

We already know which files to target and the lines that need modification. This solution leaves original files in place thus avoiding the swapping problem. Files remain in situ and are changed directly using Perl. This has the advantage any changes made using apanel (us-mode) will now be captured when burning a CD.

This dynamic mode switching has one more advantage; we can offer a user an option of selecting a different location (drive and folder) for the temporary folder or use a default folder.

Path to a real plugin

Perl Commands

Before looking at any scripts I cover the Perl commands used. In many ways and because of its origin Perl is nothing more than a glorified command processor, admittedly a very powerful one. I find interactive input-output always poses a problem hence cover this first. The following four commands are more than adequate for most scripts.

Instrustion Perl Input and Output

system( )
system( 'cls' )
system( 'pause' )

system(): is a function that will execute any command you would normally type at a command prompt. Enclose the command between quotes.

'cls': Clears the command prompt screen.
'pause': This halts a script and prompts a user with the message Press any key to continue....

print "Some text\n"; Prints whatever is between the two quotes, \n start on a new line.

<STDIN>

my $input = <STDIN>;

When <STDIN> is executed it gets characters from the keyboard and stops when the enter key is pressed. The enter key itself is included as a character.
To capture an input from <STDIN> you assign it to a variable as shown $input
my is not required for scripts run directly however when initiated from a batch file my forces the variable to be local to the script. Hence my is required to capture a variable, otherwise the script swans off.

chomp $input;

In most situations you do not want that enter key character (mentioned above) hence chomp removes it giving you a clean string.
  The above looks confusing lets look at a real example. I want a variable that contains only the characters for a person’s name, the variable is $some_name

my $some_name = <STDIN>;
chomp $some_name;

Just two lines of neat code

Using the above commands you can provide some neat dialogue between users and scripts. No fancy Windows things, just a simple text interface using the command prompt window.

Other commands used, these hide dirty tricks that go on behind the scenes!Well thought I would make it sound dramatic.

Note: For portability I use only core Perl commands.

Instrustion Perl Input and Output

open(INFILE,$file);
@some_name=<INFILE>;
close(INFILE);

These commands read the content of a text file into an array.

$file is a variable containing the complete path to the file including its name.

open() is the function that opens a file, INFILE is referred to as an handle you can name it whatever you like so long as it is unique. It contains information about the file that Perl can use latter.

@some_name @ creates an array who's name is some_name you can use whatever you like for some_name so long as it is unique.

=<INFILE> get every line that is associated with handle INFILE and put it into the array some_name.

close(INFILE) Just closes the file associated with handle INFILE and cleans up.

open(OUTFILE,">$file");
 foreach $save(@some_name){
  print OUTFILE $save;
}
close(OUTFILE);

These commands write the content of an array line by line into a text file.

$file is a variable containing the complete path to the file including its name.

open() is the function that opens a file, OUTFILE is referred to as an handle you can name it whatever you like so long as it is unique. It contains information about the file that Perl can use latter. The > means that the file is open for writing. Note: When a file is open for writing its contents are destroyed.

foreach $save(@some_name) foreach @some_name scans the array @some_name line by line after reaching the last line continues on to the next command. Every time a line is scanned it is read from the array and placed into the variable $save overwriting its previous content.

print OUTFILE $save This line is placed within the foreach loop. It prints (copies) the content of variable $save to the file associated with handle OUTFILE. Hence every line in the array is written to the file.

if($new_drive eq ""){
  $new_drive = $temp_drive;
}

If the condition within the brackets is true execute the commands within the curly braces otherwise continue.

In this example the variable $new_drive is checked to see it it is equal to the string in the quotes. If it is true then a drive letter has not been set to give it a value the $temp_drive is assigned to it.

@mpg=();

The quickest way to clear an array is to assign nothing to it. No content in the brackets.

push(@mpg,"data");

There are several ways to assign data to an array. Push is a function that takes two parameters, the first is the array to work on @mpg followed by the data to store. Data is stored in the next available location, arrays automatically expand to accommodate the data.

$str1 = pop(@mpg);

Pop is the reverse of push, it extracts one line from the array and can be assigned to a variable. The array automatically contracts once the data has been removed (popped)

Note: The data popped is the last one that was pushed into the array. Last in first out.

@mpg=reverse(@mpg);

The reverse function when applied to an array reveres the order of its content. It makes a temporary copy hence the reason to assigning it to itself. If you apply this command before popping data you now get what was first pushed into the array.

sub update_files {

}

Sub (functions) are pieces of reusable code that you place inside the curly braces. Each sub must have a unique name for example update_files with no brackets after the name.

To call a sub from your main code add bracketes to the name for example update_files()

Passing parameters to a sub use a comma seperated list fred(10,20,30);

These values get passed as an array named @_ you access these values using $_[0] etc these variable contain the following values:

$_[0]=10
$_[1]=20
$_[2]=30

You can pass arrays for example fred(10,20,@mpg) these get passed to the array @_ what I find a pain, arrays gets flattened. To access values within your sub, now looks like this:

$_[0]=10
$_[1]=20
$_[2]=$mpg[0]
$_[3]=$mpg[1]
$_[4]=$mpg[2]

Summary

Real reason for listing the above commands apart from making my scripts easier to read you now know what to search for on the Internet. There are some great tutorials out there that are worth reading.

Time to look at USCD2 plugin.

Top


Ric