Oily Rag 1: CD Part 3: Difference between revisions
(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 ...) |
Upazixorys (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
=[http://abaviteha.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]= | |||
{{Uc nav oily rag 1 CD}} | {{Uc nav oily rag 1 CD}} | ||
'''Uniform Server running from a CD - Part 3''' | '''Uniform Server running from a CD - Part 3''' | ||
Line 24: | Line 25: | ||
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. | 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. | ||
{|cellpadding= | {|cellpadding="6" cellspacing="1" style="background:#444444" | ||
|-style= | |-style="background:#d7d7d7" | ||
!Instrustion | !Instrustion | ||
!Perl Input and Output | !Perl Input and Output | ||
|-valign= | |-valign="top" style="background:#e6e6e6" | ||
|width= | |width="150"| | ||
'''system( )''' | '''system( )'''<br> | ||
system( 'cls' ) | system( 'cls' )<br> | ||
system( 'pause' ) | system( 'pause' ) | ||
| | | | ||
'''''system()'':''' is a function that will execute any command you would normally type at a command prompt. Enclose the command between quotes. | '''''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. | ''''''cls''':''' Clears the command prompt screen.<br> | ||
''''pause':''' This halts a script and prompts a user with the message '''Press any key to continue....''' | ''''pause':''' This halts a script and prompts a user with the message '''Press any key to continue....''' | ||
|-valign= | |-valign="top" style="background:#d7d7d7" | ||
|'''print''' | |'''print''' "Some text\n"; | ||
|Prints whatever is between the two quotes, \n start on a new line. | |Prints whatever is between the two quotes, \n start on a new line. | ||
|- style= | |- style="background:#e6e6e6" | ||
| | | | ||
''' | '''<STDIN>''' | ||
my $input = | my $input = <STDIN>; | ||
| | | | ||
When | 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.<br> | ||
To capture an input from | To capture an input from <STDIN> you assign it to a variable as shown '''$input'''<br> | ||
'''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. | '''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. | ||
|-valign= | |-valign="top" style="background:#d7d7d7" | ||
| | | | ||
'''chomp''' $input; | '''chomp''' $input; | ||
|In most situations you do not want that enter key character (mentioned above) hence chomp removes it giving you a clean string. | |In most situations you do not want that enter key character (mentioned above) hence chomp removes it giving you a clean string. | ||
|-valign= | |-valign="top" | ||
|style= | |style="background:#e6e6e6"|&nbsp; | ||
|style= | |style="background:#d2ffc6"|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'''<br> | ||
'''my $some_name = | '''my $some_name = <STDIN>;'''<br> | ||
'''chomp $some_name;''' | '''chomp $some_name;''' | ||
Line 70: | Line 71: | ||
'''''Note'':''' For portability I use only core Perl commands. | '''''Note'':''' For portability I use only core Perl commands. | ||
{|cellpadding= | {|cellpadding="6" cellspacing="1" style="background:#444444" | ||
|-style= | |-style="background:#d7d7d7" | ||
!Instrustion | !Instrustion | ||
!Perl Input and Output | !Perl Input and Output | ||
|-valign= | |-valign="top" style="background:#e6e6e6" | ||
|width= | |width="250"| | ||
'''open(INFILE,$file);''' | '''open(INFILE,$file);'''<br> | ||
'''@some_name= | '''@some_name=<INFILE>;'''<br> | ||
'''close(INFILE);''' | '''close(INFILE);''' | ||
| | | | ||
Line 88: | Line 89: | ||
'''@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. | '''@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'''. | '''close(INFILE)''' Just closes the file associated with handle INFILE and '''cleans up'''. | ||
|-valign= | |-valign="top" style="background:#d7d7d7" | ||
| | | | ||
'''open(OUTFILE, | '''open(OUTFILE,">$file");'''<br> | ||
''' foreach $save(@some_name){''' | '''&nbsp;foreach $save(@some_name){'''<br> | ||
''' print OUTFILE $save;''' | '''&nbsp;&nbsp;print OUTFILE $save;'''<br> | ||
'''}''' | '''}'''<br> | ||
'''close(OUTFILE);''' | '''close(OUTFILE);''' | ||
| | | | ||
Line 103: | Line 104: | ||
'''$file''' is a variable containing the complete path to the file including its name. | '''$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 | '''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. | '''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. | '''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. | ||
|- style= | |- style="background:#e6e6e6" | ||
| | | | ||
'''if($new_drive eq | '''if($new_drive eq ""){'''<br> | ||
''' $new_drive = $temp_drive;''' | '''&nbsp;&nbsp;$new_drive = $temp_drive;'''<br> | ||
'''}''' | '''}''' | ||
| | | | ||
Line 117: | Line 118: | ||
In this example the variable $new_drive is checked to see it it is '''eq'''ual 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. | In this example the variable $new_drive is checked to see it it is '''eq'''ual 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. | ||
|-valign= | |-valign="top" style="background:#d7d7d7" | ||
| | | | ||
'''@mpg=();''' | '''@mpg=();''' | ||
| | | | ||
The quickest way to clear an array is to assign nothing to it. No content in the brackets. | The quickest way to clear an array is to assign nothing to it. No content in the brackets. | ||
|- style= | |- style="background:#e6e6e6" | ||
| | | | ||
'''push(@mpg, | '''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. | 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. | ||
|-valign= | |-valign="top" style="background:#d7d7d7" | ||
| | | | ||
'''$str1 = pop(@mpg);''' | '''$str1 = pop(@mpg);''' | ||
Line 134: | Line 135: | ||
'''''Note'':''' The data popped is the last one that was pushed into the array. Last in first out. | '''''Note'':''' The data popped is the last one that was pushed into the array. Last in first out. | ||
|- style= | |- style="background:#e6e6e6" | ||
| | | | ||
'''@mpg=reverse(@mpg);''' | '''@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. | 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. | ||
|-valign= | |-valign="top" style="background:#d7d7d7" | ||
| | | | ||
'''sub update_files {''' | '''sub update_files {''' | ||
Line 153: | Line 154: | ||
These values get passed as an array named '''@_''' you access these values using '''$_[0]''' etc these variable contain the following values: | These values get passed as an array named '''@_''' you access these values using '''$_[0]''' etc these variable contain the following values: | ||
$_[0]=10 | $_[0]=10<br> | ||
$_[1]=20 | $_[1]=20<br> | ||
$_[2]=30 | $_[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: | 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 | $_[0]=10<br> | ||
$_[1]=20 | $_[1]=20<br> | ||
$_[2]=$mpg[0] | $_[2]=$mpg[0]<br> | ||
$_[3]=$mpg[1] | $_[3]=$mpg[1]<br> | ||
$_[4]=$mpg[2] | $_[4]=$mpg[2] | ||
|} | |} |
Revision as of 10:46, 24 November 2010
UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY
Oily Rag 1: Introduction | CD Part 1 | CD Part 2 | CD Part 3 | CD Part 4 | CD Part 4 Scripts | CD Part 5 | CD Part 6 | Downloads |
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.
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( )<br> system( 'cls' )<br> 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.<br> '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.<br> To capture an input from <STDIN> you assign it to a variable as shown $input<br> 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<br>
my $some_name = <STDIN>;<br> 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);<br> @some_name=<INFILE>;<br> 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");<br> foreach $save(@some_name){<br> print OUTFILE $save;<br> }<br> 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 ""){<br> $new_drive = $temp_drive;<br> } |
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<br> $_[1]=20<br> $_[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<br> $_[1]=20<br> $_[2]=$mpg[0]<br> $_[3]=$mpg[1]<br> $_[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.
Ric |