Hi,

I have been trying to execute this program and i am getting an error. Kindly help me.

#!usr/bin/perl -w

# The filename of the file which contains the protein sequence
$proteinfilename = '/users/sukeerthiteja/desktop/perl/NM_021964fragment.pep';

# First open the file and associate a filehandle with it. for readability purpose lets use the filehandle PROTEINFILE.
open(PROTEINFILE, $proteinfilename);

# Now we do the actual reading of the protein sequence data from the file,by using < > to get
# input from the filehandle. We store the data into variable $protein
$protein = <PROTEINFILE>;

#now that we got our data we can close the file
close PROTEINFILE;

#Print the protein onto the screen
print "Here is the protein:\n\n";

print $protein;

exit;

ERROR:

sukeerthi-tejas-macbook-pro:desktop sukeerthiteja$ perl 1.pl
Here is the protein:

{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350


I am not able to print the protein content on the screen.

Kindly email me to tejaminnu@gmail.com

Thank you
Regards
Teja

You are opening a file without testing if the open was successful. Add an 'or die..' and error message to your open statement so you will know if your open statement failed and have some clue why it didn't work.

Also, you are assuming your file contains only one line of data. In the following, I read and print in a loop while there are any records in the file that I have not already printed.

#!/usr/bin/perl
use strict;
use warnings;

# The filename of the file which contains the protein sequence
my $proteinfilename = 'NM_021964fragment.pep';#File is in my current working directory

#First open the file and associate a filehandle with it.
#For readability and more modern style lets name the filehandle $fh
#and use the 3-parameter version of the open statement.
open(my $fh, '<', $proteinfilename) or die "Failed to open $proteinfilename: $!";

print "Here is the protein:\n\n";
# Now we do the actual reading of the protein sequence data from the file,by using < > to get
# input from the filehandle. We store the data into variable $protein.
# In case there is more than one record in the file, read and print each
# record in a while loop.
while (my $protein = <$fh>){
    #Print the protein onto the screen
    print $protein;
}

#now that we got our data we can close the file
close $fh;

exit;

Hi,

I am still getting this error.


sukeerthi-tejas-macbook-pro:desktop sukeerthiteja$ perl 1.pl
Global symbol "$proteinfilename" requires explicit package name at 1.pl line 8.
Global symbol "$proteinfilename" requires explicit package name at 1.pl line 13.
Global symbol "$proteinfilename" requires explicit package name at 1.pl line 13.
Execution of 1.pl aborted due to compilation errors.

Thank you
Teja

Add the following statement before the line where your first error occurs, (i.e. before line 8): my $proteinfilename; You should always declare lexical variables the first time you refer to them. This is what my does.

This article has been dead for over six months. Start a new discussion instead.