PDA

View Full Version : Getting blank screen when it should be outputting file


chump2877
01-21-2006, 01:42 AM
First off, I'm new to Perl, but know PHP quite well....

There are no error messages, the output of the code is just a blank screen...Why?

Here is the code:

#!c:\apache2triad\perl\bin\perl.exe -w

use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/html\n\n";

# Open session id file

$session_file = "c:/apache2triad/temp/" . $ENV{'request_STRING'};

open (MYFILE, $session_file);
while (<MYFILE>)
{
chomp;
@lines = <MYFILE>; # Read it into an array
}
close (MYFILE);

print @lines; # Print the array

Where the file name is obtained from the URL via the GET method:

http://www.datamonkeys.co.uk/webmail/webmail/perl/create_mbox.cgi?sess_40b971496e891148c3ed0760576ed7ce

FishMonger
01-21-2006, 04:01 AM
That's an odd approach.

First, you should be using the CGI module.
http://search.cpan.org/~lds/CGI.pm-3.15/CGI.pm

During development/debugging, you might want to use warningsToBrowser.

You should be using the strict pragma.

The name of the environment variable that you're using is wrong, it should be,
$ENV{QUERY_STRING}

You're not checking the sucess/failure status of the filehandle.
open (MYFILE, $session_file) || die "can't open $session_file $!";

If you want to slurp the file into the array, there's no need to use the while loop.

Here's a modified version that covers most of the issues.
#!c:\apache2triad\perl\bin\perl.exe -w

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

print header();
warningsToBrowser(1);
print start_html();

# Open session id file

my $session_file = "c:/apache2triad/temp/" . $ENV{QUERY_STRING};
open (MYFILE, $session_file) || die "can't open $session_file $!";
my @lines = <MYFILE>; # Read it into an array
close (MYFILE);

print "$_<br>" for @lines; # Print the array
print end_html();

chump2877
01-21-2006, 06:31 AM
I appreciate your help so far...

I took what you gave me and expanded on it some:

#!c:\apache2triad\perl\bin\perl.exe -w

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

print header();
warningsToBrowser(1);
print start_html();

# Open session id file

my $session_file = "c:/apache2triad/temp/sess_" . $ENV{QUERY_STRING};

open (MYFILE, $session_file) || die "can't open $session_file $!";
my $file = <MYFILE>; # Read it into a variable
close (MYFILE);


if ($file =~ /sent/)
{
my $mbox = 'sent';
}
if ($file =~ /drafts/)
{
my $mbox = 'drafts';
}
if ($file =~ /deleted/)
{
my $mbox = 'deleted';
}

if ($mbox == "")
{
print 'Session file does not contain a mailbox name.<br><br>';
exit();
}

print $mbox;


#print $file; # Print file contents

exit();


print end_html();

The session file will always have either "sent, "drafts", or "deleted" inside of it, so I tried to run an expression match for those 3 strings....

The problem is that when I run this code, I get the message:

Session file does not contain a mailbox name.

Which means the code isn;t finding one of those 3 words in the file...but it should be...

Here is an example of my session file contents:

username|s:6:"Username";password|s:9:"Password";pref|i:1;constant|i:0;mbox_name|s:4:"sent";mailbox|s:10:"Sent_Items";

you'll see that the word "sent" is in there, but the pattern matching code isn;t finding it...Why?

Thanks again...

FishMonger
01-21-2006, 08:15 AM
Since you're using a scalar instead of the array, you're only reading the first line of the file. Is very first line of the file what you want and is it the line you posted?

if ($mbox == "")
That is a numerical equality test, not a string equality test.

Here's a cleaner version.
#!c:\apache2triad\perl\bin\perl.exe -w

use strict; # ALL OF YOUR SCRIPTS SHOULD INCLUDE THE STRICT PRAGMA
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

print header();
warningsToBrowser(1);
print start_html();

# Open session id file
my $session_file = "c:/apache2triad/temp/sess_" . $ENV{QUERY_STRING};

open (MYFILE, $session_file) || die "can't open $session_file $!";
my $file = <MYFILE>; # Read THE FIRST LINE into a variable
close (MYFILE);

my $mbox;
if ($file =~ /(drafts|sent|deleted)/) {
$mbox = $1;
print $mbox;
}
else {
print 'Session file does not contain a mailbox name.<br><br>';
}

print end_html();

chump2877
01-21-2006, 08:17 AM
Since you're using a scalar instead of the array, you're only reading the first line of the file. Is very first line of the file what you want and is it the line you posted?

The file is only one line long, so i went the scalar route....

I'll take a look at your code and see what i can do...thanks

chump2877
01-21-2006, 08:44 AM
Wow what you did works great! :thumbsup:

if ($file =~ /(drafts|sent|deleted)/)
{
$mbox = $1;
print $mbox;
}

how does the regular expression match all of the sudden end up in $mbox...in other words, what is $1 ?

I thought the regular expression match returned either true or false, or the number of matches found, or the position of the first match....

Please explain, thank you

FishMonger
01-21-2006, 03:35 PM
The regex is returning true or false, but it is also capturing the match, by use of the parenthesis, into the $1 variable. We then use $1 to assign the value to $mbox. If you're not needing to use $mbox elsewhere, you can leave that assignment out and the print statement becomes print $1;. The regex is also using alternations (the | between the words) which allows it to match drafts or sent or deleted without having the separate regex's and if statements.

I highly recommend reading Mastering Regular Expressions.
http://www.oreilly.com/catalog/regex2/

chump2877
01-23-2006, 12:54 PM
Do you know of any good online tutorials for mastering regular expressions.....I found this: http://us2.php.net/manual/en/reference.pcre.pattern.syntax.php

But that is really only good for reference i think...thanks

nkrgupta
01-23-2006, 01:04 PM
Do you know of any good online tutorials for mastering regular expressions.....

http://www.troubleshooters.com/codecorn/littperl/perlreg.htm
http://www.crazygrrl.com/weav/regex.php3
http://www.sthomas.net/oldpages/roberts-perl-tutorial.htm#77-BasicRegularExpressions
http://wadict.soas.ac.uk/wadict/reg_ex_from_mysql_manual.html
http://www.oreillynet.com/pub/a/network/2002/07/15/regexp.html

And a book at http://www.oreilly.com/catalog/regex2/

Naveen