heh, yup. Boredom has it's finest moments...
Quote:
Why is my $dir using the param and the others not? I assume it has something to do with accessing it for the allowed directories, but not sure why you define this one this way and not $file also.
Because the value is used in a regex a few lines down. The regex could have problems with $in->param('dir'); so it is easier to just define it, then use a regular scalar in the regex.
I don't do it with $file because that one isn't used anywhere like $dir. No point in assigning another variable when $in->param('file') will work just fine. Plus, the variable DOES get assigned when it gets to the print_it routine (as a local var).
Quote:
I understand the grep statement pretty well.. psuedo code here "die unless thisdirhere is in thisallowedlisthere" .. and before it print $in->header; just calles CGI.pm's Default text/html header ??
correct on both counts.
Quote:
($in->param('display') =~ /^(txt|html)$/) ? print_it($in->param('display'), $dir, $in->param('filename')) : &print_form;
Could you tell me a little more about how this line works. why =~ /^ .. I see this alot in perl when pattern matching etc. Also, I guess this may answer my first question. Why not pre-assign $filename beforet his point. You do it inline here.
Regular pattern matching. Telling it do look at the value passed in to param('display'). If it matches txt or html, go to print_it, otherwise go to print_form.
basically: expr ? true : false;
evaluate the expression, do the first if the expresssion is true, second if is false.
As for the pattern itself:
$in->param('display') =~ /^(txt|html)$/
=~ means pattern matching. / is the deliminator (notice one at the beginning, one at the end. kind of like quotes). if the standard / delimiter is used then =~ is fine. If you have slashes in your regex, you can use a different delimiter so that the slashes won't need to be escaped. In that case, you have to use m to specify matching. For instance:
$in->param('display') =~ m,^(txt|html)$,
or
$in->param('display') =~ m!^(txt|html)$!
or
$in->param('display') =~ m#^(txt|html)$#
etc.
The ^ means "the very beginning of the string" and the $ means "the very end". (but, they can mean other things too, you really need to read about regex's).
so ^(txt|html)$ means the pattern must start right at the beginning of the string, and either be followed by txt or html, then immediedly end the string. This way, "text", "htm" etc are not valid.
Quote:
The line before and after is the header/footer calls. I get that part. Does the =~ sort of act like the if/then code. It seems this 3 line bit tells the script what to do. If txt/html print stuff, if not, print the form. Am I right? If so I understand why my form has the header and footer, if not, I don't know where it gets the calls from!
Sort of right! Basically, since you always print the header and footer, no matter what routine you go to, no point in reduntantly putting those calls in the routine. Always call the header, then to the ? : line to determine which subroutine to branch to (print_it, or print_form), then always print the footer. simple!
The 3 lines are not acting together.
the ? : line, is sort of like an if statement thoguh.
Quote:
The first line with the 3 $var's in it, I guess assings the value to $_ ?? (the $@)??
see up in the ? : statement where we call print_it with: print_it($in->param('display'), $dir, $in->param('filename')) ? We're passing three values to the subroutine.
The line you are asking about assigns the values passed to local variables in the subroutine for use in our operations.
@_ is kind of like $_ except an array instead of a scaler. so if you pass print_it(1, 2, 3);
in the subroutine
($a, $b, $c) = @_;
would assign 1 to $a, 2 to $b etc...
Quote:
What about the two $printer lines? I thought using the same var name would over right the current info? And here we go with = and =~ .. How's that work? I know you can add when you use qq~ .. but this doesn't seem quite the same.
Heh, this is where I was having fun with perl. Basically, if you are printing html, nothing needs to be done excpt print out the font tags around $_ while going though the file.
When printing txt, it's the exact same thing excet you want $_ html-escaped.
So, I assigned $printer to be the default output (along with a print statement...cuz of the eval).
Then this line:
$printer =~ s/(\$_)/", \$in->escapeHTML($1), "/ if ($display eq 'txt');
is doing a regex substitution. basically, if $display equals "text", replace $_ with $in->escapeHTML($_) (plus the " and , because of the formating that i want).
the \ inf front of $_ is because i want a literal $, and not the "end of string" position as explained above. then in the second part of the substitition, I use $1, which means the value of whatever matched in the first set of parentheses (notice the \$_ is in parenthesis). Again that sounds confusing, and you need to read up on regexes.
Substituitns are: =~ s/substutuite this/with this/; (and the i means case insensitive).
Quote:
What I don't get though, is the eval in print_it sub. Is it eval'ing by the situation you gave it when you defined $printer?
YES!
$printer will contain one of two strings. Either:
print "< font size=2 face=verdana, arial >$_< br >";
or
print "< font size=2 face=verdana, arial >", $in->escapeHTML($_), "< br>";
the word print is included in the string inside $printer. Also notice that I contained printer's string in single quotes, because I am using double quotes in the value inside printer (the print "<font etc")
You already know about looping and using $_ to mean "the one im on right now". well, I do the loop, but eval $printer.
What happens is, eval gets executed so literaly: print "< font blah blah blah> $_" gets executed!! (neat, eh?! Hope that made sense).
Quote:
One last question, I think. In print_standard you have use shift. What does that do?
In this instance, again assigns the value that was passed, to the local variable. @_ could have been used (or @_[0] for that matter) but since it was only one value, shift is fine.
Likewise, earlier I could have done:
$display = shift;
$file = shift;
$dir = shift;
but as you see, no need.
*whew* you are making me type today!
anyway, hope that helps!
--mark