Newbie Question : Parameters

Hi.

I'm writing a small script that will create text files and store info in them.  Basically, the initial script will just populate a new file with default values, and for this I have a script that works fine.

What I need to do is to be able to pass the script a filename for the data to be stored in.  So, where I have this line of code....

              open(FILE,">filename.txt");

....I need to be able to pass the script the filename and somehow create the file with that name.

Look forward to your comments.

John.
LVL 6
JaymolAsked:
Who is Participating?
 
holliCommented:
html:
<a href="http://www.myserver.com/myscript.pl?file=myfile.dat>link</a>

OR

<form action="http://www.myserver.com/myscript.pl" method="post">
<input type="hidden" name="file" value="myfile.dat">
</form>

perl:

#!/usr/bin/perl
use CGI;
my $q=new CGI; my $filename=$q->param("file");
open FILE, "> $filename;"
#do your stuff here
close FILE;

print $q->header . $q->start_html . "<b>Info has been saved.</b>" . $q->end_html;
0
 
JaymolAuthor Commented:
Thanks Holli but that doesn't work for me.

Any ideas as to why?

John.
0
 
holliCommented:
please post your script.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
JaymolAuthor Commented:
It's your script.



          #!/usr/bin/perl --

          use CGI;
          my $q=new CGI;
          my $filename=$q->param("file");

          open FILE, "> $filename.txt";
          print FILE "0";
          close FILE;



That's all there is at the minute and it doesn't work.

John.
0
 
JaymolAuthor Commented:
Oh, btw, I know the path is okay as I've already got a simple counter script that's working on the server.

Ta,

John.
0
 
JaymolAuthor Commented:
and I did set the file privileges correctly.

John.
0
 
JaymolAuthor Commented:
Thanks for the help Holli.  I've only graded this C as I had to make quite a few changes to the script to make it work.

Thanks again,

John.
0
 
holliCommented:
if your remove this "--" then the above (your) code works fine.

call it from the shell: perl script.pl file=myfile
call from browser: http://yourserver.com/script.pl?file=myfile

0
 
JaymolAuthor Commented:
Holli,

Thanks, but the "--" is required or the scripts don't work on my server.

Thanks again for your help,

John.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.