Re: easiest `cat` in perl

2003-10-04 Thread Bryan Harris

>> And what is the "T" in -Tw?  That doesn't appear to show up in the man
>> page...
> 
> T means tainted.  It's what you want to run on all code in your cgi-bin
> directory so that a hacker can't r00t your box.  Basically it prevents
> your perl script from doing anything dumb.
> 
> Out of curiousity, is the file you are copying text / html?  I noticed
> your mime type said text/html and figured that could cause you problems
> if, for instance, you were trying to output a binary file.


Why would I want to do that?  I'm pretty new at the cgi/mime stuff, so I'm
not sure how that all works together.  In this case I'm just dynamically
creating a web page in html (or text for testing purposes).

Thanks for the info on the T, though I'd be interested in knowing exactly
what it does.  How could a hacker root my machine if I didn't have the -T
option set?

- B


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-03 Thread Dan Anderson
> And what is the "T" in -Tw?  That doesn't appear to show up in the man
> page...

T means tainted.  It's what you want to run on all code in your cgi-bin
directory so that a hacker can't r00t your box.  Basically it prevents
your perl script from doing anything dumb.

Out of curiousity, is the file you are copying text / html?  I noticed
your mime type said text/html and figured that could cause you problems
if, for instance, you were trying to output a binary file.

-Dan


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-03 Thread Bryan Harris


> Ahh.  A buffering issue.
> Your content-type: is not appearing before the header.incl.  You need
> to add "$| = 1;" before the print.
> 
> That's the reason I start nearly all my CGI scripts with:
> 
>   #!/usr/bin/perl -Tw
>   use strict;
>   $|++;
> 
> so that I don't ever have to worry about STDOUT buffering.


Wow, I never would've guessed.  Isn't the copy command sending stuff to
STDOUT just like print?  How is the stuff in the copy command beating the
stuff in the print command?

And what is the "T" in -Tw?  That doesn't appear to show up in the man
page...

Thanks for helping with this, I never would've figured that out.

- Bryan



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-03 Thread Randal L. Schwartz
> "Bryan" == Bryan Harris <[EMAIL PROTECTED]> writes:

>> use File::Copy;
>> copy "header.incl", \*STDOUT;


Bryan> I like this, it's very clean.

Bryan> Unfortunately I'm having trouble using it in a cgi script...

Bryan> **
Bryan> #!/usr/bin/perl -w
Bryan> use File::Copy;

Bryan> print "Content-type: text/plain\n\n";

Bryan> copy "header.incl", \*STDOUT;
Bryan> print "More stuff goes here\n";

Bryan> **

Ahh.  A buffering issue.
Your content-type: is not appearing before the header.incl.  You need
to add "$| = 1;" before the print.

That's the reason I start nearly all my CGI scripts with:

#!/usr/bin/perl -Tw
use strict;
$|++;

so that I don't ever have to worry about STDOUT buffering.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-02 Thread Bryan Harris

> use File::Copy;
> copy "header.incl", \*STDOUT;


I like this, it's very clean.

Unfortunately I'm having trouble using it in a cgi script...

**
#!/usr/bin/perl -w
use File::Copy;

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

copy "header.incl", \*STDOUT;
print "More stuff goes here\n";

**

Apache calls this an "Internal Server Error", though it works fine from the
command line.  And scripts without the copy seem to work fine.  Any idea
why?

TIA.

- Bryan



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-02 Thread Steve Grazzini
On Thu, Oct 02, 2003 at 05:17:34PM +0200, Thomas B?tzler wrote:
> Todd Wade <[EMAIL PROTECTED]> wrote:
> > "Gary Stainburn" <[EMAIL PROTECTED]> wrote in message
> > news:[EMAIL PROTECTED]
> > > print while();
> 
> This is bad because it first pulls in the file to
> build the list.

It doesn't.  The "while" modifier is a looping construct.
Except for the lack of a block, these are exactly the same.

  print while ;

  while () { print }

They both read and print one-line-at-a-time.

> > print ;
> > 
> > print takes a list of arguments:
> 
> Just to be nit-picking (and to repeat what I learned
> from a M.J. Dominus talk at YAPC::EU this year ;-))
> 
> "print'ing a list is slower than printing a single
> (large) scalar."
> 
> In this particular case it might be worthwhile to
> use "slurping":

