[25758] in Perl-Users-Digest
Perl-Users Digest, Issue: 7997 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Apr 20 18:05:38 2005
Date: Wed, 20 Apr 2005 15:05:13 -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 Wed, 20 Apr 2005 Volume: 10 Number: 7997
Today's topics:
abs_path($0) gags <mnemotronic@gmail.com>
Re: abs_path($0) gags <jl_post@hotmail.com>
Re: Cgi.pm and CSS <glex_nospam@qwest.invalid>
Re: Cgi.pm and CSS <hackeras@gmail.com>
Re: Cgi.pm and CSS <glex_nospam@qwest.invalid>
Delete characters imphasing@gmail.com
Re: Delete characters <noreply@gunnar.cc>
Re: Delete characters <jl_post@hotmail.com>
glob in specific directory shumaker@cs.fsu.edu
Re: glob in specific directory <mark.clementsREMOVETHIS@wanadoo.fr>
Re: glob in specific directory shumaker@cs.fsu.edu
Re: glob in specific directory <matternc@comcast.net>
Re: glob in specific directory shumaker@cs.fsu.edu
Re: glob in specific directory shumaker@cs.fsu.edu
Re: How to call the remains of a pattern match <tadmc@augustmail.com>
how to evaluate values in a hash <monte_b1@yahoo.com>
Re: how to evaluate values in a hash xhoster@gmail.com
Perl and vmstat <usaims@yahoo.com>
Re: Perl and vmstat <xx087@freenet.carleton.ca>
Re: Perl and vmstat <asolkar@gmail.com>
Re: Perl and vmstat <mark.clementsREMOVETHIS@wanadoo.fr>
perl execution speed surfunbear@yahoo.com
Re: Q on BEGIN and INIT (Anno Siegel)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 20 Apr 2005 13:04:15 -0700
From: "pt" <mnemotronic@gmail.com>
Subject: abs_path($0) gags
Message-Id: <1114027454.988241.311020@g14g2000cwa.googlegroups.com>
I'm doing this:
use Cwd qw/abs_path/ ;
print "path to script = ",abs_path($0),"\n" ;
And I get this:
C:\>perl temp\Util.pl
Uncaught exception from user code:
Cannot chdir to temp\Util.pl: No such file or directory at
temp\Util.pl line 147
Cwd::_croak('Cannot chdir to temp\Util.pl: No such file or
directory') called at C:/Program Files/ActiveState
/Perl/lib/Cwd.pm line 432
Cwd::fast_abs_path('temp\Util.pl') called at temp\Util.pl line
147
The help implies that it should return the path of the argument (the
script name): abs_path and friends
These functions are exported only on request. They each take a single
argument and return the absolute pathname for it. If no argument is
given they'll use the current working directory.
Version info:
This is perl, v5.8.3 built for MSWin32-x86-multi-thread
(with 8 registered patches, see perl -V for more detail)
Copyright 1987-2003, Larry Wall
Binary build 809 provided by ActiveState Corp.
http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Feb 3 2004 00:28:51
------------------------------
Date: 20 Apr 2005 14:50:50 -0700
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: abs_path($0) gags
Message-Id: <1114033849.982182.127380@g14g2000cwa.googlegroups.com>
pt wrote:
> I'm doing this:
>
> use Cwd qw/abs_path/ ;
> print "path to script = ",abs_path($0),"\n" ;
>
> And I get this:
> C:\>perl temp\Util.pl
> Uncaught exception from user code:
> Cannot chdir to temp\Util.pl: No such file or directory at
> temp\Util.pl line 147
> Cwd::_croak('Cannot chdir to temp\Util.pl: No such file or
> directory') called at C:/Program Files/ActiveState
> /Perl/lib/Cwd.pm line 432
> Cwd::fast_abs_path('temp\Util.pl') called at temp\Util.pl
line
> 147
>
>
> The help implies that it should return the path of the argument (the
> script name): abs_path and friends
> These functions are exported only on request. They each take a single
> argument and return the absolute pathname for it. If no argument is
> given they'll use the current working directory.
Good catch. I was able to successfully reproduce your problem, and
by using the debugger I noticed that the abs_path() function was
somehow calling the fast_abs_path() subroutine (I'm not exactly sure
why, but I think it might have to do with something in the XS code).
If you look at the fast_abs_path subroutine (do "perldoc -m Cwd" and
search for "sub fast_abs_path"), you'll notice that it has the line:
my $path = @_ ? shift : ($Curdir ||= File::Spec->curdir);
This line sets the $path to the first parameter, or to the current
directory (".") if there were no parameters passed in. Unfortunately,
the $path variable is treated a few lines later as a path to a
directory (not a file) with the conditional:
if (!CORE::chdir($path)) {
require Carp;
Carp::croak ("Cannot chdir to $path: $!");
}
This means that you are not allowed to pass in a full path to a
file, but rather to a directory (that is, if you do pass in a
paramether). Because of this restriction, I recommend you extract out
the directory path with dirname(), like this:
use Cwd qw/abs_path/;
use File::Basename;
print "path to script = ",
abs_path(-d $0 ? $0 : dirname($0)),
"\n" ;
The "-d" checks to see if $0 is a file or directory. If it is a
directory, it is passed as-is to abs_path(). If it's not a directory,
its directory path is passed to abs_path(). Of course, $0 should
always be a file, so "-d $0" will always fail, making the dirname()
function strip out the directory path of $0 (which could very well be
".").
I hope this helps.
-- Jean-Luc
------------------------------
Date: Wed, 20 Apr 2005 13:49:36 -0500
From: "J. Gleixner" <glex_nospam@qwest.invalid>
Subject: Re: Cgi.pm and CSS
Message-Id: <4vx9e.31$Xo6.598@news.uswest.net>
Nikos wrote:
[...]
> Tr( {-align=>'center'},
> td( {colspan=>3}, font( {-size=>4,
> -color=>'LightSkyBlue'}, $row->{host} ) ) ) ),
>
> br(), br();
> }
>
> But i want to write it more clear with the use of CSS.
> How can i write it?
Your CSS has the classes, however you're not using them. Read up on CSS
and post questions to the various HTML or CSS newsgroups.
Maybe a site like the following may help:
http://stopdesign.com/present/2005/wdw-sf/
When using CSS correctly, your final HTML will not contain any font or
align elements or attributes, and if done really well, you won't have
any table elements at all.
------------------------------
Date: Wed, 20 Apr 2005 22:57:18 +0300
From: Nikos <hackeras@gmail.com>
Subject: Re: Cgi.pm and CSS
Message-Id: <d46c6p$coo$1@nic.grnet.gr>
> When using CSS correctly, your final HTML will not contain any font or
> align elements or attributes, and if done really well, you won't have
> any table elements at all.
Thank you, it sounds very promising! :-)
Also is a way of actually seperating perl with html since all the
formatting attributes are in just one file called style.css :-)
------------------------------
Date: Wed, 20 Apr 2005 15:45:46 -0500
From: "J. Gleixner" <glex_nospam@qwest.invalid>
Subject: Re: Cgi.pm and CSS
Message-Id: <_bz9e.33$Xo6.641@news.uswest.net>
Nikos wrote:
>
>> When using CSS correctly, your final HTML will not contain any font or
>> align elements or attributes, and if done really well, you won't have
>> any table elements at all.
>
>
> Thank you, it sounds very promising! :-)
> Also is a way of actually seperating perl with html since all the
> formatting attributes are in just one file called style.css :-)
Using CSS separates the content from the presentation (layout, color,
font, etc.), nothing at all to do with perl. :-)
------------------------------
Date: 20 Apr 2005 14:51:48 -0700
From: imphasing@gmail.com
Subject: Delete characters
Message-Id: <1114033908.422093.151790@g14g2000cwa.googlegroups.com>
I need to delete the first five characters of a string. Is there an
easy way to do this? It doesn't matter what character, I just need to
delete the first 5. I have a string like this:
45ghsHello There!
or maybe
hf745Hello There!
and I want to get rid of the 45ghs or hf745.
thanks,
Alex
------------------------------
Date: Wed, 20 Apr 2005 23:56:00 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Delete characters
Message-Id: <3co1h3F6nnghnU1@individual.net>
imphasing@gmail.com wrote:
> I need to delete the first five characters of a string. Is there an
> easy way to do this?
perldoc -f substr
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 20 Apr 2005 15:00:10 -0700
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: Delete characters
Message-Id: <1114034410.519271.197560@o13g2000cwo.googlegroups.com>
imphas...@gmail.com wrote:
>
> I need to delete the first five characters of a string. Is there an
> easy way to do this? It doesn't matter what character, I just need to
> delete the first 5. I have a string like this:
>
> 45ghsHello There!
>
> or maybe
>
> hf745Hello There!
>
> and I want to get rid of the 45ghs or hf745.
Dear Alex,
Fortunately for you, there is an easy way to do this.
Say you have a string like:
my $string = "45ghsHello There!";
You can remove the first five characters like this:
substr($string, 0, 5, "");
or even like this:
substr($string, 0, 5) = "";
To read more about substr() and what it can do, type:
perldoc -f substr
at any DOS or UNIX prompt.
I hope this helps, Alex.
-- Jean-Luc
------------------------------
Date: 20 Apr 2005 13:30:24 -0700
From: shumaker@cs.fsu.edu
Subject: glob in specific directory
Message-Id: <1114029024.300512.75040@g14g2000cwa.googlegroups.com>
I am writing a DTS package task in perl but am having trouble with the
glob statment. Basically I want to take all *.dat files in a certain
directory, read them in line by line and output them to a single output
file. I have to use full path names because I don't run the perl
script from a specific directory, it's run by the sql server. (Unless
someone knows how to set the working directory from the perl script?)
I set mydirectory to the value of a global DTS package variable, which
is a user supplied string
$mydirectory == "M:/Database Files/Import/"
open(OUT,"> ".$mydirectory."outputfilename.txt") or
die "Can't open output file: $!";
The output file is created in $mydirectory with no problem, but the
data isn't getting read in.
The script worked fine with:
while (glob "*.dat")#for each .dat file
{
open(IN,$_); #open the .dat file
. . .
when it was a regular script being run from the directory, but when I
try to tell it to search a specific directory:
while (glob $mydirectory."*.dat")
{
open(IN,$_); #open the .dat file
. . .
Nothing is getting written to me output file, and I'm pretty sure it's
something to do with my syntax for the glob or how I'm specifying the
path string.
PS. If my string variable contains single backslash characters instead
of forward slash characters, do the backslashes get treated as escape
sequences, or are escape sequences only interpreted in string literals?
------------------------------
Date: Wed, 20 Apr 2005 22:39:54 +0200
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: glob in specific directory
Message-Id: <4266be1a$0$19355$8fcfb975@news.wanadoo.fr>
shumaker@cs.fsu.edu wrote:
> I am writing a DTS package task in perl but am having trouble with the
> glob statment. Basically I want to take all *.dat files in a certain
> directory, read them in line by line and output them to a single output
> file. I have to use full path names because I don't run the perl
> script from a specific directory, it's run by the sql server. (Unless
> someone knows how to set the working directory from the perl script?)
chdir?
> I set mydirectory to the value of a global DTS package variable, which
> is a user supplied string
> $mydirectory == "M:/Database Files/Import/"
This isn't an assignment.
Mark
------------------------------
Date: 20 Apr 2005 13:43:11 -0700
From: shumaker@cs.fsu.edu
Subject: Re: glob in specific directory
Message-Id: <1114029791.178740.171910@f14g2000cwb.googlegroups.com>
"This isn't an assignment."
Sorry. I know it's not an assignment, I was just saying it is equal to
that. The actual statement in my code is:
$mydirectory =
$DTSGlobalVariables->Item("adhoc_output_dat_file_directory")->{Value};
But I was trying to avoid posting anything DTS specific for clarity.
It seems to be a problem with confusing glob because of spaces in my
path.
------------------------------
Date: Wed, 20 Apr 2005 16:54:08 -0400
From: Chris Mattern <matternc@comcast.net>
Subject: Re: glob in specific directory
Message-Id: <882dnbyI_rjvXPvfRVn-uA@comcast.com>
shumaker@cs.fsu.edu wrote:
> I am writing a DTS
DTS?
> package task in perl but am having trouble with the
> glob statment. Basically I want to take all *.dat files in a certain
> directory, read them in line by line and output them to a single output
> file. I have to use full path names because I don't run the perl
> script from a specific directory, it's run by the sql server. (Unless
> someone knows how to set the working directory from the perl script?)
Um, you might want to read:
perldoc -f chdir
>
> I set mydirectory to the value of a global
Why global?
> DTS package variable, which
> is a user supplied string
It's a user supplied string run by the SQL server...did you remember
to untaint the variable?
> $mydirectory == "M:/Database Files/Import/"
>
> open(OUT,"> ".$mydirectory."outputfilename.txt") or
> die "Can't open output file: $!";
>
> The output file is created in $mydirectory with no problem, but the
> data isn't getting read in.
>
> The script worked fine with:
>
> while (glob "*.dat")#for each .dat file
> {
> open(IN,$_); #open the .dat file
Why aren't you checking to see if your open worked, like you
did with the output file?
> . . .
>
> when it was a regular script being run from the directory, but when I
> try to tell it to search a specific directory:
>
> while (glob $mydirectory."*.dat")
> {
> open(IN,$_); #open the .dat file
> . . .
>
> Nothing is getting written to me output file, and I'm pretty sure it's
> something to do with my syntax for the glob
No, that's fine.
> or how I'm specifying the
> path string.
Possibly. If you checked your open, you could find out why it's
failing. Have die() report back the name it tried to open as
well.
>
> PS. If my string variable contains single backslash characters instead
> of forward slash characters, do the backslashes get treated as escape
> sequences, or are escape sequences only interpreted in string literals?
Only in string literals.
--
Christopher Mattern
"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
------------------------------
Date: 20 Apr 2005 14:13:39 -0700
From: shumaker@cs.fsu.edu
Subject: Re: glob in specific directory
Message-Id: <1114031619.526785.244180@o13g2000cwo.googlegroups.com>
Thanks.
I found documentation that glob doesn't work with directory paths that
have spaces in them.
------------------------------
Date: 20 Apr 2005 14:23:50 -0700
From: shumaker@cs.fsu.edu
Subject: Re: glob in specific directory
Message-Id: <1114032230.671399.73590@o13g2000cwo.googlegroups.com>
Just in case someone else needs the solution for making glob work with
a directory name that has spaces:
while (glob ("\Q$mydirectory*.dat") )
{
The \Q is the key.
Note that the string in $mydirectory ends with a / to seperate the
directory from the *.dat filename.
------------------------------
Date: Wed, 20 Apr 2005 15:58:00 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: How to call the remains of a pattern match
Message-Id: <slrnd6dgio.5fu.tadmc@magna.augustmail.com>
Jim Gibson <jgibson@mail.arc.nasa.gov> wrote:
> In article <Fzv9e.11590$F6.2306405@news.siol.net>, Jure Simsic
>> &sub( ${what's left of $a} )
> 2. There is no need to put ampersand (&) in front of function call.
Unless you've been so silly as to use a reserved word as the
function name, like this OP did.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 20 Apr 2005 13:22:17 -0700
From: "monte" <monte_b1@yahoo.com>
Subject: how to evaluate values in a hash
Message-Id: <1114028537.476888.101030@l41g2000cwc.googlegroups.com>
i need to evalue hte values of a hash for a given key. I am new to perl
and I need a little help. I am doing a hash where the key is the cell
and the value is the lib but now that I got the values how to eval the
values and have them print only once in the output.
INPUT file format:
lib0 cell1
lib2 cell3
lib2 cell4
lib3 cell3
lib0 cell1
lib2 cell4
lib2 cell4
etc..
DESIRED ouput:
cell1 lib0 (lib0 should only appears once in output)
cell3 lib2 lib3 (this is an error)
cell4 lib2
MY code outputs:
cell1 lib0 lib0
cell3 lib2 lib3
cell4 lib2 lib2 lib2
my code is the following but I need help evaluating the values to check
that the current lib is the same as the previous lib and output the
name only once if it is the same lib otherwise return an error message.
can you please give a hint on how to do this values checking?
($rec{$cell}= "Cell:$cell Lib:") if (! exists($rec{$cell}));
$rec{$cell}.="$lib\t"; #this here produces my code output
foreach $cell(keys %rec)
{
print" $rec{$cell}\n";
}
------------------------------
Date: 20 Apr 2005 22:00:47 GMT
From: xhoster@gmail.com
Subject: Re: how to evaluate values in a hash
Message-Id: <20050420180047.819$so@newsreader.com>
"monte" <monte_b1@yahoo.com> wrote:
> i need to evalue hte values of a hash for a given key. I am new to perl
> and I need a little help. I am doing a hash where the key is the cell
> and the value is the lib but now that I got the values how to eval the
> values and have them print only once in the output.
> INPUT file format:
> lib0 cell1
> lib2 cell3
> lib2 cell4
> lib3 cell3
> lib0 cell1
> lib2 cell4
> lib2 cell4
> etc..
>
> DESIRED ouput:
> cell1 lib0 (lib0 should only appears once in output)
> cell3 lib2 lib3 (this is an error)
> cell4 lib2
Are the things in parentheis supposed to be part of the output
or not?
>
> MY code outputs:
> cell1 lib0 lib0
> cell3 lib2 lib3
> cell4 lib2 lib2 lib2
No, your code doesn't output that. Even after I fix it by adding
the missing loop and split, it still doesn't output that.
>
> my code is the following but I need help evaluating the values to check
> that the current lib is the same as the previous lib
You mean previous for the same cell? If each one must be the same
as the previous, then there are only ever allowed to be one lib per
cell, right?
> and output the
> name only once if it is the same lib otherwise return an error message.
What you mean by "return"? There doesn't seem to be any returnable blocks
here.
> can you please give a hint on how to do this values checking?
You are missing something like:
while (<>) { chomp;
my ($lib,$cell)=split;
> ($rec{$cell}= "Cell:$cell Lib:") if (! exists($rec{$cell}));
> $rec{$cell}.="$lib\t"; #this here produces my code output
if (! exists $rec{$cell}) {
$rec{$cell}=$lib;
} else {
die "Error message" unless $rec{$cell} eq $lib;
};
};
> foreach $cell(keys %rec)
> {
> print" $rec{$cell}\n";
> }
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 20 Apr 2005 12:40:15 -0700
From: "usaims" <usaims@yahoo.com>
Subject: Perl and vmstat
Message-Id: <1114026015.321888.318230@z14g2000cwz.googlegroups.com>
Hello:
I am trying to write a small perl script that parses the "us"
column of the vmstat command and if that number is greater or equal to
3, it will notify me. The script does run, but I'm not getting any
output.
#!/usr/bin/perl
use strict;
use warnings;
my $test = 3;
my @memusage = `vmstat -n 1 | awk '{print \$14}'`;
foreach ( my $list = (@memusage) ) {
if ( $list <= $test ) {
print "Failure\n";
}
}
I can get vmstat to output to screen by using:
#!/usr/bin/perl
use strict;
use warnings;
my $test = 3;
my @memusage = system("vmstat -n 1 | awk '{print \$14}'");
foreach ( my $list = (@memusage) ) {
if ( $list <= $test ) {
print "Failure\n";
}
}
But it outputs the vmstat stats but it doesn't evaluate the foreach
statement. I have googled for "vmstat" in the perl group, but
didn't come up with anything. I have searched perldoc, didn't see
anything. Please direct me to a link or a perldoc source or suggest an
alternative. If I have missed something in FAQ, apologies in advance.
Thanks in advance.
usaims
------------------------------
Date: 20 Apr 2005 20:04:36 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: Perl and vmstat
Message-Id: <slrnd6dden.dhr.xx087@smeagol.ncf.ca>
At 2005-04-20 03:40PM, usaims <usaims@yahoo.com> wrote:
> Hello:
>
> I am trying to write a small perl script that parses the "us"
> column of the vmstat command and if that number is greater or equal to
> 3, it will notify me. The script does run, but I'm not getting any
> output.
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> my $test = 3;
> my @memusage = `vmstat -n 1 | awk '{print \$14}'`;
>
> foreach ( my $list = (@memusage) ) {
> if ( $list <= $test ) {
> print "Failure\n";
> }
> }
You should let perl do awk's work:
my $test = 3;
my @output = `vmstat -n 1`;
# the last line of the vmstat output is $output[-1]
my $us = ( split ' ', $output[-1] )[13];
print "Failure\n" if $us >= $test;
> I can get vmstat to output to screen by using:
That's because system() does not capture the output like `backticks` do.
--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca
------------------------------
Date: Wed, 20 Apr 2005 13:38:51 -0700
From: Mahesh A <asolkar@gmail.com>
Subject: Re: Perl and vmstat
Message-Id: <116dferrfvtpr0d@corp.supernews.com>
usaims wrote:
> Hello:
>
> I am trying to write a small perl script that parses the "us"
> column of the vmstat command and if that number is greater or equal to
> 3, it will notify me. The script does run, but I'm not getting any
> output.
>
< snipped code >
There is no output because the vmstat (with -n 1) is still running
(updating its output). Try 'open'ing the command and processing it. E.g.,
-----
#!/usr/bin/perl
use strict;
use warnings;
my $test = 3;
open (CMD, "vmstat -n 1 |");
while (<CMD>) {
if (/^[\d ]+$/) {
my $us = ( split )[13];
print "US = $us\n";
last if (($us >= $test) && ($. > 3));
}
}
close (CMD);
print "Failure!\n";
-----
I am not so familiar with vmstat command usage. When I used it, value of
'us' on the first line is always way greater than 3. So I am looking at
lines later than 3 ($. > 3). You can modify the condition to suit you.
HTH,
Mahesh.
--
Mahesh Asolkar
asolkar-at-gmail-dot-com
------------------------------
Date: Wed, 20 Apr 2005 22:41:09 +0200
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Perl and vmstat
Message-Id: <4266be66$0$19355$8fcfb975@news.wanadoo.fr>
usaims wrote:
> Hello:
>
> I am trying to write a small perl script that parses the "us"
> column of the vmstat command and if that number is greater or equal to
> 3, it will notify me. The script does run, but I'm not getting any
> output.
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> my $test = 3;
> my @memusage = `vmstat -n 1 | awk '{print \$14}'`;
>
> foreach ( my $list = (@memusage) ) {
> if ( $list <= $test ) {
> print "Failure\n";
> }
> }
>
> I can get vmstat to output to screen by using:
> #!/usr/bin/perl
> use strict;
> use warnings;
> my $test = 3;
> my @memusage = system("vmstat -n 1 | awk '{print \$14}'");
>
> foreach ( my $list = (@memusage) ) {
notwithstanding any other issues, your foreach is wrong. Try:
foreach my $list(@memusage){
...
------------------------------
Date: 20 Apr 2005 13:43:06 -0700
From: surfunbear@yahoo.com
Subject: perl execution speed
Message-Id: <1114029786.080723.150500@g14g2000cwa.googlegroups.com>
We have a wrapper language for C someone wrote which basicaly
reads files and operates on the fields. It formats the fields, adding
commas, removing spaces,checking for valid values and many similar
types of operations.
I am wondering if perl would run nearly as fast as such a thing, and
suspect it would. If the kind of processing you are doing could be done
in a 100 lines or so of perl code and the overall issue is how fast the
I/O is and moving one field to another, some pattern matching,
inserting data, I would expect perl would be fairly fast. Any comments
on that ?
------------------------------
Date: 20 Apr 2005 18:08:41 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Q on BEGIN and INIT
Message-Id: <d465r9$ks1$1@mamenchi.zrz.TU-Berlin.DE>
J Krugman <jkrugman345@yahbitoo.com> wrote in comp.lang.perl.misc:
>
>
>
> In Programming Perl, Third Edition, page 484, it says:
>
> INIT blocks are really just like BEGIN blocks, except they let
> the programmer distinguish construction that must happen at
> compile phase from construction that must happen at run phase.
> When you're running a script directly, that's not terribly
> important because the compiler gets invoked every time anyway;
> but when compilation is separate from execution, the distinction
> can be crucial. The compiler may only be invoked once, and the
> resulting executable may be invoked many times.
I don't find the above paragraph very enlightening either.
Having little experience with embedded Perl, and less with mod_perl,
I can't answer your specific questions concerning mod_perl.
This is how I model the behavior of BEGIN, INIT, END, and the rarely
used CHECK blocks:
First off, they are in many respects like subroutines. The code is
not executed immediately, but stored away for later. Also, they are
actual sub-blocks, you can return from them. As with all subroutines,
compilation happens at, well, compile time.
They differ from subs in that there can be more than one of each. All
INIT blocks are collected together, and so are END and CHECK blocks,
possibly BEGIN blocks too, though there it wouldn't matter. INIT blocks
are collected in sequence, CHECK and END blocks in reverse sequence.
That may look weird, but in practice, if it matters at all, it tends
to make sense.
Also unlike real subs, you don't control when they are executed, but
Perl does. BEGIN blocks are executed as soon as each one is compiled
(that's why is doesn't matter if they are collected somewhere). CHECK
happens at the end of the compile phase, and INIT at the beginning of
run time, before the first statement of the main program is run. That
means that CHECK immediately precedes INIT if both are present.
END is run when the program exits, though there are a few ways of
exiting a program without triggering END.
Let's look at the situation in Bekman and Cholet again:
> blocks don't work in mod_perl, for the same reason [this doesn't]:
>
> panic% perl -e 'eval qq(INIT { print "ok\n" })'
Indeed it doesn't work (and gives a warning to that effect). That is
because when eval is called, the INIT block(s) (if any) have already
been run -- before the first executable statement. Even if the block is
added to the INIT sequence by eval (I suppose it is), that has no effect.
A similar case is CHECK, which also cannot be added to at run time,
because it has already happened. In contrast, adding an END block
in a run-time eval *has* an effect because it has yet to be executed.
A BEGIN block in string-eval also shows an effect, because each is
executed immediately.
In general, I wouldn't use an INIT block for general initializing
purposes. Do it at normal run-time. If you want the initializing code
near the code that uses it, there may be reason to do it in a BEGIN
block. I'd use INIT if it were essential that all compilation and
module-loading has been done when it's called. If not, it's just too
hard to say when it will run and when it won't.
Anno
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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.
#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 V10 Issue 7997
***************************************