The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 05:55:13 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Loading Variables To Script from text file (config.txt)  (Read 2128 times)
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« on: June 11, 2009, 01:43:57 AM »

I write a lot of apps for others to use and need to give them a way
of changing options, settings, etc.  For example, read a list of URLs
from a file, I could force them to put their URLs in urls.txt, instead,
I let them decide the name of the file in a config.txt and my script
reads their settings.

I use to do it like this:

open(IN, "config.txt");
$/=undef;
$slurp = <IN>;
close IN;
$/ = "\n";
eval($slurp);

The dollar slash is Perl's special character for setting the line ending, so by setting it
to undef, the entire file is read in as a single string.

I reset the line ending or I get huge headaches later.

Perl's eval function interprets a string as if it is actual perl code and, of course,
my config.txt files are written that way with cetain lines commented out, etc.

In the config.txt I might have like this:

# PUT THE NAME OF YOUR URLS FILE HERE
$your_urls_filename = 'my_urls.txt';

Anyways, I did this for a long time unil I read about Perl's do function.

Now I just do this..

do("config.txt");

and the "text" of the config.txt file is executed as perl.

cool huh?

well, once in awhile I gotta post something here about Perl or perk might
move the board out of site. Cheesy

Bompa


Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: June 14, 2009, 02:19:53 PM »

Thanks for the post Bomps - although not a PERLer, it is still hot watching how that language goes.
Sometimes the syntax for PERL just weirds me out.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!