Oily Rag 1: CD Part 3: Difference between revisions

no edit summary
(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 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="6" cellspacing="1" style="background:#444444"
{|cellpadding="6" cellspacing="1" style="background:#444444"
|-style="background:#d7d7d7"
|-style="background:#d7d7d7"
!Instrustion
!Instrustion
!Perl Input and Output
!Perl Input and Output
|-valign="top" style="background:#e6e6e6"
|-valign="top" style="background:#e6e6e6"
|width="150"|
|width="150"|
'''system( )'''<br>
'''system( )'''&lt;br&gt;
system( 'cls' )<br>
system( 'cls' )&lt;br&gt;
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.<br>
''''''cls''':''' Clears the command prompt screen.&lt;br&gt;
''''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="top" style="background:#d7d7d7"
|-valign=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|'''print''' "Some text\n";
|'''print''' &quot;Some text\n&quot;;
|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="background:#e6e6e6"
|- style=&quot;background:#e6e6e6&quot;
|
|
'''<STDIN>'''
'''&lt;STDIN&gt;'''


my $input = <STDIN>;
my $input = &lt;STDIN&gt;;
|
|
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>
When &lt;STDIN&gt; 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.&lt;br&gt;
To capture an input from <STDIN> you assign it to a variable as shown '''$input'''<br>
To capture an input from &lt;STDIN&gt; you assign it to a variable as shown '''$input'''&lt;br&gt;
'''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="top" style="background:#d7d7d7"
|-valign=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|
|
'''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="top"
|-valign=&quot;top&quot;
|style="background:#e6e6e6"|&nbsp;
|style=&quot;background:#e6e6e6&quot;|&amp;nbsp;
|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>
|style=&quot;background:#d2ffc6&quot;|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'''&lt;br&gt;


'''my $some_name = <STDIN>;'''<br>
'''my $some_name = &lt;STDIN&gt;;'''&lt;br&gt;
'''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="6" cellspacing="1" style="background:#444444"
{|cellpadding=&quot;6&quot; cellspacing=&quot;1&quot; style=&quot;background:#444444&quot;
|-style="background:#d7d7d7"
|-style=&quot;background:#d7d7d7&quot;
!Instrustion
!Instrustion
!Perl Input and Output
!Perl Input and Output
|-valign="top" style="background:#e6e6e6"
|-valign=&quot;top&quot; style=&quot;background:#e6e6e6&quot;
|width="250"|
|width=&quot;250&quot;|
'''open(INFILE,$file);'''<br>
'''open(INFILE,$file);'''&lt;br&gt;
'''@some_name=<INFILE>;'''<br>
'''@some_name=&lt;INFILE&gt;;'''&lt;br&gt;
'''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.
='''&lt;INFILE&gt;''' 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="top" style="background:#d7d7d7"
|-valign=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|
|
'''open(OUTFILE,">$file");'''<br>
'''open(OUTFILE,&quot;&gt;$file&quot;);'''&lt;br&gt;
'''&nbsp;foreach $save(@some_name){'''<br>
'''&amp;nbsp;foreach $save(@some_name){'''&lt;br&gt;
'''&nbsp;&nbsp;print OUTFILE $save;'''<br>
'''&amp;nbsp;&amp;nbsp;print OUTFILE $save;'''&lt;br&gt;
'''}'''<br>
'''}'''&lt;br&gt;
'''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 > means that the file is open for writing. Note: When a file is open for writing its contents are destroyed.
'''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 &gt; 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="background:#e6e6e6"
|- style=&quot;background:#e6e6e6&quot;
|
|
'''if($new_drive eq ""){'''<br>
'''if($new_drive eq &quot;&quot;){'''&lt;br&gt;
'''&nbsp;&nbsp;$new_drive = $temp_drive;'''<br>
'''&amp;nbsp;&amp;nbsp;$new_drive = $temp_drive;'''&lt;br&gt;
'''}'''  
'''}'''  
|
|
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="top" style="background:#d7d7d7"
|-valign=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|
|
'''@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="background:#e6e6e6"
|- style=&quot;background:#e6e6e6&quot;
|
|
'''push(@mpg,"data");'''
'''push(@mpg,&quot;data&quot;);'''
|
|
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="top" style="background:#d7d7d7"
|-valign=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|
|
'''$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="background:#e6e6e6"
|- style=&quot;background:#e6e6e6&quot;
|
|
'''@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="top" style="background:#d7d7d7"
|-valign=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|
|
'''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<br>
$_[0]=10&lt;br&gt;
$_[1]=20<br>
$_[1]=20&lt;br&gt;
$_[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<br>
$_[0]=10&lt;br&gt;
$_[1]=20<br>
$_[1]=20&lt;br&gt;
$_[2]=$mpg[0]<br>
$_[2]=$mpg[0]&lt;br&gt;
$_[3]=$mpg[1]<br>
$_[3]=$mpg[1]&lt;br&gt;
$_[4]=$mpg[2]
$_[4]=$mpg[2]
|}
|}
322

edits