lomSpace has asked for the wisdom of the Perl Monks concerning the following question:

Hello, How do you print the return value from a subroutine to a file?
Particularly, when you are using pass by value.
#!/usr/bin/perl -w use strict; use FileHandle; use Data::Dumper; open(my $in1, "/Users/mydir/Desktop/hw3_infile1.txt"); open(my $report, ">/Users/mydir/Desktop/hw3_report.txt"); my @aa_seqs = ("MPIGSKERPTFFEIFKTRCNKADLGPIS", "MDLSALRVEEVQNVINAMQKIL +ECPIC", "MDLSAVQIQEVQNVLHAMQKILECPICLE"); my @nt_seqs = ("atgccgctaccgt", "cgcctagcgccgcatta","ccgtaggcattc", "g +acctaggtcag"); #call subroutines #pass by value $report print $report "Values returned via pass by value:\n"; my @oligo_array = get_oligo_seqs($in1); # pass by value print "@oligo_array\n"; close($in1); #print results print $report "Before calling subroutine:\n"; for my $aa_seqs(@aa_seqs){ print $report "$aa_seqs\n"; } print $report "\n"; for my $nt_seqs(@nt_seqs){ print $report "$nt_seqs\n"; } print $report "\n"; #pass by reference change_array(\@aa_seqs, \@nt_seqs); #print results print $report "After calling subroutine:\n"; for my $aa_seqs(@aa_seqs){ print $report "$aa_seqs\n"; } print $report "\n"; for my $nt_seqs(@nt_seqs){ print $report "$nt_seqs\n"; } close($report); ######## Subroutines ######### sub get_oligo_seqs{ my ($fh) = shift; while(<$fh>){ my @fields = split /\t/; my $oligos = $fields[1]; print "$oligos\n"; } } sub change_array{ my ($aa, $nt) = @_; push(@$nt, 'ggacttacgggccatataa'); shift(@$aa); }

Replies are listed 'Best First'.
Re: printing sub return values to a file
by ikegami (Patriarch) on Nov 19, 2009 at 07:03 UTC
    You write to a file using
    print $fh EXPR;

    EXPR can be any expression, including a subroutine call.

Re: printing sub return values to a file
by cdarke (Prior) on Nov 19, 2009 at 08:41 UTC
    So far as I can see your subroutines do not explicitly return anything. With no return statement they will return the value of the last statement within them. That's fine for change_array, but the line my @oligo_array = get_oligo_seqs($in1); looks suspicious to me, since get_oligo_seqs does not return an array (I think it will give a single element containing an empty string).

    I wonder if you are confusing the print statement with a return?
      I think it will give a single element containing an empty string
      Not that it much matters, but it probably returns 1 (the return value of a successful print). The exceptions are if print fails for some reason, or if the file handled by $fh is empty—in which latter case I would have guessed that the return value would be $fh, but Data::Dumper suggests that it's ''—or, I suppose, if $fh isn't a readable filehandle, in which case something else happens.
      Hi cdarke!
      I made a change to the subroutine, but it only returns the first element.
      Any ideas on how I can get around that to return the all of the elements?
      LomSpace
        The following will loop through all lines of the input file (assuming you opened the file prior to calling the sub), populate the array, then return the array. I think your code was exiting the sub after reading only one line.
        sub get_oligo_seqs{ my ($fh) = shift; my @seqs; while(<$fh>){ my @fields = split /\t/; my $oligos = $fields[1]; push(@seqs,($oligos)); } return @seqs; }