Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Out of memory error!!!

by McA (Priest)
on Mar 27, 2013 at 22:05 UTC ( #1025815=note: print w/replies, xml ) Need Help??

in reply to Out of memory error!!!

Some hints:

Should be followed immediately with
use strict; use warnings;

You mix up several ways to open a file. You should take consistently the three parameter form. Also use scalars as file handles. So your

open (INFO, "<", "$File") or die("Cannot open the flat file"); open(my $LOG,">>LOG_file") or die("Cannot open"); open(XML,">xml_file") or die("Cannot open");
open(my $INFO, '<', $File) or die "Cannot open the '$File': $!; open(my $LOG, '>>' 'LOG_file') or die "Cannot open 'LOG_file: $!"; open(my $XML, '>', 'xml_file') or die "Cannot open 'xml_file': $!";

As stated by davido, you should iterate line by line over the file:

while(defined(my $line2 = <$INFO>)) { }

Every $line2 will have a LF at the end. Probably you want to get rid of it. In this case it's not necessary.

chomp $line2;

It seems you're working on fixed column format files (COBOL output?). The format of the row (line) is defined by the first two bytes (segment). So, instead of creating a big if-elsif-else-tree it would be more maintainable to create a dispatcher based on the segment. The usage of unpack may be your friend to extract the data easily. So I propose the following:

my %config = ( '00' => { 'unpack' => "x40A8", 'fields' => [qw(Var1)], }, 'A0' => { 'unpack' => "x2A9x3A3x3A42A30", 'fields' => [qw(Var1 Var2 Var3 Var4)], }, ); while(defined(my $line = <$INFO>)) { my $segment = substr $line, 0, 2; if(exists $config{$segment}) { my @fields = unpack $config{$segment}->{'unpack'}, $line; my %record; $record{$config{$segment}->{'fields'}->[$_]} = $fields[$_] for +each (0..$#fields); print Dumper(\%record), "\n"; } else { die "ERROR: Unknown segment '$segment'. Config or data error?" +; } }

At the end you have to close the filehandles and not the 'filenames':

close $INFO or die "Can't close file '$File': $!"; close $LOG or die "Can't close file 'LOG_file': $!"; close $XML or die "Can't close file 'xml_file': $!";

These proposals reflect only my personal opinion. Hopefully a little bit helpful.


Replies are listed 'Best First'.
Re^2: Out of memory error!!!
by sathya83aa (Acolyte) on Mar 28, 2013 at 08:12 UTC

    Hi McA,

    Thanks for those suggestions..Even after changing the foreach to while, I'm still facing the same Out of memory issue :-(

    Any suggestions?

    Thanks & Regards,

    sathya V.

      If you're still running out of memory, you'll have to find out where you are storing your data and maybe not store all the data in RAM. If you are storing the bulk of your data in a hash, an easy way of trading time for space is to use DB_File or some other key/value store that can be tied to a hash. Update: Also see Tie::DBI, which just had a new release out.

      Often it also helps to output progress through your file so that you get an idea where in your program you run out of memory.

      In your example you're opening a filehandle named XML, so I assume you want to convert your input stream to xml format. If this is right we would like to see how the xml output is produced. Probably you're using a memory intensive way.


        Ok Let me make it more clear..Im fetching those variables from a flat file and printing it in the XML file. The order of printing of the variable will differ from how we are getting the variable.


        open (my $INFO, "<", "$File") or die("Cannot open the flat file $!");

        open(my $LOG,">>LOG_file") or die("Cannot open $!");

        open(my $XML,">xml_file") or die("Cannot open $!");

        while (defined(my $line2 = $INFO)) {

            $line2 =~ s/&/&amp;/g;

            print "$line2\n";

            my $segment = substr($line2,0,2);

            if($segment eq "00")


            my $Var1 = trim(substr($line2,40,8));


            if($segment eq "A0")


            $Var2 = trim(substr($line2,2,9));

            $Var3 = trim(substr($line2,14,3));

            $Var4 = trim(substr($line2,20,42));

            $Var5 = trim(substr($line2,62,30));

        and so on


            print XML "<Header>\n";

            print XML "<AAA>VAR1<\/AAA>\n";

            print XML "<BBB>VAR2<\/BBB>\n";

            print XML "<CCC>VAR3<\/CCC>\n";

          print XML "<DDD>VAR4<\/DDD>\n";

          print XML "<EEE>VAR5<\/EEE>\n";

            print XML "<Header>\n";

        close $LOG;

        close $XML;

        close $INFO;

        sub trim($)


           my $string = shift;

            $string =~ s/^\s+//;

           $string =~ s/\s+$//;

           return $string;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025815]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-03-19 09:40 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (238 votes). Check out past polls.