0

I just created a text test.conf file with some information. How can I read it on Perl?
I am new to Perl and I am not sue would will I need to do.
I tried the following:

C:\Perl\Perl_Project>perl
#!/usr/local/bin/perl 
open (MYFILE, 'test.conf'); 
while (<MYFILE>) 
{ chomp; print "$_\n"; } 
close (MYFILE);

I tried installing Perl on my laptop that has Windows 7 OS, and using command line.

serenesat
  • 4,611
  • 10
  • 37
  • 53
  • 2
    That's a lot to stick on the command line. Put the Perl program you have up there in a file and run it using `c:\> perl MyPerlProgram.pl`. Also, if this is Windows, the shebang line isn't really doing anything for you. – Hambone Nov 30 '15 at 02:09
  • its a test.conf file that I created ..... its not test.pl I am afraid I didn't get that. – userlearningpython Nov 30 '15 at 02:22
  • Check out https://metacpan.org/pod/Config::Tiny – Miller Nov 30 '15 at 03:41
  • What is your program doing that makes you think it's not working? Have you considered adding an `or die $!` to your `open()`? – Matt Jacob Nov 30 '15 at 05:34
  • 1
    Possible duplicate of [What's the best way to open and read a file in Perl?](http://stackoverflow.com/questions/318789/whats-the-best-way-to-open-and-read-a-file-in-perl) – Matt Jacob Nov 30 '15 at 05:36
  • What is the actual problem you're trying to solve? Your program, as it stands, doesn't do anything very useful. The extension `.conf` suggests that you might want to do something else with the information in that file, other than just printing it out. – Edward Dec 01 '15 at 09:33

2 Answers2

2

Instead of using command line, write your program in a file (you can use any editor to write your program, I would suggest use Notepad++) and save as myprogram.pl in the same directory where you have your .conf file.

use warnings;
use strict;

open my $fh, "<", "test.conf" or die $!;
while (<$fh>)
{
    chomp;
    print "$_\n";
} 
close $fh;

Now open a command prompt and go to the same path where you have your both file myprogram.pl and test.conf file and execute your program by typing this:

perl myprogram.pl

You can give full path of your input file inside program and can run your program from any path from command prompt by giving full path of your program:

perl path\to\myprogram.pl

Side note: Always use use warnings; and use strict; at the top of your program and to open file always use lexical filehandle with three arguments with error handling.

serenesat
  • 4,611
  • 10
  • 37
  • 53
1

This is an extended comment more than an answer, as I believe @serenesat has given you everything you need to execute your program.

When you do "command line" Perl, it's typically stuff that is relatively brief or trivial, such as:

perl -e "print 2 ** 16"

Anything that goes beyond a few lines, and you're probably better off putting that in a file and having Perl run the file. You certainly can put larger programs on the command line, but when it comes to going back in and editing lines, it becomes more of a hassle than a shortcut.

Also, for what it's worth the -n and -p parameters allow you to process the contents of a stream, meaning you could do something like this:

perl -ne "print if /oracle/i" test.conf
Hambone
  • 15,600
  • 8
  • 46
  • 69