Interpolation of an uninterpolated passed string inside a routine in Perl

I am trying to parse a tab delimited text file into a nested hash using a routine. Each line of the file will be entered using a unique identifier from the uid column, and the title line as nested keys. Which column (s) is (is) to become uid changes (since sometimes there is no unique column, so uid must be a combination of columns). My problem is related to the variable $uidthat I pass as a non-interpolated string. When I try to use it inside a subprogram in an interpolated way, it will give me only an uninterpreted value:

    use strict;
    use warnings;

    my $lofrow = tablehash($lof_file, '$row{gene}', "transcript", "ENST");

    ##sub to generate table hash from file w/ headers
    ##input values are file, uid, header starter, row starter, max column number
    ##returns hash reference (deref it)
    sub tablehash   { 
        my ($file, $uid, $headstart, $rowstart, $colnum) = @_;
        if (!$colnum){ # takes care of a unknown number of columns
            $colnum = 0;
        }
        open(INA, $file) or die "failed to open $file, $!\n";
        my %table; # permanent hash table 
        my %row; # hash of column values for each row
        my @names = (); # column headers
        my @values = (); # line/row values
        while (chomp(my $line = <INA>)){ # reading lines for lof info
            if ($line =~ /^$headstart/){
                @names = split(/\t/, $line, $colnum);
            } elsif ($line =~ /^$rowstart/){ # splitting lof info columns into variables
                @values = split(/\t/, $line, $colnum);
                @row{@names} = @values;
                print qq($uid\t$row{gene}\n); # problem: prints "$row{gene} ACB1"
                $table{"$uid"} = { %row }; # puts row hash into permanent hash, but with $row{gene} key)
            }
        }
        close INA;
        return \%table;
    }

. $table{$row{$uid}} "gene", $uid of "$row{gene}|$row{rsid}", $table{ACB1|123456}

+4
1

- Perl. -

"foo $bar baz"

Perl -

'foo ' . $bar . ' $baz'

.

, , , $, .


- , . - , . ( , , - .)

my $lofrow = tablehash($lof_file, sub { my ($row) = @_; $row->{gene} }, "transcript", "ENST");

sub tablehash   { 
    my ($file, $mkuid, $headstart, $rowstart, $colnum) = @_;    
    ...
                my $uid = $mkuid->(\%row);
                $table{$uid} = { %row };

$mkuid , , ( -) uid. tablehash , %row. , ,

my $lofrow = tablehash($lof_file, sub { my ($row) = @_; "$row->{gene}|$row->{rsid}" }, "transcript", "ENST");

- , :

my $lofrow = tablehash($lof_file, "gene|rsid", "transcript", "ENST");

sub tablehash   { 
    my ($file, $uid_template, $headstart, $rowstart, $colnum) = @_;    
    ...
                (my $uid = $uid_template) =~ s/(\w+)/$row{$1}/g;
                $table{$uid} = { %row };

s/// %row.


:

  • strict warnings.
  • if (!$colnum) { $colnum = 0; } $colnum ||= 0;.
  • . - ( , ).
  • , .
  • ( $0, , \n die).
  • my @foo = (); my %bar = (); my @foo; my %bar;. ; .
  • chomp(my $line = <INA>) , EOF ( , undef).
  • my %row;, , . , .

:

open my $fh, '<', $file or die "$0: can't open $file: $!\n";
while (my $line = readline $fh) {
    chomp $line;
    ...
}
+3

Source: https://habr.com/ru/post/1677509/


All Articles