[12599] in Perl-Users-Digest
Perl-Users Digest, Issue: 13 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 2 20:57:27 1999
Date: Fri, 2 Jul 1999 17:53:12 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 2 Jul 1999 Volume: 9 Number: 13
Today's topics:
newbie help... should be simple tony____d@my-deja.com
Re: newbie help... should be simple (Andreas Fehr)
Re: newbie help... should be simple <dodger@dodger.org>
Re: newbie help... should be simple <swiftkid@bigfoot.com>
Re: newbie help... should be simple (John Borwick)
Re: newbie help... should be simple <swiftkid@bigfoot.com>
Re: newbie help... should be simple tony____d@my-deja.com
Re: newbie in PERL: Can you please suggest a solution? (Andreas Fehr)
Re: Newbie is back!! This time knows his stuff (he thin <gellyfish@gellyfish.com>
Re: Newbie questions (Andreas Fehr)
Re: Newbie questions (Bart Lateur)
Re: Newbie questions (David Cantrell)
Re: Newbie questions <upsetter@ziplink.net>
NT Services: running perl scripts <R.D.Allen@bath.ac.uk>
Order of Output (Ken)
Re: Order of Output <tchrist@mox.perl.com>
parse mail files:beginning Perl in UNIX environment <joeh@adm.com>
Re: parse mail files:beginning Perl in UNIX environment (Ken Pizzini)
Digest Administrivia (Last modified: 1 Jul 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 02 Jul 1999 13:42:34 GMT
From: tony____d@my-deja.com
Subject: newbie help... should be simple
Message-Id: <7lifk7$btt$1@nnrp1.deja.com>
I was wondering if someone would be so kind to help,
I am trying to split a url into pieces, then use one of the pieces (the
last piece) to print to the html page
The elements of the url are:
http://foo.foo.com/bla/bla.dll?bla&blanumber=123546842
All I can do so far is remove the http://, I'd like the script to find
the "=" and print out only how ever many numbers follow, but
only if numbers follow the "=". The result on the html page would look
like:
Here is the Number: 123546842
This is the chunk of code that I thought would get me close to an
answer:
if (/\:\/\/([^\.]*)\.foo\.com(.*)\?(.*)/i) {
# ($Host,$rank,$Increment) = ($1,0,10);
@parts = split(/\&/,$3);
foreach $part (@parts) {
if ($part =~ /^qt=(.*)/) {
$terms = $1;
$terms =~ tr/+/ /;
$terms =~
s/%([a-fA-F0-9][a-fA-F0-9])/pack('C',hex($1))/eg;
}
if ($part =~ /^st=(.*)/) {
$rank = $1;
}
if ($part =~ /^nh=(.*)/) {
$Increment = $1;
}
}
return "<A HREF=\"$_\"$TARGET>Here is the Number:</A>;
}
I've been reading "Perl and CGI for the World Wide Web" by Elizabeth
Castro. It's probable that some people would think that the book may
be lousy if I'm asking for help with a problem like this, but I
assure you that the book is real good and well worth the money,
specially for a first programming book.
Thanks,
-Tony
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
------------------------------
Date: Fri, 02 Jul 1999 14:26:39 GMT
From: backwards.saerdna@srm.hc (Andreas Fehr)
Subject: Re: newbie help... should be simple
Message-Id: <377cc8c4.31939676@news.uniplus.ch>
On Fri, 02 Jul 1999 13:42:34 GMT, tony____d@my-deja.com wrote:
>I was wondering if someone would be so kind to help,
>
>I am trying to split a url into pieces, then use one of the pieces (the
>last piece) to print to the html page
>The elements of the url are:
>
>http://foo.foo.com/bla/bla.dll?bla&blanumber=123546842
>
>All I can do so far is remove the http://, I'd like the script to find
>the "=" and print out only how ever many numbers follow, but
>only if numbers follow the "=". The result on the html page would look
>like:
>
>Here is the Number: 123546842
>
>
Try the following:
my $index;
my $url = "http://foo.foo.com/bla/bla.dll?bla&blanumber=123546842";
# get the index of "=" ==> -1 if no "=" found
$index = index($url, "=");
# increment the index and test if it
if (++$index)
{
# there is probably a better way by getting the substring only
# once e.g. my $number = substr($url, $index); and replace the
# substr(,) by $number.
print substr($url, $index) if (substr($url, $index) !~ m/[^0-9]/);
}
Andreas
------------------------------
Date: Thu, 1 Jul 1999 07:28:56 -0700
From: "Dodger" <dodger@dodger.org>
Subject: Re: newbie help... should be simple
Message-Id: <7ligh4$6in$1@ancalagon.dragon-net.net>
tony____d@my-deja.com wrote in message <7lifk7$btt$1@nnrp1.deja.com>...
>I was wondering if someone would be so kind to help,
>
>I am trying to split a url into pieces, then use one of the pieces (the
>last piece) to print to the html page
>The elements of the url are:
>
>http://foo.foo.com/bla/bla.dll?bla&blanumber=123546842
>
If the URL you are talking about is the URL to the script itself, I'd do
this:
require 'cgi-lib.pl';
&ReadParse(*input);
print 'Content-type: text/html\n\nThe Number Is '.$input{'blanumber'};
If it's something else, you should still download cgi-lib.pl and use it or
an equivelent OO Module like CGI or lwp. The basic capability to split up a
URL with a GET srting has been done so bloody many times that it is a bit
absurd to bother doing it again. Reinventing the wheel gets to the point of
silly.
Dodger
------------------------------
Date: Fri, 2 Jul 1999 19:22:03 +0500
From: "Faisal Nasim" <swiftkid@bigfoot.com>
Subject: Re: newbie help... should be simple
Message-Id: <7ljl7i$7ek11@news.cyber.net.pk>
> http://foo.foo.com/bla/bla.dll?bla&blanumber=123546842
>
> All I can do so far is remove the http://, I'd like the script to find
> the "=" and print out only how ever many numbers follow, but
> only if numbers follow the "=". The result on the html page would look
> like:
>
> Here is the Number: 123546842
use CGI qw/:standard/;
$number = url_param ( 'blanumber' );
#$number contains 123456842
Is that what you want?
--
Faisal Nasim (the Whiz Kid)
Web: http://wss.hypermart.net/
AOL: Whiz Swift ICQ: 4265451
FAX: (815) 846-2877
------------------------------
Date: Fri, 2 Jul 1999 14:57:39 GMT
From: John.Borwick@sas.com (John Borwick)
Subject: Re: newbie help... should be simple
Message-Id: <3782d05c.95469818@newshost.unx.sas.com>
On Fri, 02 Jul 1999 13:42:34 GMT, tony____d@my-deja.com wrote:
>I am trying to split a url into pieces, then use one of the pieces (the
>last piece) to print to the html page
>The elements of the url are:
>
>http://foo.foo.com/bla/bla.dll?bla&blanumber=123546842
>
>All I can do so far is remove the http://, I'd like the script to find
>the "=" and print out only how ever many numbers follow, but
>only if numbers follow the "=". The result on the html page would look
>like:
>
>Here is the Number: 123546842
If you just want the numbers at the end of a string, use
print "Here is the Number: $1\n" if /(\d+)$/;
If you want to find the keys from the POST method
# relies on a hash converting a list to key, value, key, value
my %args_hash =
# 3. map x=y or just add a key $_
map { /^ ( \w+ ) = ( \w+ ) $/x ? ($1, $2) : ($_, "") }
# 2. split query string
split /&/,
# 1. get the query;
(split /\?/,
"http://foo.foo.com/bla/bla.dll?bla&blanumber=123546842")[1]
print $args_hash{'blanumber'};
also try perldoc CGI
and man perlre
--
John Borwick
------------------------------
Date: Fri, 2 Jul 1999 20:02:00 +0500
From: "Faisal Nasim" <swiftkid@bigfoot.com>
Subject: Re: newbie help... should be simple
Message-Id: <7ljnih$7ek12@news.cyber.net.pk>
> require 'cgi-lib.pl';
> &ReadParse(*input);
> print 'Content-type: text/html\n\nThe Number Is '.$input{'blanumber'};
\n\n won't be converted to newlines inside single quotes!
print "Content-type: text/html\n\nThe Number Is $input{'blahnumber'}";
------------------------------
Date: Fri, 02 Jul 1999 17:03:18 GMT
From: tony____d@my-deja.com
Subject: Re: newbie help... should be simple
Message-Id: <7lircd$gvh$1@nnrp1.deja.com>
Slam Dunk! (doin a little touchdown dance)
I don't know how to thank you guys for your help, I got the script
running exactly how I needed it. It's a good feeling. I hope you guys
that offered info; Dodger, Andreas Fehr, Faisal Nasim and John Borwick
have an awesome weekend, God Bless.
-Tony
In article <7lifk7$btt$1@nnrp1.deja.com>,
tony____d@my-deja.com wrote:
> I was wondering if someone would be so kind to help,
>
> I am trying to split a url into pieces, then use one of the pieces
(the
> last piece) to print to the html page
> The elements of the url are:
>
> http://foo.foo.com/bla/bla.dll?bla&blanumber=123546842
>
> All I can do so far is remove the http://, I'd like the script to find
> the "=" and print out only how ever many numbers follow, but
> only if numbers follow the "=". The result on the html page would
look
> like:
>
> Here is the Number: 123546842
>
> This is the chunk of code that I thought would get me close to an
> answer:
>
> if (/\:\/\/([^\.]*)\.foo\.com(.*)\?(.*)/i) {
> # ($Host,$rank,$Increment) = ($1,0,10);
> @parts = split(/\&/,$3);
> foreach $part (@parts) {
> if ($part =~ /^qt=(.*)/) {
> $terms = $1;
> $terms =~ tr/+/ /;
> $terms =~
> s/%([a-fA-F0-9][a-fA-F0-9])/pack('C',hex($1))/eg;
> }
> if ($part =~ /^st=(.*)/) {
> $rank = $1;
> }
> if ($part =~ /^nh=(.*)/) {
> $Increment = $1;
> }
> }
> return "<A HREF=\"$_\"$TARGET>Here is the Number:</A>;
> }
>
> I've been reading "Perl and CGI for the World Wide Web" by Elizabeth
> Castro. It's probable that some people would think that the book may
> be lousy if I'm asking for help with a problem like this, but I
> assure you that the book is real good and well worth the money,
> specially for a first programming book.
>
> Thanks,
> -Tony
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
>
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
------------------------------
Date: Fri, 02 Jul 1999 06:06:58 GMT
From: backwards.saerdna@srm.hc (Andreas Fehr)
Subject: Re: newbie in PERL: Can you please suggest a solution?
Message-Id: <1104_930895618@chstaw0439>
On Thu, 01 Jul 1999 10:31:01 -0400, toby <toby@venice.cas.utk.edu> wrote:
> Use the Text::CSV_XS module which offers an interface to comma separated
> text files (*.csv for MS Excel). The API is similar to SQL and really simple
> to use.
Take care! As this is Microsoft, a comma has not to be a comma, Excel depends on
the list separator settings entered in the Windows international settings. If you enter
a $, your comma (=separator) will be a $ (as in M$).
Andreas
------------------------------
Date: 2 Jul 1999 12:39:29 -0000
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: Newbie is back!! This time knows his stuff (he thinks)
Message-Id: <7libu1$19t$1@gellyfish.btinternet.com>
On Thu, 1 Jul 1999 16:19:16 +1200 Cameron Graham wrote:
> if ( $input[0] eq '' )
> { $error="Please give me your real name"; }
> { &oppshtml }
>
I dont think this does what you think it does - I think you mean
if ( $input[0] eq '' )
{
$error = 'Please give me you real name';
&oppshtml;
}
Even better you should fix your subroutine to take an argument and then
render this as:
&oppshtml('Please give me your real name') if ($input[0] eq '');
Or even neater (IMO)
&oppshtml('Blah balh ...') unless $input[0];
Please see the perlsub manpage if you dont know how to use arguments
in the subroutine.
I would also recommend that you get a good book on Perl:
<http://reference.perl.com/query.cgi?books>
/J\
--
Jonathan Stowe <jns@gellyfish.com>
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
Hastings: <URL:http://www.newhoo.com/Regional/UK/England/East_Sussex/Hastings>
------------------------------
Date: Fri, 02 Jul 1999 05:59:25 GMT
From: backwards.saerdna@srm.hc (Andreas Fehr)
Subject: Re: Newbie questions
Message-Id: <1103_930895165@chstaw0439>
On Thu, 01 Jul 1999 10:19:30 -0800, CapWalker <jamescwalker@csi.com> wrote:
> Sorry to double post, but I can't find my first message...
>
> As a total newbie I need some guidance, either to a FAQ or
> if someone can reply...
>
> 1 - What programs should I get to start Perl ? I've gotten
> version 5.x and the PDK from Activestate
To start with, the ActivePerl is OK.
> 2 - What program in the two above will create a stand alone
> executable ?
There is some tool from Mercury Systems to build Win32 and Unix executables.
Check http://www.demobuilder.com/
I'd recomend to play arround with perl, befor creating exes.
> 3 - I'm going out tonight and buying the camel book from
> O'reilly. Are there others that speak to a Windows 9x
> or NT environment ?
Lerning Perl on Win32 systems. I read it, and it is a good book to
start with, in my opinion. Later on you will need some "Camel" for
more information on perl (or read the html files comming with ActiveState)
Andreas
------------------------------
Date: Fri, 02 Jul 1999 11:03:33 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: Newbie questions
Message-Id: <377f9c4c.526840@news.skynet.be>
Abigail wrote:
>There's learning Perl for Windows though - I've always wondered how
>that book is written. More pictures? One syllable words? Silly sounds?
It doesn't assume that fork() or open(PIPE,"| prog") work, I guess.
Bart.
------------------------------
Date: Fri, 02 Jul 1999 14:10:17 GMT
From: NukeEmUp@ThePentagon.com (David Cantrell)
Subject: Re: Newbie questions
Message-Id: <377cc7fd.75754168@news.insnet.net>
On Thu, 01 Jul 1999 10:19:30 -0800, CapWalker <jamescwalker@csi.com>
said:
>1 - What programs should I get to start Perl ? I've gotten
> version 5.x and the PDK from Activestate
You already have averything you need to run perl on Windows.
>3 - I'm going out tonight and buying the camel book from
> O'reilly. Are there others that speak to a Windows 9x
> or NT environment ?
As you are a complete newbie, buy the Llama book as well.
[Copying newsgroup posts to me by mail is considered rude]
--
David Cantrell, part-time Unix/perl/SQL/java techie
full-time chef/musician/homebrewer
http://www.ThePentagon.com/NukeEmUp
------------------------------
Date: Fri, 02 Jul 1999 16:54:42 GMT
From: Scratchie <upsetter@ziplink.net>
Subject: Re: Newbie questions
Message-Id: <m96f3.642$6M6.195301@news.shore.net>
Abigail <abigail@delanet.com> wrote:
: There's learning Perl for Windows though - I've always wondered how
: that book is written. More pictures? One syllable words? Silly sounds?
I always assumed that it addressed Windows-specific issues or replaced
Unix-specific information with Windows-specific information.
Why not ask Tom? He's co-author. :)
--Art
--
--------------------------------------------------------------------------
National Ska & Reggae Calendar
http://www.agitators.com/calendar/
--------------------------------------------------------------------------
------------------------------
Date: Fri, 2 Jul 1999 18:33:29 GMT
From: Richard Allen <R.D.Allen@bath.ac.uk>
Subject: NT Services: running perl scripts
Message-Id: <377D05F9.A1E65B22@bath.ac.uk>
Hi there,
I am trying to get a script to run as a service under NT. It worked a
treat in Linux (shame the server isn't unix), and works with a couple of
changes in NT. I have followed the directions in the ActivePerl Help
(Services section) as well as the Srvany.exe help info but had no
success.
Anyone out there succeeded at doing this? Any other good FAQ's to have
a good read of?
Cheers,
--
Rich
-----------------------------------------------------------------------------
Richard D Allen R.D.Allen@bath.ac.uk
http://www.bath.ac.uk/~enprda
Ballooning memories are for life,
not just for the duration of the flight.
P.S. "Why be normal, when you can be strange?"
-----------------------------------------------------------------------------
------------------------------
Date: Fri, 02 Jul 1999 21:57:36 GMT
From: kloomis@it-resourcesNOSPAM.com (Ken)
Subject: Order of Output
Message-Id: <377d3567.29417227@news.tiac.net>
I found the following info in the FAQ. Could someone be so kind as to
explain how to use this? Assume I know nothing.
>Q4.12: Why doesn't my system () output come out in the right order?
>This has to do with the way the standard output is buffered. In order for the output to display in the correct order, you need to turn buffering off by using the $| variable:
>$| = 1;
Ken
Ken Loomis
IT Resources
Lexington, MA
www.it-resources.com
------------------------------
Date: 2 Jul 1999 17:01:09 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Order of Output
Message-Id: <377d44b5@cs.colorado.edu>
[courtesy cc of this posting mailed to cited author]
In comp.lang.perl.misc,
kloomis@it-resources.com writes:
:I found the following info in the FAQ. Could someone be so kind as to
:explain how to use this? Assume I know nothing.
:
:>Q4.12: Why doesn't my system () output come out in the right order?
:>This has to do with the way the standard output is buffered. In order for the output to display in the correct order, you need to turn buffering off by using the $| variable:
:
:>$| = 1;
+---------------+
| % man perlvar |
+---------------+
[VERBA DELETA]
$| If set to nonzero, forces a flush right away and after
every write or print on the currently selected output channel.
Default is 0 (regardless of whether the channel is actually
buffered by the system or not; $| tells you only whether you've
asked Perl explicitly to flush after each write). Note that
STDOUT will typically be line buffered if output is to the
terminal and block buffered otherwise. Setting this variable
is useful primarily when you are outputting to a pipe, such as
when you are running a Perl script under rsh and want to see the
output as it's happening. This has no effect on input buffering.
(Mnemonic: when you want your pipes to be piping hot.)
[VERBA DELETA]
+----------------+
| % man perlfaq4 |
+----------------+
How do I flush/unbuffer an output filehandle? Why must I do this?
The C standard I/O library (stdio) normally buffers characters
sent to devices. This is done for efficiency reasons, so that
there isn't a system call for each byte. Any time you use
print() or write() in Perl, you go though this buffering.
syswrite() circumvents stdio and buffering.
In most stdio implementations, the type of output buffering and
the size of the buffer varies according to the type of device.
Disk files are block buffered, often with a buffer size of more
than 2k. Pipes and sockets are often buffered with a buffer size
between 1/2 and 2k. Serial devices (e.g. modems, terminals) are
normally line-buffered, and stdio sends the entire line when it
gets the newline.
Perl does not support truly unbuffered output (except insofar as
you can `syswrite(OUT, $char, 1)'). What it does instead support
is "command buffering", in which a physical write is performed
after every output command. This isn't as hard on your system as
unbuffering, but does get the output where you want it when you
want it.
If you expect characters to get to your device when you print
them there, you'll want to autoflush its handle. Use select()
and the `$|' variable to control autoflushing (see the section
on "$|" in the perlvar manpage and the "select" entry in the
perlfunc manpage):
$old_fh = select(OUTPUT_HANDLE);
$| = 1;
select($old_fh);
Or using the traditional idiom:
select((select(OUTPUT_HANDLE), $| = 1)[0]);
Or if don't mind slowly loading several thousand lines of module
code just because you're afraid of the `$|' variable:
use FileHandle;
open(DEV, "+</dev/tty"); # ceci n'est pas une pipe
DEV->autoflush(1);
or the newer IO::* modules:
use IO::Handle;
open(DEV, ">/dev/printer"); # but is this?
DEV->autoflush(1);
or even this:
use IO::Socket; # this one is kinda a pipe?
$sock = IO::Socket::INET->new(PeerAddr => 'www.perl.com',
PeerPort => 'http(80)',
Proto => 'tcp');
die "$!" unless $sock;
$sock->autoflush();
print $sock "GET / HTTP/1.0" . "\015\012" x 2;
$document = join('', <$sock>);
print "DOC IS: $document\n";
Note the bizarrely hardcoded carriage return and newline in
their octal equivalents. This is the ONLY way (currently) to
assure a proper flush on all platforms, including Macintosh.
That's the way things work in network programming: you really
should specify the exact bit pattern on the network line
terminator. In practice, `"\n\n"' often works, but this is not
portable.
See the perlfaq9 manpage for other examples of fetching URLs
over the web.
[VERBA DELETA]
+--------------------------------------------------------+
| % (leaf through the Perl Cookbook, aka the "Ram Book") |
+--------------------------------------------------------+
Flushing Output
Problem
When printing to a filehandle, output doesn't appear
immediately. This is a problem in CGI scripts running on some
programmer-hostile web servers, where if the web server sees
warnings from Perl before it sees the (buffered) output of your
script, it sends the browser an uninformative ``500 Server
Error''. These buffering problems arise with concurrent access
to files by multiple programs, and when talking with devices or
sockets.
Solution
Disable buffering by setting the per-filehandle variable `$|' to
a true value, customarily 1:
$old_fh = select(OUTPUT_HANDLE);
$| = 1;
select($old_fh);
Or if you don't mind the expense, by calling the `autoflush'
method from the IO modules:
use IO::Handle;
OUTPUT_HANDLE->autoflush(1);
Discussion
In most stdio implementations, buffering varies with the type of
output device. Disk files are block buffered, often with a
buffer size of more than 2k. Pipes and sockets are often
buffered with a buffer size between 1/2 and 2k. Serial devices,
including terminals, modems, mice, and joysticks, are normally
line-buffered; stdio sends the entire line out only when it gets
the newline.
Perl's `print' function does not support truly unbuffered
output, meaning a physical write for each individual character.
Instead, it supports ``command buffering'', in which one
physical write is made after every separate output command. This
isn't as hard on your system as no buffering at all, and it
still gets the output where you want it, when you want it.
Control output buffering through the `$|' special variable.
Enable command buffering by setting it to a true value.
[FOOTNOTE: It has no effect upon input; see ``Reading From The
Keyboard'' and ``Using POSIX Termios'' in Chapter 15, *User
Interfaces*. for unbuffered input.] Set it to a false value to
use default stdio buffering. This illustrates the difference:
#!/usr/bin/perl -w
# seeme - demo stdio output buffering
$| = (@ARGV > 0); # command buffered if arguments given
print "Now you don't see it...";
sleep 2;
print "now you do\n";
If you call this program with no arguments, STDOUT is not
command buffered. Your terminal (console, window, telnet
session, whatever) doesn't receive output until the entire line
is completed, so you see nothing for two seconds and then get
the full line `"Now you don't see it ... now you do"'. If you
call the program with at least one argument, STDOUT is command
buffered. That means you first see `"Now you don't see it..."',
and then after two seconds you finally see `"now you do"'.
The dubious quest for ever-more compact code has led programmers
to use the return value of `select', the filehandle that *was*
currently selected, as part of the second `select':
select((select(OUTPUT_HANDLE), $| = 1)[0]);
Fortunately, there's another way. The FileHandle and IO modules
provide a class method called `autoflush'. Call it with true or
false values (the default value is true) to control autoflushing
on a particular output handle:
use FileHandle;
STDERR->autoflush; # already unbuffered in stdio
$filehandle->autoflush(0);
If you're willing to accept the oddities of indirect object
notation covered in Chapter 13, *Classes, Objects, and Ties*,
you can even write something reasonably close to English:
use IO::Handle;
# assume REMOTE_CONN is an interactive socket handle,
# but DISK_FILE is a handle to a regular file.
autoflush REMOTE_CONN 1; # unbuffer for clarity
autoflush DISK_FILE 0; # buffer this for speed
This avoids the bizarre `select' business, and contributes
greatly to the readability of your code. Unfortunately, your
program takes longer to compile. That's because you're now
including the IO::Handle module, thousands and thousands of
lines must first be read and compiled. Learn to manipulate `$|'
directly, and you'll be happy you did.
When it comes to making sure your output gets where you want it,
when you want it, buffer flushing is important. It's
particularly important with sockets, pipes, and devices, because
you may be trying to do interactive I/O with these. More so, in
fact, because you can't assume line-buffering. Consider this:
#!/usr/bin/perl
# getpcomidx - fetch www.perl.com's index.html document
use IO::Socket;
$sock = new IO::Socket::INET (PeerAddr => 'www.perl.com',
PeerPort => 'http(80)');
die "Couldn't create socket: $@" unless $sock;
# the library doesn't support $! setting; it uses $@
$sock->autoflush(1);
# Mac *must* have \015\012\015\012 instead of \n\n here.
# It's a good idea for others, too, as that's the spec,
# but implementations are encouraged to accept "\cJ\cJ" too,
# and as far as we're seen, they do.
$sock->print("GET /index.html http/1.1\n\n");
$document = join('', $sock->getlines());
print "DOC IS: $document\n";
There's no way to control input buffering using any kind of
flushing discussed so far. For that, you need to see ``Reading
From The Keyboard'' and ``Using POSIX Termios'' in Chapter 15,
*User Interfaces*.
See Also
The `$|' variable in perlvar(1) (Camel:2), the FileHandle(3)
module (Camel:7), and the IO::Handle module, `select' in
perlfunc.
+---------------------------------------------------------------------------------------------------------------------+
| % wget 'http://www.openbsd.org/cgi-bin/man.cgi?query=stdio&apropos=0&sektion=3&manpath=OpenBSD+Current&format=html' |
+---------------------------------------------------------------------------------------------------------------------+
[MEA MAXIMA CULPA -- previous line too long, too long]
STDIO(3) OpenBSD Programmer's Manual STDIO(3)
NAME
stdio - standard input/output library functions
SYNOPSIS
#include <stdio.h>
FILE *stdin;
FILE *stdout;
FILE *stderr;
DESCRIPTION
The standard I/O library provides a simple and efficient buffered
stream I/O interface. Input and output is mapped into logical data
streams and the physical I/O characteristics are concealed. The
functions and macros are listed below; more information is available
from the individual man pages.
A stream is associated with an external file (which may be a
physical de- vice) by opening a file, which may involve creating
a new file. Creating an existing file causes its former contents
to be discarded. If a file can support positioning requests
(such as a disk file, as opposed to a terminal) then a file
position indicator associated with the stream is positioned at
the start of the file (byte zero), unless the file is opened with
append mode. If append mode is used, the position indicator will be
placed at the end-of-file. The position indicator is maintained by
subsequent reads, writes and positioning requests. All input occurs
as if the characters were read by successive calls to the fgetc(3)
function; all output takes place as if all characters were written
by successive calls to the fputc(3) function.
A file is disassociated from a stream by closing the file. Output
streams are flushed (any unwritten buffer contents are transferred
to the host environment) before the stream is disassociated from
the file. The value of a pointer to a FILE object is indeterminate
(garbage) after a file is closed.
A file may be subsequently reopened, by the same or another program
exe- cution, and its contents reclaimed or modified (if it can
be repositioned at the start). If the main function returns to
its original caller, or the exit(3) function is called, all open
files are closed (hence all out- put streams are flushed) before
program termination. Other methods of program termination may
not close files properly and hence buffered out- put may be lost.
In particular, _exit(2) does not flush stdio files. Neither does an
exit due to a signal. Buffers are flushed by abort(3) as required
by POSIX, although previous implementations did not.
This implementation needs and makes no distinction between ``text''
and ``binary'' streams. In effect, all streams are binary.
No translation is performed and no extra padding appears on any
stream.
At program startup, three streams are predefined and need not be
opened explicitly:
o standard input (for reading conventional input),
o standard output (for writing conventional output), and
o standard error (for writing diagnostic output).
These streams are abbreviated stdin, stdout and stderr. Initially,
the standard error stream is unbuffered; the standard input and
output streams are fully buffered if and only if the streams do not
refer to an interactive or ``terminal'' device, as determined by the
isatty(3) func- tion. In fact, all freshly opened streams that refer
to terminal devices default to line buffering, and pending output
to such streams is written automatically whenever such an input
stream is read. Note that this ap- plies only to ``true reads'';
if the read request can be satisfied by ex- isting buffered data, no
automatic flush will occur. In these cases, or when a large amount
of computation is done after printing part of a line on an output
terminal, it is necessary to fflush(3) the standard output before
going off and computing so that the output will appear. Alterna-
tively, these defaults may be modified via the setvbuf(3) function.
The stdio library is a part of the library libc and routines are
automat- ically loaded as needed by the compiler. The SYNOPSIS
sections of the following manual pages indicate which include files
are to be used, what the compiler declaration for the function
looks like and which external variables are of interest.
The following are defined as macros; these names may not be re-used
with- out first removing their current definitions with #undef:
BUFSIZ, EOF, FILENAME_MAX, FOPEN_MAX, L_cuserid, L_ctermid, L_tmpnam,
NULL, SEEK_END, SEEK_SET, SEE_CUR, TMP_MAX, clearerr, feof, ferror,
fileno, freopen, fwopen, getc, getchar, putc, putchar, stderr, stdin,
stdout. Function versions of the macro functions feof, ferror,
clearerr, fileno, getc, getchar, putc, and putchar exist and
will be used if the macro definitions are explicitly removed.
SEE ALSO
close(2), open(2), read(2), write(2)
BUGS
The standard buffered functions do not interact well with certain
other library and system functions, especially vfork and abort.
STANDARDS
The stdio library conforms to ANSI X3.159-1989 (``ANSI C'').
--
The generation of random numbers is too important to be left to chance.
------------------------------
Date: Fri, 02 Jul 1999 16:25:36 -0400
From: Loki <joeh@adm.com>
Subject: parse mail files:beginning Perl in UNIX environment
Message-Id: <377D203F.63C3C795@adm.com>
To tackle parsing some mail files i thought i'd see how perl does
compared to awk;
i wanted to
[a] parse the mail file and
[b] write each message to a file whose name is the sender and an
incremental number: sender.1, sender.2
[c] trap the UNIX signals SIGINT (3) and SIGTERM (15) so i could cleanup
any tmp files
i really didn't get far; someone helped me get going but i am not sure
what i am doing with the $1 which i hope
is both the first arg to the program and also the first arg in the
string beginning with ^From;
In any case, can someone help me understand how to do what i want to do
in Perl
I am running this on Solaris, Linux, and SCO
Thanks!
--
If possible please cc: response posts to joeh@adm.com as I do not always have
access to a news server; thanks!
Disclaimer: opinions expressed my own and not representative of my employers
------------------------------
Date: 2 Jul 1999 23:08:41 GMT
From: ken@halcyon.com (Ken Pizzini)
Subject: Re: parse mail files:beginning Perl in UNIX environment
Message-Id: <slrn7nqenn.deo.ken@pulsar.halcyon.com>
On Fri, 02 Jul 1999 16:25:36 -0400, Loki <joeh@adm.com> wrote:
>i really didn't get far; someone helped me get going but i am not sure
>what i am doing with the $1 which i hope
>is both the first arg to the program and also the first arg in the
>string beginning with ^From;
"$1" in perl refers to the first parenthesized subexpression of
the last RE match; $ARGV[0] (sic) contains the first command-line
parameter.
>In any case, can someone help me understand how to do what i want to do
>in Perl
Try posting some code showing what you _are_ doing. And
comp.lang.perl.misc is really the only one of the newsgroups
that you posted which is relevant; Followups: adjusted accordingly.
--Ken Pizzini
------------------------------
Date: 1 Jul 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 1 Jul 99)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V9 Issue 13
************************************