Click here to get back home

Need help with a simple (I think) Perl script

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Need help with a simple (I think) Perl script Dick Sutton 05-28-2008
Posted by Dick Sutton on May 28, 2008, 11:01 pm
Please log in for more thread options
First of all, I am a rank amateur at Perl. Here is my problem: I have a
hundred or more files in a directory on a web server (let's call it
'Library'). Each file is a pdf file and is named 'yyyymmm.pdf' where yyyy
is the year (i.e. 2007) and mmm is the first 3 letters of the month (i.e.
Jan). So a typical file name looks like '2007Jan.pdf'.

I wrote a simple html page using FORM that allows the user to select the
year and the month and then press the SUBMIT button and I want the
respective pdf file returned into the users browser. The problem is, I
don't know how to return a pdf file to the browser.

Here's what I have so far:

#!/usr/local/bin/perl -wT
use strict;
use CGI ':standard';

# declare variables...
my $year;
my $month;
my $pdffile;

# get the parameters...
$year = param('Year');
$month = param('Month');

# construct the relative pathname to the actual PDF file
$pdffile = '../Library/'.$Year.$Month.'.pdf';

print 'Content-type: application/pdf\n\n';

This is where I'm stuck. Can someone push me in the right direction. I
would think it should be trivial. I just don't know how to procede.

Thanks in advance...

Dick


Posted by Frank Seitz on May 29, 2008, 12:26 am
Please log in for more thread options
Dick Sutton wrote:
>
> Here's what I have so far:
>
> #!/usr/local/bin/perl -wT
> use strict;
> use CGI ':standard';
>
> # declare variables...
> my $year;
> my $month;
> my $pdffile;
>
> # get the parameters...
> $year = param('Year');
> $month = param('Month');
>
> # construct the relative pathname to the actual PDF file
> $pdffile = '../Library/'.$Year.$Month.'.pdf';
>
> print 'Content-type: application/pdf\n\n';
>
> This is where I'm stuck. Can someone push me in the right direction. I
> would think it should be trivial. I just don't know how to procede.

$\ = undef;
open my $fh,'<',$pdffile or die $!;
print <$fh>;

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by Uri Guttman on May 29, 2008, 1:33 am
Please log in for more thread options

FS> $\ = undef;

that should be $/. this is another reason File::Slurp is useful. no need
to know this.

FS> open my $fh,'<',$pdffile or die $!;
FS> print <$fh>;

use File::Slurp ;
print read_file( $pdffile ) ;

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Posted by Frank Seitz on May 29, 2008, 1:49 am
Please log in for more thread options
Uri Guttman wrote:
>
> FS> $\ = undef;
>
> that should be $/.

You are right. The code works anyway (because
print provides a list context).

> this is another reason File::Slurp is useful. no need
> to know this.
>
> FS> open my $fh,'<',$pdffile or die $!;
> FS> print <$fh>;
>
> use File::Slurp ;
> print read_file( $pdffile ) ;

There is one disadvantage: File::Slurp is not in the core.

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by Uri Guttman on May 29, 2008, 10:45 am
Please log in for more thread options

FS> Uri Guttman wrote:

FS> open my $fh,'<',$pdffile or die $!;
FS> print <$fh>;
>>
>> use File::Slurp ;
>> print read_file( $pdffile ) ;

FS> There is one disadvantage: File::Slurp is not in the core.

but many use it anyway. the not in core module excuse is weak as is the
"i can't install/use cpan" excuse.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Similar ThreadsPosted
How to call a simple perl script from HTML without need of HTTPS but simple HTTP ? May 11, 2006, 4:37 am
Newbie wants a simple script in perl April 5, 2008, 3:41 pm
Can someone write me a simple Perl script for a few bucks? February 3, 2006, 11:20 pm
Improving performance of a simple Perl script July 28, 2006, 5:20 pm
launch simple perl script from browser January 15, 2007, 6:35 pm
simple perl script for file uploads ? September 11, 2008, 1:32 am
need simple perl script to do secure data transmission July 28, 2004, 4:43 am
simple perl script for automatic form submission. November 12, 2004, 6:03 pm
"Convert" perl command line to simple script June 28, 2007, 11:44 am
Amending Posting Guidelines [was re: Improving performance of a simple Perl script] July 28, 2006, 6:54 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap