2

Can you show me how to create a subroutine or function using this code? Basically I want to make my code into a subroutine so I'll be able to re-use it without making my script too long.

Here is my script:

#!/usr/local/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Carp qw(croak);

my @fields;
my ($tmp_var, $rec_type, $country, $header, $Combline, $records, $line);
my $filename = 'data5.txt';

open (my $input_fh, '<', $filename ) or croak "Can't open $filename: $!";
open  my $OUTPUTA, ">", 'drp1.txt' or die $!;
open  my $OUTPUTB, ">", 'drp2.txt' or die $!;
while (<$input_fh>) {
    $line = _trim($_);  
    @fields = split (/\|/, $line);  
    $rec_type = $fields[0];
    $country = $fields[1];
    my $string = substr $fields[1], 0, 1;
    $header = $line if(/^INVHDR/);  

    if ($rec_type eq 'INVDET') {                                     
        if ($string eq 'I')  {           
            $records = $header . $line;                 
            print  $OUTPUTA $records, scalar <$input_fh>;               
        }
        else {           
            $records = $header . $line;
            print  $OUTPUTB $records, scalar <$input_fh>;
        }      
    }     
}   

close $OUTPUTA or die $!;
close $OUTPUTB or die $!;

sub _trim {
    my $word = shift;
    if ( $word ) {      
        $word =~ s/\s*\|/\|/g;      #remove trailing spaces
        $word =~ s/"//g;            #remove double quotes
    }
    return $word;
}

This is the part of the script that I wanted to put in a subroutine or function:

$line = _trim($_);  
@fields = split (/\|/, $line);  
$rec_type = $fields[0];
$country = $fields[1];
my $string = substr $fields[1], 0, 1;
$header = $line if (/^INVHDR/);

if ($rec_type eq 'INVDET') {                                     
    if ($string eq 'I')  {           
        $records = $header . $line;                 
        print $OUTPUTA $records, scalar <$input_fh>;                
    }
    else {
        $records = $header . $line;
        print $OUTPUTB $records, scalar <$input_fh>;
    }
}     

1 Answer 1

1

I would suggest breaking it out a little differently and expand on your _trim function, turning it into a parse function:

use strict;
use warnings;

open( my $input_fh, '<', 'data5.txt' ) or die "Can't open $filename: $!";
open( my $OUTPUTA, '>', 'drp1.txt' ) or die $!;
open( my $OUTPUTB, '>', 'drp2.txt' ) or die $!;

my $header = '';
while (<$input_fh>) {
    if ($_ =~ /^INVHDR/) {
        $header = $_;
    }
    if ($_ =~ /^INVDET/) {
        my @data = parse($_);
        my $line = $header . join('|', @data);
        # scalar <$input_fh> is almost certainly not doing what you expect, 
        # though I'm not sure what you're try to accomplish with it
        if ( $data[1] =~ /^I/ ) {
            print $OUTPUTA $line;
        } else {
            print $OUTPUTB $line;
        }
    }
}

sub parse {
    my $input = shift || return;
    my $input =~ s/"//g; # remove double quotes
    # Here I've combined the removal of trailing spaces with the split.
    my @fields = split( m{\s*\|}, $input );
    return @fields;
}
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.