EHD.analysis") or die... NOTE: If a file by that name already exists it will be overwriten! You could append lines to the end of an existing file: open(OUT, ">>EHD.analysis") or die.. Print to a file (in both cases): print OUT "The mutation is in exon $exonNumber\n"; Reminder: Writing to files no comma here"> EHD.analysis") or die... NOTE: If a file by that name already exists it will be overwriten! You could append lines to the end of an existing file: open(OUT, ">>EHD.analysis") or die.. Print to a file (in both cases): print OUT "The mutation is in exon $exonNumber\n"; Reminder: Writing to files no comma here">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

6a.1 More about files: Globbing. 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "<EHD.fasta"); And then read lines from the filehandle,

Similar presentations


Presentation on theme: "6a.1 More about files: Globbing. 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "<EHD.fasta"); And then read lines from the filehandle,"— Presentation transcript:

1 6a.1 More about files: Globbing

2 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "<EHD.fasta"); And then read lines from the filehandle, exactly like you would from : my $line = ; my @inputLines = ; foreach $line (@inputLines)... Every filehandle opened should be closed: close(IN); Always check the open didn ’ t fail (e.g. if a file by that name doesn ’ t exists): open(IN, "<$file") or die "can't open file $file"; Reminder: Reading files

3 6a.3 Open a file for writing, and link it to a filehandle: open(OUT, ">EHD.analysis") or die... NOTE: If a file by that name already exists it will be overwriten! You could append lines to the end of an existing file: open(OUT, ">>EHD.analysis") or die.. Print to a file (in both cases): print OUT "The mutation is in exon $exonNumber\n"; Reminder: Writing to files no comma here

4 6a.4 Perl allows easy access to the files in a directory by “ globbing ” : In globbing - the * represents any string character. For example, *.* represents all filenames. Note: the “ glob ” gives a list of the file names in the directory. my @files = ; foreach my $fileName (@files) { open(IN, $fileName) or die "can't open file $fileName"; my @lines = ; foreach my $line (@lines) { do something... } } Reading directories no " here

5 6a.5 You can interpolate variables in the glob, as in double-quoted strings: @files = ; If $lesson is 4 then we may get these files in @files : class_ex4.pl class_ex4.1.pl class_ex4.2.pl Reading directories

6 6a.6 Delete a file: unlink ("fred.txt") or die "can't delete fred.txt"; Delete all files in a directory whose name matches a certain “ pattern ” : unlink or die "can't delete files in fred"; (Here – all file names that end with “.txt ” ) Move/rename files: rename ("fred.txt", "friends\\bob.txt") or die "can't move fred.txt"; Manipulating files

7 6a.7 Generally, you can execute any command of the operating system: $systemReturn = system("del fred.txt"); Or: $systemReturn = system("copy fred.txt george.txt"); When checking the value returned by a system call, usually 0 means no errors: if ($systemReturn != 0) { die "can't copy fred.txt"; } Calling system commands

8 6a.8 Common DOS commands: copy Copies one or more files to another location. rename Renames a file or files. del Deletes one or more files md my_dir Make a new directory cd my_dir Change directory rd my_dir Removes a directory. help List all dos commands help dir Get help on a dos command Some system commands

9 6a.9 Class exercise 6a 1.Write a script that prints all the file names in “C:\Perl\lib\”, which their names end with ".pl", to a file named “C:\eclipse\perl_ex\perlFiles.txt". 2*.Change the script of the previous question, such that the directory name and the output file are passed as command-line arguments. 3*. Change the previous script, such that after creating the output file, it is copied to another file, whose name is also passed as a command-line argument.


Download ppt "6a.1 More about files: Globbing. 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "<EHD.fasta"); And then read lines from the filehandle,"

Similar presentations


Ads by Google