#!/usr/bin/perl -w # # Documentation of program in pod format # =pod =head1 Perl template. Fasta splitter Revision History $Log: template.pl,v $ Revision 1.3 2002/03/05 13:23:45 lund Revision history added =cut # # Import libraries # use Getopt::Std; # Command line options use File::Path; # mkpath, rmtree commands use strict; # Strict parsing use English; # Allow use of long names (Camel p. 127) use lib "$ENV{VACHOME}/lib/perl"; # Path to perl modules # # Get command line options. Each command line option must be present in the # Use vars line and in the getoptys line. colon after an option means that it # takes an argument. # use vars qw($opt_h $opt_o $opt_f); #### Declaration of the variables where the options are stored getopts('hf:o:')||usage(); #### Options taken from commandline with preceding - #### Colon (:) after an option means that a value is read off the commandline #### No colon means that the option is an off/on switch # # usage prints a description of how to use the program # sub usage{ print("Usage: $0 \[-h\] \[-o outputfile\] \[-f definition_file\] [inputfile(s)]\n"); print("If no inputfiles are given data is read from standard in\n"); print("Command line options:\n"); print(" h: Print this meassage\n"); print(" f: File to read extra stuff from\n"); print(" o: Write to output file. Default is standard out\n"); exit; } # # If -h option is used, print description of usage # if (defined($opt_h)) {usage()}; # # Open outputfile or write to std out # if (not defined($opt_o)) {$opt_o="-";} ### if no -o is set on the commandline $opt_o contains the string "-"; open(OUT, ">$opt_o") or die "Can't open $opt_o for writing: $!"; ### If a file with the name $opt_o cannot be opened ### the program dies with an error message ### $! contains error messages from the system # # Open xtra input file () # if (defined($opt_f)) { open(IN, "<$opt_f") or die "Can't open $opt_f for reading: $!"; } ################################################################################ # # Parse input # ################################################################################ # my @fields; ########## Declaration of an array my @words; ########## Another array my $var; ########## Declaration of a variable; while(){ #### Reads the current line of the file print "$_"; #### print the default input variable to stdout } if(defined($opt_f)){ #### If an input file is defined on the commandline while(!eof(IN)){ #### Continue until the end of the file $var=; #### Transfer the content of the current line to #### the variable $var and shift to the next line #### of the file $_=$var; @fields = split; #### Split the default input variable $_ on one or more #### whitespaces but ignores any preceeding whitespaces @words=split/\s/,$var; #### Split the variable $var on one or more #### whitespaces including any preceeding whitespaces foreach my $oneword (@fields){ #### The content of each defined position in the array #### is transfered to the variable just declared printf OUT "%s ", $oneword ; #### Print the variable $oneword to the file OUT } printf OUT "%s", $_; #### Print the variable $_ (i.e. the whole line) to the file OUT foreach my $aword (@words){ #### The content of each defined position in the array #### is transfered to the variable just declared printf OUT "%s ", $aword ; #### Print the variable $oneword to the file OUT } printf OUT "%s", $_; #### Print the variable $_ (i.e. the whole line) to the file OUT } } my $cmd ="echo \"Ho Ho\nHa Ha\n\""; system("$cmd"); #### Run the echo command (could be any executable) open(HO, "$cmd |"); #### Run the echo command (could be any executable) #### and pipe it to the filehandle #### Works as if the output of the command was written #### to a file, and the file then opened for reading. my @lines=; #### Read all lines from the files and transfer #### those the array (one line per place starting at 0) close(HO); #### Close the file for(my $n=0; $n