Hello,
I have a conceptual question related to PERL. (My code is at the bottom of this post) I am very new to PERL so keep that in mind when you read this post & look at my code.
My goal is to create a website where a student can upload a file to a specific directory. There are 3 directories (one for each class). Within the class directories are folders for each of the 5 students. Within the student folders are 4 folders for assignments.
I realize I could write *MANY* "if" statements to say that if they chose a specific class, go to this directory... if it is a particular student, go to this directory... and if they selected a particular assigment, go to this directory. This method seems very cumbersome & inefficent, however.
I tried to change this line below so that the path the student uploads to will change depending on the values for the variables they entered. I tried this & could not get it to work.
$filename = "$HOME/classes/csc2010/laura/1/outputfile.txt";
As long as I tell it what class folder to go to(whether it's CSC2010, CSC3320 or CSC3410), it works- it opens a text file in that folder & prints the student id varialbe
to -->
$filename = "$HOME/classes/
$CSC/$studentid/$assignment/outputfile.txt";
But when I insert the variable representing the class folder, it does work- it does not open a text file in that class folder & print. I'm wondering if maybe there is something wrong with the $CSC variable. It prints out ok & I put chomp($CSC); in to take care of any whitespace. Is it not possible to put variables to resolve pathnames?
If you have any ideas, comments, suggestions, they are welcome.
Thanks-
Laura
lfindle@mindspring.com
My code is as follows:
**************************************************
#!/usr/local/bin/perl
# 2010c.pl
print "Content-type: text/html\n\n";
# i realize i should probably be using CGI.pm library
# rather than using the below code to handle URL encoded
# POST- that will be my next step
if ($ENV{"REQUEST_METHOD"} eq 'GET') { $buffer = $ENV{'QUERY_STRING'};
+ }
else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$studentid = $FORM{'studentid'};
$CSC = $FORM{'CSC'};
chomp($CSC);
$assignment = $FORM{'assignment'};
$file = $FORM{'file'};
# here's the code I wrote that I'm having conceptual
# problems with
if ($CSC eq "CSC2010") {
if ($studentid eq "laura") {
if ($assignment eq "1") {
$HOME = (getpwnam("wfp51916"))[7];
$filename = "$HOME/classes/csc2010/laura/1/outputfile.txt";
open( OUTFILE, ">>$filename" ) or die ( "no go miss ");
print OUTFILE "$studentid\n";
close( OUTFILE );
}
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.