CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Read variables from a file

 



Rivotti
User

Mar 21, 2000, 5:13 AM

Post #1 of 9 (1719 views)
Read variables from a file Can't Post

I'm trying to retrieve variables such as - $name="name" - from a file. Is it possible to to? There is a better way?
The idea is to get the information an use it, all o this in the same CGI.


Cure
User

Mar 21, 2000, 7:38 PM

Post #2 of 9 (1719 views)
Re: Read variables from a file [In reply to] Can't Post

Hi Rivotti

Example: register.cgi

#!/usr/bin/perl

use CGI;
$q = new CGI;

print $q->header;

if ($q->param('action') eq 'cure') {&subscribe}
else { &already_subscribed }

sub subscribe {


print $q->start_html(-title=>"Already Listed",
-author=>'cure',
-BGCOLOR=>'white');

print qq~
Hi there
~;
print $q->end_html;



}

sub already_subscribed {

print $q->start_html(-title=>"Fake Email",
-author=>'cure',
-BGCOLOR=>'white');

print qq~
<form action=register.cgi method=post>
<input type=text name=action>
<input type=submit value=submit>
</form>
~;
print $q->end_html;

}

Cure


Rivotti
User

Mar 22, 2000, 2:52 AM

Post #3 of 9 (1719 views)
Re: Read variables from a file [In reply to] Can't Post

Hi Cure:

Thank you for the reply, but that wasn't what I wanted. Let me explain better:

What I really want is a program that grabs variables such as - $name="something..." in another file ( for ex: data.inf ) and use it at the same time. Itīs like getting info from a cookie but in this case it's not a cookie, it's a file.

Thank you for the help
Rivotti




Jasmine
Administrator / Moderator

Mar 22, 2000, 4:01 AM

Post #4 of 9 (1719 views)
Re: Read variables from a file [In reply to] Can't Post

Rivotti:

You can just require the file with all of the variables that you want to use in your program. For example, your list of variables can be in a variables.pl file like this:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# variables.pl

$name = "blah";
$email = 'email@email.com';
$url = "http://perlarchive.com";
</pre><HR></BLOCKQUOTE>

Then, in your program, you can use:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl
# program.cgi

require "variables.pl";
print $url; #will print http://perlarchive.com
</pre><HR></BLOCKQUOTE>

Hope this helps!


Rivotti
User

Mar 22, 2000, 4:41 AM

Post #5 of 9 (1719 views)
Re: Read variables from a file [In reply to] Can't Post

Jasmine:

Thank you very much. That solved the problem.
I have just one question. What if I wanted to get the file in another site. For ex: my CGI is in "www.site1.com" and I want to open the file.pl in the site "www.site2.com". Is it possible or the only way to work is getting the file inside the same server?

Thank you again for your help
Rivotti



Jasmine
Administrator / Moderator

Mar 22, 2000, 5:00 AM

Post #6 of 9 (1719 views)
Re: Read variables from a file [In reply to] Can't Post

Files that need to be "required" need to be in the same server path. For example, if your complete server path is:

/home/perlarchive/www/

you can require any files within /home/perlarchive and /home/perlarchive/www

The alternative is if you have both site1.com and site2.com on the same physical server, and you have root access on that server, you can upload the file(s) you want to share with the entire server in your Perl modules directory.

Yet another more extreme alternative is to ftp the "shared" file to one or more domains that need it. You can automate this process if you wish by writing a program that uses the Net::FTP and is invoked by cron. This is a much more complex solution, but it'll work.


Rivotti
User

Mar 22, 2000, 5:22 AM

Post #7 of 9 (1719 views)
Re: Read variables from a file [In reply to] Can't Post

Jasmine:

Thank you again for the help. I'd already considered using Net::FTP but I was hoping for a easier way to do it.

Keep the good work
Rivotti


Jasmine
Administrator / Moderator

Mar 22, 2000, 6:26 AM

Post #8 of 9 (1719 views)
Re: Read variables from a file [In reply to] Can't Post

But ... but ... but ... Net::FTP is easy Smile

Just in case you were concerned about learning how to use yet another Perl module, here's my "cut and paste" version of using Net::FTP -- you'll just need to edit the variables, copy and paste it into your program, and that's that for that.


<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


###################################
# First, define the 6 variables
# that's needed for Net::FTP to
# to its thing.
###################################

###################
# Login Information
####################

$domainname = "yourdomain.com";
$username = "yourusername";
$password = "yourpassword";

####################
# Upload Information
####################

$filetoupload = "/home/domain1/libs/whatever.pl";
$uploaddir = "/home/domain2/libs/";
$newfilename = "whatever.pl";




###################################
# Now, variables are defined, you
# can pretty much just copy and
# paste the following into your
# program -- you can comment out
# the confirmation messages if
# you want.

use Net::FTP;

$ftp = Net::FTP->new($domainname);
print "Connection successful.\n";

print "Attempting to log in to $domainname...\n";
$ftp->login("$username","$password") &#0124; &#0124; die "Couldn't log in using $username / $password.\n";
print "Login successful to $domainname.\n";

print "Changing directory to $uploaddir.\n";
$ftp->cwd("$uploaddir") &#0124; &#0124; die "Couldn't change to upload directory $uploaddir. $!\n";

$dir = $ftp->pwd(); # just to confirm current directory
print "Successfully changed directory to $dir\n";

print "Switching to ASCII transfer mode.\n";
$ftp->ascii &#0124; &#0124; warn "Couldn't switch to ascii mode. Possible corrupted upload coming up.\n";

$ftp->put($filetoupload,$newfilename) &#0124; &#0124; die "Could not upload $filetoupload into $uploaddir/$newfilename $!\n";
print "Uploaded successful.\n";

$ftp->quit();
</pre><HR></BLOCKQUOTE>

Okay ... enough fun for me this morning. Off to work I go Smile


Good luck!


Rivotti
User

Mar 22, 2000, 7:24 AM

Post #9 of 9 (1719 views)
Re: Read variables from a file [In reply to] Can't Post

Jasmine:

Is good to have someone like you helping. Thank you again, very much...

Rivotti




 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives