

For example, in my Perl program I can access the $var_dir variable that is defined in my configuration file. $good_file_extensions = '\.eps$|\.ps$|\.pdf$' Īs you can see, I like this approach because the format of this configuration file is very easy for a human to read and maintain.Īnother cool thing about this approach is that it makes the variable names that are used in the configuration file available in your Perl program. $fifo_file = '/opt/devdaily/ftplogger/bin/fel.fifo' $var_dir = '/opt/devdaily/ftplogger/var' Here are a few (slightly modified) lines from the configuration file that this program reads: > Using file handler operator Read file using operator is most.
while(my linePerl read file code#
Open CONFIG, "$config_file" or die "Program stopping, couldn't open the configuration file '$config_file'.\n" ĭie "Couldn't interpret the configuration file ($config_file) that was given.\nError details follow: if you can see from that code segment, the name of the configuration file is passed into this Perl program as the first argument on the command line. Once you hit EOF on your read and then sleep for a while, you (probably) have to stick in a dummy seek to reset things.

The first parameter is the filehandle which allows Perl to refer to the file in future. Unless youve changed this (such as building Perl with Configure -Uuseperlio ), you can open filehandles. # do what's needed to read in the configuration file. The open function opens a file for input (i.e. The read function reads a block of information from the buffered filehandle: This function is used to read binary data from the file. Simple examples to open a file for reading. including C, C++, C, Java, JavaScript, Perl, Python, and many others.
Perl read file how to#
Here’s some Perl code that I just pulled from a working program that demonstrates how to read a human-readable configuration file in a Perl program: Here is another easier possibility to read in a json file: include json/json.
