Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple script 2

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
I don't know PERL but tried solving this problem by myself and it failed :(

It's only small so hopefully someone with a few seconds to spare will help me out. Can you write this little script for me?

Thanks very much for reading :)

# recieve post data
# data: username="you" value="hello"

# create txt file username/you.txt

# open txt file
# print value/hello
# close txt file
# end
 
I'm not sure if this is how you're getting 'you' and 'hello' in, but it should give you enough of an idea to manage.
Code:
use strict;   #requires variables to be declared before use
use warnings; #also good practice
use CGI qw(:standard); #param() function
use CGI::Carp qw(fatalsToBrowser); #report fatal errors to browser

#get the two values
my $username = param('username');
my $value = param('value');

#check if 'username' is a directory
if(-d 'username')
{   #open a file for writing ">"
    open FILE, ">username/$username".".txt" or die "Unable to open file: $!";
    print FILE "value/$value\n";
    close FILE;
} else {
    die "'username' not a directory";
}
It's been awhile since I've done much CGI work. Hope it helps. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
That's excelent, THANK YOU! :)

I'm not sure how the values get in with your examples. I'm hoping to submit them from a HTML form using GET...

Can anyone clear up this problem? How does PERL create new txt files? I have looked through a reference of commands but couldn't find anything to do that :(

There's the console command in linux called "touch" but I dunno how PERL pulls it off.
 
$username & $value are in the query_string. The shortest way to do this is ?username=value.

I then tried the following but it failed. Any ideas? :(

#!user/bin/perl
($username, $value) = split (/=/, $query_string);
print "$username\n";
print "$value\n";
 
Thanks for your reponse to my post, stormbind.

#!/_/perl/bin/perl.exe

$query_string="username=value";
($username, $value) = split (/=/, $query_string);
print "$username\n";
print "$value\n";

This runs fine for me.
Your shebang line looks a big odd, maybe a typo.

The Open() function will create a new file for writing if the existing one does not exist. You could always use the system function to create the file. eg.
system("touch username/$username");



The discipline used to write things down is the first step in making them a reality.
 
I'm assuming you're calling the script something like this:
or have that data filled out in a form with this script as its action. The code below can get the data off the GET line, using a function in the CGI module. This doesn't deal at all with URL encoding (%20 is a space, etc).
Code:
#get the two values
my $username = param('username');
my $value = param('value');
Next is how the text file is created. Most programming languages have at least three file modes. Read, write, append. For the first and the last, the file must exist to open it. Read only lets you read from the file. Append only lets you write, but it adds the data you write to the end of the file that already exists. The write method will attempt to create the file if it doesn't exist, and if it does exist, it truncates (removes) what's there already.

In perl, you can specify which of these modes (as well as many more) to use by the first character in the "filename".

[tt]open FILE, "[red]>[/red]username/$username".".txt"[/tt]

The [red]>[/red] is what makes this file opening for write. To append, you use [red]>>[/red]. To read, you can either put nothing there, or use [red]<[/red], but the nothing is more common.

Read about the open function here for more information:
Also, I agree you shebang looks odd. You probably meant it to be this:
#![red]/[/red]user/bin/perl ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Thank you both for your help. I actually understand!

A good example is worth a thousand lessons :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top