Re: help on this code below
by roboticus (Chancellor) on Oct 27, 2011 at 17:32 UTC
|
sasidhardv:
Since you're not asking for anything specific, I'll just point you to two documents: perldoc perlre to help you understand the regular expressions (the bits between forward slashes) and perldoc perlsyn to help you understand the control flow statements (next, if, etc.)
...roboticus
When your only tool is a hammer, all problems look like your thumb.
| [reply] [d/l] [select] |
|
i am uploading a file from a html file. that data is stored in filetoupload_02 field. and i am passing the user who has triggered this upload, that value is in pusr field.
i want to know what values are coming into the perl file and what are their values this below code saying something about filetoupload_02 field and i want to know what is the value for pusr field also from the below code.
when i am using $puser = $query->param("p_usr");but this value $puser i am not able to assign to any other variable i don't know why is that pls help
foreach $key (sort {$a <=> $b} $query->param()) {
next if ($key =~ /^\s*$/);
next if ($query->param($key) =~ /^\s*$/);
next if ($key !~ /^filetoupload_(\d+)$/);
$Number = $1;
| [reply] [d/l] [select] |
|
A few observations:
The first line loops through the CGI parameter keys (assuming $query is a valid CGI.pm object or something similar). They're sorted numerically, which is odd, and possibly pointless.
The next line skips to the next key if this one is empty of anything but whitespace -- unlikely, and superseded two lines later by a more specific check.
The next line skips to the next key if this one's value is empty of anything but whitespace.
The next line skips to the next key if this one doesn't start with "filetoupload_" followed by one or more digits. If it does match that pattern, the digits are put in $Number by the next line and the loop proceeds on.
Then after a blank line, your 'if' line checks the value of this filetoupload_\d+ parameter, and if it doesn't end in a forward or backward slash, puts everything after the last slash in $1, which the next line puts into $Filename.
The next line puts $2 into $pxuser. But since there is only one set of capturing parentheses in the last regex, $2 will always be empty at this point, and thus so will be $pxuser. This is probably a problem.
You also talk about getting a "p_usr" value, as well as a "pusr" value, but your code doesn't do this, and these are also not interchangeable. Where do you expect "the user who has triggered the upload" value to come from? Are "pxuser", "p_usr", and "pusr" three different things, or the same thing inconsistently typed?
| [reply] |
Re: help on this code below
by johnny_carlos (Scribe) on Oct 27, 2011 at 19:37 UTC
|
You can also run the debugger, there are commands to print out the variables and see what they're doing line-by-line:
perldebug | [reply] |
|
chop $SAVE_DIRECTORY if ($SAVE_DIRECTORY =~ /\/$/);
use CGI qw(:standard);
$query = new CGI;
$puser = $query->param("pusr");
in the above code
$SAVE_DIRECTORY is /usr/tmp and i am trying to create a file with the value of puser in /usr/tmp.
it has become a hell and i am not able to understand y i am not able to create the file with $puser value. the /usr/tmp dir has write permission to all. | [reply] [d/l] |
|
Your code doesn't try to open a file, so it's hard to guess. What does "not able to create the file" mean? Does the file not show up, or does it show up somewhere else? Is there an error? Have you checked the web server's error_log? Have you tried having your script send errors to the browser, as CGI.pm is able to do?
Most likely, $puser still isn't getting the value you think it is, because of the problem I outlined in a message above. Add a line like the following to see the value of $puser in your browser. What is displayed between the equal signs? While you're at it, you might want to do the same thing with $SAVE_DIRECTORY; maybe it doesn't really contain /usr/tmp.
print "\$puser =$puser=";
| [reply] [d/l] |
|
|
|
Re: help on this code below
by hbm (Hermit) on Oct 27, 2011 at 17:57 UTC
|
foreach $key (sort {$a <=> $b} $query->param()) {
# rearrange:
next if ($query->param($key) =~ /^\s*$/);
next if ($key !~ /^filetoupload_(\d+)$/);
# next condition is a subset of the previous condition
# i.e., $key cannot be empty AND match filetoupload_\d+
#next if ($key =~ /^\s*$/);
$Number = $1;
# only one matching group: ( )
if ($query->param($key) =~ /([^\/\\]+)$/) {
$Filename = $1;
$Filename =~ s/^\.+//;
# hence, no $2
#$pxuser = $2;
#$pxuser =~ s/^\.+//;
$files{$Number} = $Filename;
$File_Handle = $query->param($key);
if (!$ALLOW_INDEX && $Filename =~ /^index/i) {
print header;
| [reply] [d/l] |
|
what does this mean
if ($query->param($key) =~ /([^\/\\]+)$/) { # is this removing spaces ??
and
next if ($key !~ /^filetoupload_(\d+)$/);
especially (\d+)$/ mean | [reply] [d/l] [select] |
|
what does this mean if ($query->param($key) =~ /([^\/\\]+)$/) { # is this removing spaces ??
The square brackets enclose a character class. The first character in that class is a caret, which negates the class. Each initial backslash escapes the following character. The plus sign matches one or more. The parentheses store whatever is matched. The dollar matches the end of the line. Putting it all together, "if parameter ends with one or more non-slashes, give me those non-slashes in $1.
This is matching only - no substitution.
and
next if ($key !~ /^filetoupload_(\d+)$/);
especially (\d+)$/ mean
\d matches a digit. Here, caret matches the start of the string. Everything else is described above. Putting it together, if $key starts with "filetoupload_" and ends with digits, give me those digits in $1.
| [reply] [d/l] [select] |