But this does load the whole file in memory, and that really
is sloppy.  Sometimes you know the file is going to be small,
and so the sloppiness is acceptable, but the general rule is:
don't slurp files unless you have to.  It doesn't scale.

-- 
Steve

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: easiest `cat` in perl

2003-10-02 Thread Thomas Bätzler
Joshua Colson [mailto:[EMAIL PROTECTED] suggested:
> sub load_file {
>   my($file,$html) = shift;
>   $html = '';
>   open(FILE, "$file") or die "Cannot open $file for reading: $!"
>   while() { $html .= $_; }
>   return $html;
> }

Instead of "while() { $html .= $_; }", you
could use "$html = join("", )".

HTH,
Thomas

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: easiest `cat` in perl

2003-10-02 Thread Thomas Bätzler
Todd Wade <[EMAIL PROTECTED]> wrote:

> "Gary Stainburn" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
[...]
> > One thing you forgot was to close the file.  Also, don't 
> > forget that you can do it with less typing:

Closing files is optional in Perl ;-) Filehandles
will either be closed when the program terminates
when the filehandle is opened the next time.

> > $file = "/location/of/header.incl";
> > open (FH, "$file") or die "Cannot open file $file $!";

If you wanted to pipe other stuff than just plain
text, then 

binmode(FH);

would be a good idea for portability.

> > print while();

This is bad because it first pulls in the file to
build the list.

> > close(FH);

> print ;
> 
> print takes a list of arguments:

Just to be nit-picking (and to repeat what I learned
from a M.J. Dominus talk at YAPC::EU this year ;-))

"print'ing a list is slower than printing a single
(large) scalar."

In this particular case it might be worthwhile to
use "slurping": We create a scope in which the input
record separator $/ is temporarily set to undef.
Reading from  then returns just a single record.

The payoff - printing is nearly twice as fast.

#!/usr/bin/perl -w

use strict;
use Benchmark;

my $file = "out.txt";
my $count = 2;

open( OUT, ">$file") or die "Can't open '$file':$!";

print "print using a scalar:\n";
timethis( $count, 'open(IN, "$0"); { local $/; undef $/; print OUT ; };
close( IN)' );

close( OUT );
unlink $file;

open( OUT, ">$file") or die "Can't open '$file':$!";

print "print using a list:\n";
timethis( $count, 'open(IN, "$0"); print OUT ; close( IN)' );

close( OUT );
unlink $file;
__END__
print using a scalar:
timethis 2:  6 wallclock secs ( 2.85 usr +  2.44 sys =  5.30 CPU) @
3775.72/s (n=2)
print using a list:
timethis 2: 11 wallclock secs ( 7.88 usr +  2.67 sys = 10.56 CPU) @
1894.84/s (n=2)

Cheers,
Thomas

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-02 Thread Randal L. Schwartz
> "Bryan" == Bryan Harris <[EMAIL PROTECTED]> writes:

Bryan> What I'm interested in is what's the easiest way to print the
Bryan> contents of a file?  For example, if I want to output my header
Bryan> file, "header.incl", how can I print the contents of that file
Bryan> the easiest (and most generally compatible)?

Easy, and efficient:

  use File::Copy;
  copy "header.incl", \*STDOUT;

Accept no substitutes.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-02 Thread Joshua Colson
# Set $header_file to the PATH to header.incl
my $header_file = 'header.incl';
# Call load_file() which takes a filename as an argument and
# returns the contents of that file. Then print what load_file()
# returns.
print load_file($header_file);

sub load_file {
  my($file,$html) = shift;
  $html = '';
  open(FILE, "$file") or die "Cannot open $file for reading: $!"
  while() { $html .= $_; }
  return $html;
}  

# Hope that helps.

On Wed, 2003-10-01 at 23:14, Bryan Harris wrote:
> I'm just barely starting into the world of CGI.  I think this is going to be
> the best thing I ever did.  What I think I want to do is have a library of
> HTML snippets (like a generic header and footer), and then use perl to
> output them in order along with any custom content.
> 
> What I'm interested in is what's the easiest way to print the contents of a
> file?  For example, if I want to output my header file, "header.incl", how
> can I print the contents of that file the easiest (and most generally
> compatible)?
> 
> I've tended to shy away from backticks because I kind-of feel like they're
> not "real" perl, and somehow less portable.  Should I feel that way?
> 
> TIA.
> 
> - Bryan
> 
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-02 Thread Peter Scott
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Todd Wade) writes:
>
>"Gary Stainburn" <[EMAIL PROTECTED]> wrote in message
>news:[EMAIL PROTECTED]
>> On Thursday 02 Oct 2003 10:25 am, Owen wrote:
>> > On Wed, 01 Oct 2003 23:14:00 -0700
>> >
>> > Bryan Harris <[EMAIL PROTECTED]> wrote:
>> > > What I'm interested in is what's the easiest way to print the contents
>of
>> > > a file?  For example, if I want to output my header file,
>"header.incl",
>> > > how can I print the contents of that file the easiest (and most
>generally
>> > > compatible)?
>> >
>
>>
>> One thing you forgot was to close the file.  Also, don't forget that you
>can
>> do it with less typing:
>>
>> $file = "/location/of/header.incl";
>> open (FH, "$file") or die "Cannot open file $file $!";
>> print while();
>> close(FH);
>> -- 
>
>or simply:
>
>print ;

