0

This is SeqIO.pm

package SeqIO;
use strict;
use Carp;
use warnings;
use vars('@ISA');
use vars('@EXPORT_OK');
require Exporter;

@ISA = qw(Exporter);
@EXPORT_OK = qw(readSeq writeSeq);

sub readSeq {
    my ($var1)= @_;
    print "$var1\n";
    open IN, '<$var1' or die "Cannot open file : $!";
    while(<IN>) {
        chomp $_;
        print "$_\n";   
    }
    close IN
}

sub writeSeq {}

sub new {
    my $this = {};
    bless $this;
    return $this;
}

1;

Test.pl call SeqIO.pm

use strict;
use SeqIO;
use warnings;
my $path_fasta=q/D:\360Downloads\A1.fasta/;
my $seqio = new SeqIO;

$seqio->readSeq($path_fasta);

BUT when i use readSeq it shows SeqIO=HASH(0x38ba34), anything wrong?

1
  • Aside: There should be a local $_; in there to avoid clobbering data in the caller. Commented Mar 19, 2014 at 11:33

1 Answer 1

4

When you call a Perl subroutine as a method using the -> operator, the invocant, meaning the thing on the left side of -> is passed into the subroutine as the first parameter. So change your method to this:

  sub readSeq {
      my ($self, $var1) = @_;
      print "$var1\n";
      open my $in, '<', $var1 or die "Cannot open file : $!";

      while(<$in>) {
          chomp $_;
          print "$_\n";   
      }
  }

I've also changed your filehandle to a lexical variable instead of a global symbol, and changed your open call to the three-argument version which is better.

There's also no need to export readSeq and writeSeq if you intend to use those as object methods. Exporting is only for when you want to modify the client code's namespace. So you can delete all of this:

use vars('@ISA');
use vars('@EXPORT_OK');
require Exporter;

@ISA = qw(Exporter);
@EXPORT_OK = qw(readSeq writeSeq);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.