LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-05-2003, 04:40 PM   #1
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Perl Program That Reads From .conf File


I just started to mess around with Perl because I desperatly need to learn a language for small scripts. The first script I need to write is a program that uploads a few files to an ftp server. I wanted to create a .conf file that would store all the variables. The conf file looks like this:

Code:
# What is the address of the ftp server you want to connect to?
# EXAMPLE: www.domain.org

$WebAddress = www.domain.org

# What is the username for the ftp server?
# EXAMPLE: username

$UserName = username

# What is the password for the user?
# EXAMPLE: password

$Password = password

# What file(s) would you like to upload to the server?
# EXAMPLE: webcam_image.jpg

$WebcamImage = shiner.jpg
$WebcamImageThumbnail = thb_shiner.jpg
Now I know how to do all the ftp stuff in the program but I'm unsure how to read this .conf file in the Perl program. Basically I want the program to read the .conf file and ignore everything that begins with # and all the blank lines.

How would you do this?

Last edited by Crashed_Again; 12-05-2003 at 04:43 PM.
 
Old 12-05-2003, 06:28 PM   #2
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Here's what you want! If you aren't familiar with them, then you'll need to learn regular expressions to be a competent Perl programmer.

Code:
#!/usr/bin/perl -w

my $filename = 'foobar.conf';
my $comment = '^\s*#';
my $blank = '^\s*$';
my $inputLine;

open( FILE , $filename ) or die "Unable to open $filename.\n";

while ( $inputLine = <FILE> ) {

   chomp( $inputLine );

   if ( $inputLine !~ /$comment/ &&
        $inputLine !~ /$blank/ ) {

      # Found a non-blank, non-commented line. Do something useful.
      print( "$inputLine\n" );
   }
}

close( FILE );

Last edited by eric.r.turner; 12-06-2003 at 10:36 AM.
 
Old 12-07-2003, 06:49 AM   #3
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Original Poster
Rep: Reputation: 57
Thanks man. I've successfully created the script that I wanted to do.

Muchos gracias.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Shell program Read a configuration File minil Programming 10 01-17-2005 04:37 AM
Read a char from a file (PERL) linuxlover1 Programming 4 01-09-2005 09:10 AM
samba conf file is read-only to root tmoorman Linux - Software 9 11-10-2003 08:23 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
lilo.conf and grub.conf no read access shanenin Linux - Software 1 10-02-2003 03:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration