Oily Rag 1: CD Part 3: Difference between revisions

Jump to navigation Jump to search
m
Reverted edits by Upazixorys (Talk); changed back to last version by Ric
No edit summary
m (Reverted edits by Upazixorys (Talk); changed back to last version by Ric)
 
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 25: Line 24:
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( )'''<br>
system( 'cls' )&lt;br&gt;
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.&lt;br&gt;
''''''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=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|-valign="top" style="background:#d7d7d7"
|'''print''' &quot;Some text\n&quot;;
|'''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=&quot;background:#e6e6e6&quot;
|- style="background:#e6e6e6"
|
|
'''&lt;STDIN&gt;'''
'''<STDIN>'''


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


Line 71: Line 70:
'''''Note'':''' For portability I use only core Perl commands.  
'''''Note'':''' For portability I use only core Perl commands.  


{|cellpadding=&quot;6&quot; cellspacing=&quot;1&quot; style=&quot;background:#444444&quot;
{|cellpadding="6" cellspacing="1" style="background:#444444"
|-style=&quot;background:#d7d7d7&quot;
|-style="background:#d7d7d7"
!Instrustion
!Instrustion
!Perl Input and Output
!Perl Input and Output
|-valign=&quot;top&quot; style=&quot;background:#e6e6e6&quot;
|-valign="top" style="background:#e6e6e6"
|width=&quot;250&quot;|
|width="250"|
'''open(INFILE,$file);'''&lt;br&gt;
'''open(INFILE,$file);'''<br>
'''@some_name=&lt;INFILE&gt;;'''&lt;br&gt;
'''@some_name=<INFILE>;'''<br>
'''close(INFILE);'''  
'''close(INFILE);'''  
|
|
Line 89: Line 88:
'''@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.


='''&lt;INFILE&gt;''' get every line that is associated with handle INFILE and put it into the array some_name.
='''<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=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|-valign="top" style="background:#d7d7d7"
|
|
'''open(OUTFILE,&quot;&gt;$file&quot;);'''&lt;br&gt;
'''open(OUTFILE,">$file");'''<br>
'''&amp;nbsp;foreach $save(@some_name){'''&lt;br&gt;
'''&nbsp;foreach $save(@some_name){'''<br>
'''&amp;nbsp;&amp;nbsp;print OUTFILE $save;'''&lt;br&gt;
'''&nbsp;&nbsp;print OUTFILE $save;'''<br>
'''}'''&lt;br&gt;
'''}'''<br>
'''close(OUTFILE);'''
'''close(OUTFILE);'''
|
|
Line 104: Line 103:
'''$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 &gt; 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 > 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=&quot;background:#e6e6e6&quot;
|- style="background:#e6e6e6"
|
|
'''if($new_drive eq &quot;&quot;){'''&lt;br&gt;
'''if($new_drive eq ""){'''<br>
'''&amp;nbsp;&amp;nbsp;$new_drive = $temp_drive;'''&lt;br&gt;
'''&nbsp;&nbsp;$new_drive = $temp_drive;'''<br>
'''}'''  
'''}'''  
|
|
Line 118: Line 117:


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=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|-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=&quot;background:#e6e6e6&quot;
|- style="background:#e6e6e6"
|
|
'''push(@mpg,&quot;data&quot;);'''
'''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=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|-valign="top" style="background:#d7d7d7"
|
|
'''$str1 = pop(@mpg);'''
'''$str1 = pop(@mpg);'''
Line 135: Line 134:


'''''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=&quot;background:#e6e6e6&quot;
|- 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=&quot;top&quot; style=&quot;background:#d7d7d7&quot;
|-valign="top" style="background:#d7d7d7"
|
|
'''sub update_files {'''
'''sub update_files {'''
Line 154: Line 153:
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&lt;br&gt;
$_[0]=10<br>
$_[1]=20&lt;br&gt;
$_[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&lt;br&gt;
$_[0]=10<br>
$_[1]=20&lt;br&gt;
$_[1]=20<br>
$_[2]=$mpg[0]&lt;br&gt;
$_[2]=$mpg[0]<br>
$_[3]=$mpg[1]&lt;br&gt;
$_[3]=$mpg[1]<br>
$_[4]=$mpg[2]
$_[4]=$mpg[2]
|}
|}

Navigation menu