Hey Femi,

Instructions and example code for doing all the things you want can be found in the standard Perl manual. You should be able to find it by opening a terminal window and using the perldoc command:

perldoc -f open
perldoc -f chdir
perldoc -q path

This is best, because the documentation you find will match your version of Perl.

Alternatively, you can see the same things on the web (by default for the latest stable version) at:

http://perldoc.perl.org/functions/open.html
http://perldoc.perl.org/functions/chdir.html
http://perldoc.perl.org/search.html?q=path

Read those, and try and write some code that solves your problem. If you have further trouble, feel free to email the list including the code you have tried and I or others here will be more than happy to assist.

Good luck,
Michael


On 11/13/2013 07:45 AM, Alaba, Oluwafemi (IITA) wrote:
Dear Michael,
Thanks for your kind explanation. I understood it pretty well. I feel very fortunate to have you respond to my post.

I just wanna know if i must save that file ("NM021964fragment.pep") in a particular location in my OS X to run my scripts properly. It is not yet running.

Could you please write the lines of code exactly how I should run it? And would like to hear further instructions and recommendations.

Kind regards,
Femi

On Nov 12, 2013, at 1:08 AM, Michael Brader <mbra...@internode.com.au <mailto:mbra...@internode.com.au>> wrote:

Nathan has found your problem, but see below for more suggestions on your code:

On 11/12/2013 02:15 AM, Alaba, Oluwafemi (IITA) wrote:
*Dear ALL,*

I created a file named NM021964fragment.pep (using text editor) but I could not read that particular file.

*Here is the script I used:*

#!/usr/bin/perl -w

Remove the ' -w' above and add the following two lines to get extra error checking that will lead you towards better Perl programming habits

    #!/usr/bin/perl

    use strict;
    use warnings;

#Reading protein sequence from a file

The filename of the file containing the protein sequence data
$proteinfilename = "NM021964fragment.pep";

#First we have to 'open' the file and associate it a 'filehandle' with it. We choose the filehandle
#PROTEINFILE for readability.

open(PROTEINFILE, $proteinfilename);

PROTEINFILE is known as a 'bareword filehandle' and it has some limitations. Better to use a filehandle variable. The '<' indicates that you are opening the file for reading. It is the default but using it tells someone reading your code what your intentions are. You should also check to see whether the open succeeded:

    open my $PROTEINFILE, '<', $proteinfilename
        or die "Failed to open protein file $proteinfilename. Reason: $!";


#now we do the actual reading of the protein sequence data from the file by using angle bracket < and >
$protein = <PROTEINFILE>;

This will read the first line of the file including the trailing newline. If that is what you want, fine. The 'strict' line you inserted above means that you will have to declare variables on their first use. To remove the trailing newline use chomp:

    my $protein = <$PROTEINFILE>;
    chomp $protein;

If what you wanted is to read all the lines of the file, it is customary to use a while loop:

    while ( my $protein = <$PROTEINFILE> ) {
        chomp $protein;
        # do something with $protein
    }


close PROTEINFILE;

You should always check the return value of system calls such as close. It's unlikely after reading but a big problem after writing:

close $PROTEINFILE
    or warn "Problem closing protein file $proteinfilename. Reason: $!";

Cheers,
Michael
--
Michael Brader                    Senior Software Engineer and Perl Person
There's no such thing as a DevOps team           Technology/Softdev/DevOps
Internodehttp://internode.on.net/           mbra...@internode.com.au
iiNethttp://iinet.net.au/          m.bra...@staff.iinet.net.au


--
Michael Brader                    Senior Software Engineer and Perl Person
There's no such thing as a DevOps team           Technology/Softdev/DevOps
Internode       http://internode.on.net/          mbra...@internode.com.au
iiNet             http://iinet.net.au/         m.bra...@staff.iinet.net.au

Reply via email to