Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read file in Perl and if it doesn't exist create it?

Tags:

file

file-io

perl

In Perl, I know this method :

open( my $in, "<", "inputs.txt" );

reads a file but it only does so if the file exists.

Doing the other way, the one with the +:

open( my $in, "+>", "inputs.txt" );

writes a file/truncates if it exists so I don't get the chance to read the file and store it in the program.

How do I read files in Perl considering if the file exists or not?

Okay, I've edited my code but still the file isn't being read. The problem is it doesn't enter the loop. Anything mischievous with my code?

open( my $in, "+>>", "inputs.txt" ) or die "Can't open inputs.txt : $!\n";
while (<$in>) {
    print "Here!";
    my @subjects    = ();
    my %information = ();
    $information{"name"}     = $_;
    $information{"studNum"}  = <$in>;
    $information{"cNum"}     = <$in>;
    $information{"emailAdd"} = <$in>;
    $information{"gwa"}      = <$in>;
    $information{"subjNum"}  = <$in>;
    for ( $i = 0; $i < $information{"subjNum"}; $i++ ) {
        my %subject = ();
        $subject{"courseNum"} = <$in>;
        $subject{"courseUnt"} = <$in>;
        $subject{"courseGrd"} = <$in>;
        push @subjects, \%subject;
    }
    $information{"subj"} = \@subjects;
    push @students, \%information;
}
print "FILE LOADED.\n";
close $in or die "Can't close inputs.txt : $!\n";
like image 423
ejandra Avatar asked Sep 16 '14 05:09

ejandra


People also ask

How do you create a file if not exist in Perl?

Write mode (>): If the file does not exist, a new file is created. If the file already exists, the content of the file is wipe out, therefore, you should use the write mode with extra cautious. Append mode ( >>): as its name implied, you can open the file for appending new content to the existing content of the file.

How do you check if file does not exist in Perl?

Perl has a set of useful file test operators that can be used to see whether a file exists or not. Among them is -e, which checks to see if a file exists.

How do I open a reading file in Perl?

Following is the syntax to open file.open(DATA, "<file. txt"); Here DATA is the file handle, which will be used to read the file. Here is the example, which will open a file and will print its content over the screen.


2 Answers

Use the proper test file operator:

use strict;
use warnings;
use autodie;

my $filename = 'inputs.txt';
unless(-e $filename) {
    #Create the file if it doesn't exist
    open my $fc, ">", $filename;
    close $fc;
}

# Work with the file
open my $fh, "<", $filename;
while( my $line = <$fh> ) {
    #...
}
close $fh;

But if the file is new (without contents), the while loop won't be processed. It's easier to read the file only if the test is fine:

if(-e $filename) {
   # Work with the file
   open my $fh, "<", $filename;
   while( my $line = <$fh> ) {
      #...
   }
   close $fh;
}
like image 105
Miguel Prz Avatar answered Sep 21 '22 19:09

Miguel Prz


You can use +>> for read/append, creates the file if it doesn't exist but doesn't truncate it:

open(my $in,"+>>","inputs.txt");
like image 21
famousgarkin Avatar answered Sep 21 '22 19:09

famousgarkin