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
Upazixorys (talk | contribs) No edit summary |
m (Reverted edits by Upazixorys (Talk); changed back to last version by Ric) |
||
Line 1: | Line 1: | ||
{{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= | {|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"| | ||
|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 71: | Line 70: | ||
'''''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 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. | ||
=''' | ='''<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){'''<br> | ||
'''& | ''' print OUTFILE $save;'''<br> | ||
'''}''' | '''}'''<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 | '''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;'''<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= | |-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 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= | |- 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 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 | $_[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] | ||
|} | |} |