Reasonable for short files, wastes memory for big ones.  This file
opening looks like too much work.  I prefer:

{
  local @ARGV = "/location/of/header.incl";
  print while <>;
}

-- 
Peter Scott
http://www.perldebugged.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-02 Thread Todd Wade

"Gary Stainburn" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> On Thursday 02 Oct 2003 10:25 am, Owen wrote:
> > On Wed, 01 Oct 2003 23:14:00 -0700
> >
> > Bryan Harris <[EMAIL PROTECTED]> wrote:
> > > What I'm interested in is what's the easiest way to print the contents
of
> > > a file?  For example, if I want to output my header file,
"header.incl",
> > > how can I print the contents of that file the easiest (and most
generally
> > > compatible)?
> >

> >
> > You may wish to change the print line to   print "$_\n";if you
want
> > a new line on your html for each new line in your file.
> >
> >
> >
> > Owen
>
> Hi Owen,
>
> One thing you forgot was to close the file.  Also, don't forget that you
can
> do it with less typing:
>
> $file = "/location/of/header.incl";
> open (FH, "$file") or die "Cannot open file $file $!";
> print while();
> close(FH);
> -- 

or simply:

print ;

print takes a list of arguments:

[EMAIL PROTECTED] trwww]$ perl
use warnings;
use strict;

print ;

__DATA__
one
two
three
Ctrl-D
one
two
three


Todd W.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-02 Thread Gary Stainburn
On Thursday 02 Oct 2003 10:25 am, Owen wrote:
> On Wed, 01 Oct 2003 23:14:00 -0700
>
> Bryan Harris <[EMAIL PROTECTED]> wrote:
> > What I'm interested in is what's the easiest way to print the contents of
> > a file?  For example, if I want to output my header file, "header.incl",
> > how can I print the contents of that file the easiest (and most generally
> > compatible)?
>
> Firstly specify the file;
>
> $file = "/location/of/header.incl";
>
> Next open it for reading
>
> open (FH, "$file") or die "Cannot open file $file $!";
>
>
> Lastly, go through the file line by line and print them
>
>
> while(){
> print "$_"; #$_ is the value of the current line
> }
>
> You may wish to change the print line to   print "$_\n";if you want
> a new line on your html for each new line in your file.
>
>
>
> Owen

Hi Owen,

One thing you forgot was to close the file.  Also, don't forget that you can 
do it with less typing:

$file = "/location/of/header.incl";
open (FH, "$file") or die "Cannot open file $file $!";
print while();
close(FH);
-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: easiest `cat` in perl

2003-10-02 Thread Owen
On Wed, 01 Oct 2003 23:14:00 -0700
Bryan Harris <[EMAIL PROTECTED]> wrote:

> 
> What I'm interested in is what's the easiest way to print the contents of a
> file?  For example, if I want to output my header file, "header.incl", how
> can I print the contents of that file the easiest (and most generally
> compatible)?

Firstly specify the file;

$file = "/location/of/header.incl";

Next open it for reading

open (FH, "$file") or die "Cannot open file $file $!";  


Lastly, go through the file line by line and print them


while(){
print "$_"; #$_ is the value of the current line
}

You may wish to change the print line to   print "$_\n";if you want a new line 
on your html for each new line in your file. 



Owen







-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]