[22258] in Perl-Users-Digest
Perl-Users Digest, Issue: 4479 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jan 28 21:08:39 2003
Date: Tue, 28 Jan 2003 18:05:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 28 Jan 2003 Volume: 10 Number: 4479
Today's topics:
Re: array slicing <REMOVEsdnCAPS@comcast.net>
Bad Output from Perldoc <jkeen@concentric.net>
Re: cgi-lib to cgi.pm conversion <REMOVEsdnCAPS@comcast.net>
Re: cgi-lib to cgi.pm <REMOVEsdnCAPS@comcast.net>
Re: Email address RE <REMOVEsdnCAPS@comcast.net>
Re: Entering backslashed values into variables when rea <REMOVEsdnCAPS@comcast.net>
exec in windows question (Don)
Re: exec in windows question (Malcolm Dew-Jones)
Re: exec in windows question (Ben Morrow)
How to close all ports ??? <dwayne@hotmail.com>
Re: How to close all ports ??? <spam@thecouch.homeip.net>
Launching parallel scripts (waitpid type thing?) <mthunter@students.uiuc.edu>
Re: Launching parallel scripts (waitpid type thing?) <jurgenex@hotmail.com>
pipes: A script works on SCO, but not on Linux <morden@shadows.net>
Re: question concerning using objects in a module (Ben Morrow)
Re: regular expression (Ben Morrow)
Re: Running a batch command with Location: <eric.ehlers@btopenworld.com.nospam>
Re: Substituting words in Array with items from a hash? <me@home.com>
Re: Substituting words in Array with items from a hash? <REMOVEsdnCAPS@comcast.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 28 Jan 2003 18:58:46 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: array slicing
Message-Id: <Xns9311CB9ACC36Bsdn.comcast@216.166.71.239>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
Thens <thens@nospam.com> wrote in
news:20030128223308.3fb92cbd.thens@nospam.com:
> On Tue, 28 Jan 2003 15:21:45 +0100
> Bernard El-Hagin <bernard.el-hagin@DODGE_THISlido-tech.net> wrote:
>
>> On Tue, 28 Jan 2003 15:20:09 +0100, Bernard El-Hagin
>> <bernard.el-hagin@DODGE_THISlido-tech.net> wrote:
>>
>> >On Tue, 28 Jan 2003 19:31:45 +0530, Thens <thens@nospam.com> wrote:
>> >
[...]
>> >># I want to print numbers greater than or equal to three
>> >># I expect a magical operation to return me the elements in the
>> >>list after THREE.
[...]
>> print join ' ', @numbers[2 .. $#numbers];
>
> This assumes that the offset (2) is known. But i was looking for
> something that will just take the element and find out the offset by
> itself.
Here's a general-purpose function that'll do what you need; I wrote it a
few months ago:
---------- CUT HERE ----------
=item all_after
@list = all_after {code} @input;
Returns a list of the values of @input after (and not including) the
point where {code} returns a true value. Each element of the input
list is assigned to $_ (as an lvalue alias) in turn before invoking
the test code.
Example:
@x = all_after {$_ % 5 == 0} (1..9); # returns 6, 7, 8, 9
=cut
#---> @list = all_after {code} @input;
#
# Change History:
# 10/20/2002 EJR First version
sub all_after (&@)
{
my $test = shift;
my @retval;
my $started;
local $_;
for (@_)
{
my $testval = $test->();
push @retval, $_ if $started;
$started ||= $testval;
}
return @retval;
}
---------- CUT HERE ----------
So in your case, you'd do this:
my @numbers = qw( ONE TWO THREE FOUR FIVE );
my @after3 = all_after { $_ eq 'THREE' } @numbers;
I have some similar functions; feel free to use them as you see fit:
(question for the regulars here: Are these worth submitting to the author
of List::Utils?)
---------- CUT HERE ----------
=item all_before
@list = all_before {code} @input;
Returns a list of the values of @input up until (but not including)
the point where {code} returns a true value. Each element of the
input list is assigned to $_ (as an lvalue alias) before invoking the
test code.
Example:
@x = all_before {$_ % 5 == 0} (1..9); # returns 1, 2, 3, 4
=cut
#---> @list = all_before {code} @input;
#
# Change History:
# 10/20/2002 EJR First version
sub all_before (&@)
{
my $test = shift;
my @retval;
local $_;
for (@_)
{
last if $test->();
push @retval, $_;
}
return @retval;
}
=item none_before
@list = none_before {code} @input;
Returns a list of the values of @input after (and including) the point
where {code} returns a true value. Each element of the input list is
assigned to $_ (as an lvalue alias) in turn before invoking the test
code.
This is almost the same as C<all_after>, except that it includes the
value in the list where {code} first returned true.
Example:
@x = none_before {$_ % 5 == 0} (1..9); # returns 5, 6, 7, 8, 9
=cut
#---> @list = none_before {code} @input;
#
# Change History:
# 10/20/2002 EJR First version
sub none_before (&@)
{
my $test = shift;
my @retval;
my $started;
local $_;
for (@_)
{
$started ||= $test->();
push @retval, $_ if $started;
}
return @retval;
}
=item none_after
@list = none_after {code} @input;
Returns a list of the values of @input up to (and including) the point
where {code} returns a true value. Each element of the input list is
assigned to $_ (as an lvalue alias) in turn before invoking the test
code.
This is almost the same as C<all_before>, except that it includes the
value in the list where {code} first returned true.
Example:
@x = none_after {$_ % 5 == 0} (1..9); # returns 1, 2, 3, 4, 5
=cut
#---> @list = none_after {code} @input;
#
# Change History:
# 10/20/2002 EJR First version
sub none_after (&@)
{
my $test = shift;
my @retval;
local $_;
for (@_)
{
my $testval = $test->();
push @retval, $_;
last if $testval;
}
return @retval;
}
---------- CUT HERE ----------
- --
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBPjcnpWPeouIeTNHoEQIIYACg2u3rko/prbzpb+ZuFWlnST7ixVsAoJWG
/aduTfI3Vc+4kfDa0w2CVpMF
=GwtD
-----END PGP SIGNATURE-----
------------------------------
Date: 29 Jan 2003 01:33:11 GMT
From: "James E Keenan" <jkeen@concentric.net>
Subject: Bad Output from Perldoc
Message-Id: <b17b0n$l8i@dispatch.concentric.net>
I posted this message last week on c.l.p.modules and on an ActiveState list,
but clarification was not achieved.
if (you saw the post there) {
read no further;
} else {
read on;
thanks for any assistance;
}
I have just installed Perl 5.8.0, ActivePerl build 804, on a machine running
Windows95 and I am experiencing problems with the 'perldoc' command running
from the command prompt. Under each of the following three, very common
situations:
perldoc -f [builtinfunction]
perldoc -q [word relating to FAQ]
perldoc [ModuleName]
the text is not properly left-aligning. Instead, it is skewing across the
page as if it were a stack of playing cards being tilted to the right. Each
line of POD starts 1 line below the previous but approx 4-14 characters to
the right of the *last* character in the previous line. Each line then
wraps around the edge of the command prompt window, creating the skewing
effect. For example, the first few lines of the response to 'perldoc -f
push' look like this (beware wrapping in your mail reader):
push ARRAY, LIST
Treats ARRAY as a stack, and pushes the
values of
LIST onto the
end of ARRAY. The length of ARRAY increases by
the len
gth of
... and so forth.
I have installed ActivePerl 5.6.0 and 5.6.1 on several Win95 and Win98
machines and have never experienced this problem. I also have installed
Perl 5.8.0 (ActivePerl) on a WinME, with no problem. It should be noted
that, immediately prior to installing 5.8 on this machine, I uninstalled
ActivePerl 5.6.1 from the same machine.
I searched through 'perldoc.bat' for lines where 'print' appeared to be
printing to STDIN to see if I could trace the problem. I could not (though
that may be because of difficulty in understanding that batch file).
I did a 'diff' on perldoc v2.03 (included with 5.6.1) against perldoc v2.04
(included with 5.8.0). No differences leapt out as a possible explanation
for this problem.
I began to suspect that the extra spaces at the start of lines may be being
generated by Pod::Text, but I am having trouble figuring out exactly where
this might be happening. I did a 'diff' on Pod::Text.pm v2.8 (included in
5.6.1) against Pod::Text.pm v2.20 (included in 5.8). There were many more
differences here than between the versions of perldoc, but, again, no
obvious explanation leapt out at me.
Finally, I searched my archive of the perl-win32-users@ActiveState.com
mailing list as well as did Google searches of comp.lang.perl.* for the
period December 2002 (when this build of ActivePerl was done) to the
present. No relevant hits for combinations like 'perldoc' and 'Pod::Text'.
Has anyone else experienced this? Can anyone help me out? (The perl -v
info follows.) Thanks.
This is perl, v5.8.0 built for MSWin32-x86-multi-thread (with 1 registered
patch, see perl -V for more detail)
Copyright 1987-2002, Larry Wall
Binary build 804 provided by ActiveState Corp. http://www.ActiveState.com
Built 23:15:13 Dec 1 2002
------------------------------
Date: Tue, 28 Jan 2003 19:21:34 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: cgi-lib to cgi.pm conversion
Message-Id: <Xns9311CF76CDAsdn.comcast@216.166.71.239>
Todd Anderson <todd@mrnoitall.com> wrote in
news:3E36C7AF.A4E3E450@mrnoitall.com:
> The search routine below was written using cgi-lib.pl. I am trying to
> convert it to use cgi.pm. It is not returning any found items as is.
> Even with no search parameters.
> Any help is appreciated and thanks in advance.
Have you heard of indenting? It's all the rage.
--
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
------------------------------
Date: Tue, 28 Jan 2003 19:29:24 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: cgi-lib to cgi.pm
Message-Id: <Xns9311D0CAB4BD5sdn.comcast@216.166.71.239>
Bill Smith <bill@asgweb.net> wrote in news:3E36DBBE.A2609204@asgweb.net:
> Dear Sirs,
What makes you think there are no women here?
> I am converting the search routine below, written for cgi-lib.pl to be
> used with cgi.pm. It is not returning any found items as is..
> Any help is appreciated and thanks in advance.
>
Are you Todd Anderson, or are you just in the same class as he?
--
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
------------------------------
Date: Tue, 28 Jan 2003 18:41:01 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Email address RE
Message-Id: <Xns9311C8985BC9Fsdn.comcast@216.166.71.239>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
Matthew Braid <mbear@uq.net.au> wrote in
news:b14ie7$84$1@bunyip.cc.uq.edu.au:
> Hi all,
>
> I've been looking for a package to retrieve a valid email address
> from a string and have had little luck (Mail::Address was a
> little disappointing - given the string "fleeble" it thinks the
> email address is "fleeble" rather than ''. Useful)
"fleeble" is a perfectly valid email address. What's the problem?
> Anyway, after looking at how some other people had done it I came
> up with the function at the end of this message. Note that it
> will return '' if there is no address in the string.
If you're going to reinvent the wheel, then screw what other people
have done; start by reading and understanding rfc 2822.
> Just in case - does anyone know of a package somewhere that does
> the same (better?) I can't use Mail::Address because I'm scanning
> not only for the address, but to know there _is_ an address there
> (which is why Mail::Address fails me - if its just going to return
> something random if there is no address, I'm back to using an RE
> to validate it anyway).
Does Mail::Address return something different each time you call it
with the same values? If not, I might suggest that you don't know
what the word "random" means.
- --
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13
iD8DBQE+NyOMY96i4h5M0egRAvgAAKD9rboZyltmEGK7EGSIJ+PeZ738FwCeNg15
h6pnuXX5P3E1d9b7QOrQVQU=
=B5Nf
-----END PGP SIGNATURE-----
------------------------------
Date: Tue, 28 Jan 2003 17:42:31 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Entering backslashed values into variables when read in from files
Message-Id: <Xns9311BEA74998Csdn.comcast@216.166.71.239>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
Hal Vaughan <hal@thresholddigital.com> wrote in
news:sZdZ9.54020$VU6.45440@rwcrnsc52.ops.asp.att.net:
> How can I read a line like this:
>
> set lf \012
>
> in from a file and translate that to a control code within the
> program?
So some strings (or perhaps all strings) in your input file need to
be scanned for backslash-sequences? That's not so hard.
Before the line
$config{$key} = $val;
do something like
$val =~ s/ \\ # a backslash
0 # a zero
( \d+ ) # some digits
/
chr oct($1) # replace with octal of those digits
/xeg; # whitespace-ignore, execute, global
- --
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13
iD8DBQE+NxXWY96i4h5M0egRAio9AKClsm0r8jjxP5uCE+5CEcxd+tNqegCg4bwT
cmQ38PMUztoe3NoGAMSZoc8=
=fYqN
-----END PGP SIGNATURE-----
------------------------------
Date: 28 Jan 2003 15:41:24 -0800
From: dgnaismith@yahoo.com (Don)
Subject: exec in windows question
Message-Id: <e0f580c3.0301281541.73a0e76@posting.google.com>
I've looked in the FAQ and perldocs and am having a difficult time
finding the answer to the following problem. Also searched the
newsgroups.
I'm trying to run a perl script in windows using wperl.exe thus
keeping the user from seeing a DOS type window. That part seems to
works well.
The commands shown below are running, however, I have to close the
first calc window before the second notepad command will run. example
system (calc);
system (notepad);
If I use exec, the script exits after the first line is run. example
exec "calc";
exec "notepad";
In the above example notepad does not run and this is the norm from
what I've read.
There was mention of using the back ticks or wacks `` - but I was
unable to make use of those as well.
Any ideas would be greatly appreciated. Essenially, I'd like to get
the first command (calc) to execute and then the second command
(notepad) to execute without having to close the calc window. I
actually have about five .exe commands I need to run from the script.
Yeah, I'm new at this. Thanks for any help.
Don
------------------------------
Date: 28 Jan 2003 16:08:18 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: exec in windows question
Message-Id: <3e371b72@news.victoria.tc.ca>
Don (dgnaismith@yahoo.com) wrote:
: The commands shown below are running, however, I have to close the
: first calc window before the second notepad command will run. example
: system (calc);
if NT thinks you're running a batch file then it waits for the spawned
process to exit before it continues the parent process. the NT "start"
command (system('start something')) has options to control this. There
are windows modules that can also help control this, though I don't know
which ones off hand.
: system (notepad);
: If I use exec, the script exits after the first line is run. example
: exec "calc";
: exec "notepad";
: In the above example notepad does not run and this is the norm from
: what I've read.
I would hardly choose to use the words " this is the norm ", though the
words themselves are certainly true.
I would say that that is the "documented behaviour of exec".
: There was mention of using the back ticks or wacks `` - but I was
: unable to make use of those as well.
You should figure out what those commands do, so you can understand
whether you want the functionality they provide. I suspect you do not
want to use them here.
------------------------------
Date: Wed, 29 Jan 2003 00:29:57 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: exec in windows question
Message-Id: <b177a5$79i$1@wisteria.csv.warwick.ac.uk>
dgnaismith@yahoo.com (Don) wrote:
>The commands shown below are running, however, I have to close the
>first calc window before the second notepad command will run. example
>
>system (calc);
>system (notepad);
Yes. system doesn't return until the command exits.
I presume you really meant to write system('calc'), and were actually
running under 'use strict'....?
>If I use exec, the script exits after the first line is run. example
>
>exec "calc";
>exec "notepad";
>
>In the above example notepad does not run and this is the norm from
>what I've read.
Yes. This is what exec does.
>There was mention of using the back ticks or wacks `` - but I was
>unable to make use of those as well.
No. Thy give you the output of the command as a string or list: calc/notepad
et al don't produce any output.
>Any ideas would be greatly appreciated. Essenially, I'd like to get
>the first command (calc) to execute and then the second command
>(notepad) to execute without having to close the calc window. I
>actually have about five .exe commands I need to run from the script.
Either use fork and exec (perldoc -f fork, perldoc -f exec, perldoc perlfork
for some important information for Win32) which apparently works though I
wouldn't trust it an inch (yes, this is just me. Anytime windows starts
trying to be Unixy I tend to find things start breaking); or, for a
non-portable but more flexible and reliable solution use Win32::Process.
Ben
------------------------------
Date: Wed, 29 Jan 2003 00:57:29 GMT
From: David Wayne <dwayne@hotmail.com>
Subject: How to close all ports ???
Message-Id: <7n9e3v0sakiri3dv8fkroi17ovustra030@4ax.com>
I am currently writing a script which opens ports using IO::socket,
but if the script is aborted, I cannot run the script again because
the port I used is still open.
Any suggestions ?
------------------------------
Date: Tue, 28 Jan 2003 20:34:18 -0500
From: Mina Naguib <spam@thecouch.homeip.net>
Subject: Re: How to close all ports ???
Message-Id: <vcGZ9.37791$G61.383497@weber.videotron.net>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
David Wayne wrote:
| I am currently writing a script which opens ports using IO::socket,
| but if the script is aborted, I cannot run the script again because
| the port I used is still open.
|
| Any suggestions ?
|
Check IO::Socket's documentation for the "Reuse" setting.
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQE+Ny+aeS99pGMif6wRAtCjAJoDAFpMtOTINGb2PiLZM6qRttoiUACgnEo2
APh0BYQ9ZkRDIvuZlQUbPG0=
=bpfA
-----END PGP SIGNATURE-----
------------------------------
Date: Tue, 28 Jan 2003 23:28:04 GMT
From: Mike Hunter <mthunter@students.uiuc.edu>
Subject: Launching parallel scripts (waitpid type thing?)
Message-Id: <slrnb3e4lo.fh5.mthunter@ux12.cso.uiuc.edu>
Hi everybody,
I have a driver script which sets up some files, calls 6 other scripts
that deals with the files, and then deletes the fiels once the 6 scripts
are finished.
The machine I'm using is a dual-processor, so I'd like to get 2 of the 6
child scripts running at once. But if I background them, I won't know
how long to wait to delete the files.
I could accomplish this with file locks, but that seems silly. Can
anybody think of an elegant way to do this in perl? I saw that waitpid
is available in posix (?), but I'm currently using system()...do I need
to switch to something fancier?
TIA,
Mike
------------------------------
Date: Tue, 28 Jan 2003 23:34:37 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Launching parallel scripts (waitpid type thing?)
Message-Id: <hsEZ9.2836$jj1.310@nwrddc02.gnilink.net>
Mike Hunter wrote:
> Hi everybody,
>
> I have a driver script which sets up some files, calls 6 other scripts
> that deals with the files, and then deletes the fiels once the 6
> scripts are finished.
>
> The machine I'm using is a dual-processor, so I'd like to get 2 of
> the 6 child scripts running at once. But if I background them, I
> won't know how long to wait to delete the files.
>
> I could accomplish this with file locks, but that seems silly. Can
> anybody think of an elegant way to do this in perl? I saw that
> waitpid is available in posix (?), but I'm currently using
> system()...do I need to switch to something fancier?
perldoc -f fork
perldoc -f wait
jue
------------------------------
Date: Tue, 28 Jan 2003 15:40:09 -0800
From: morden <morden@shadows.net>
Subject: pipes: A script works on SCO, but not on Linux
Message-Id: <b174do$14b$1@bob.news.rcn.net>
This script works just fine on SCO (perl 5.002) but sptbcgen fails to
write to pipe BCGENW on Linux (perl 5.6.1) :
unless (pipe(BCGENR,BCGENW)) {
die("can't open a pipe\n");
}
$opipe=fileno(BCGENW);
$pname = "|sptbcgen ".$opipe." ".fileno(BCGENR);
print STDERR "cmd ".$pname."\n";
print STDERR $opipe." ".fileno(BCGENR)."\n";
unless (open(BCGEN, $pname)) {
die("can't pipe to sptbcgen\n");
}
main(int argc, char *argv[])
{
if(argc>=2) {
int pipeout = atoi( argv[1] );
if(argc>2) {
close(atoi( argv[2] ));
}
int sz = write(pipeout, "%test\n", 6);
if(sz<1) {
fprintf(stderr, "wrote %d bytes with errno %d",
sz, errno);
^^^^^^^^^^^^^^^^^^^^^^^^ on linux for some reason an attempt to write to
fd 5 (BCGENW) failes with EBADF :-(
}
if(!(fpout=fdopen(pipeout, "w"))) {
fprintf(stderr, "I can't open fdout %s/%d error %d\n",
argv[1], pipeout, errno);
exit(22);
}
------------------------------
Date: Wed, 29 Jan 2003 00:12:50 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: question concerning using objects in a module
Message-Id: <b176a2$6qb$1@wisteria.csv.warwick.ac.uk>
mark.wirdnam@stud.unibas.ch (Mark Wirdnam) wrote:
>Hello!
>
>I have tried to understand as much of perltoot as I could (for the
>moment). As you will notice immediately I am an inexperienced amateur
>trying to program a rather large project. In an attempt to keep things
>clear for myself I am seperating the program's tasks into modules.
Good, good.
Remember to start _each_ file with
use warnings;
use strict;
>Here is how I am writing code at the moment:
>
>package Example;
>
>use Package1;
>...
>
>sub new {
> my $self .... # will be blessed
>
> $self->{var1} = ....
> $self->{var2} = ....
It's usually better to wrap attributes in methods thus:
sub var1 {
my $s = shift;
$s->{var1} = shift if @_;
return $s->{var1};
}
and then set with $self->var1(..) even in the class's own methods. It give
better encapsulation and helps prevent problems with inheritance later. (For
the same reason, you should always use the two-arg form of bless.) If you
find writing all these a pain, you may want to look at one of the Class::*
modules on CPAN.
># here comes the bit i'm aiming at: i introduce an object of Package1
>i'll be using
>
> $self->{ref_pac_1} = Package1->new(....
That is right, except you should be using a accessor method as above.
I would prefer
new Package1 arg1, ...
(note the lack of comma after Package1) which does exactly the same whing
but without any brackaets: this is purely stylistic, though.
>...
>...
>} # end new
>
>sub does_something {
> my $self = shift;
>
># and here i use it
> ($self->{ref_pac_1})->do_what_i_want();
You don't need the () brackets.
>...
>...
>}
>
>I hope I have understood correctly that this has nothing to do with
>inheritance, right? I'm just using packages within a package.
Correct, it has nothing to do with inheritance. I would say 'I'm just using
objects within an object' or '... classes within a class', as it's clearer.
>But the way I'm doing it is a lot of typing and not very easy to read.
It's fine. If you use shorter names for stuff, and put things you'll use
several times into temporary variables, it's about as concise as you could
expect.
$obj->subobj->meth();
my $subobj = $obj->subobj;
$subobj->meth();
$subobj->meth2();
$subobj->attr('foo'); # this will set the attribute of the object still
# inside $obj, as $subobj is a reference.
>So is there something wrong with this way? What would be a more
>maintainable way?
Other than using methods for attributes, not much.
Ben
------------------------------
Date: Wed, 29 Jan 2003 00:16:57 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: regular expression
Message-Id: <b176hp$6vc$1@wisteria.csv.warwick.ac.uk>
Greg Schmidt <gregs@trawna.com> wrote:
>On 28 Jan 2003 12:18:16 -0800, bionic_man1@yahoo.com (agape) wrote:
>
>>Hi:
>>
>>I am trying to do the following:
>>given a text file with such fields in it:
>>
>>-term1 /my/home/directory -term2 a.v -term3 15000 -last "var1 var2
>>var3 var4" -t4 /my/current/ -last_one "variable1 variable2" -term4
>>xyz
>>
>>
>>The above could be in any order but any switch(-term1 or -term2 or
>>-last and so forth) is followed by a value of some sort. I desire out
>>of this lengthy text to remove anything that is passed to " " which
>>in this case is -last and the quotes after it as well as -last_one and
>>quotes after it. My resultant should look like this:
>>
>>term1 /my/home/directory -term2 a.v -term3 15000 -t4 /my/current/
>>-term4 xyz
>>
>>Any reg exp you provide is helpful.
>>Agape
>
>$str =~ s/ -\w+ ".*?"//g
>works for me.
I would use s/\s+ -\w+ \s+ ".*?"//gx to allow any flavour of whitespace
separators: the OP will have to decide whether the separator is always a
single space or could be <spc><tab><cr><lf><spc>, for example.
Ben
------------------------------
Date: Wed, 29 Jan 2003 00:20:48 +0000 (UTC)
From: "eric" <eric.ehlers@btopenworld.com.nospam>
Subject: Re: Running a batch command with Location:
Message-Id: <b176p0$3kk$1@venus.btinternet.com>
"David Meyer" <djmeyer1@us.ibm.com> wrote in message
news:3E35928A.3010800@us.ibm.com...
> Hello,
hi
> In a CGI on apache (Redhat 8), I'm trying
> to start a script in the background which
> updates an .html file. After starting the
> batch job, I print a Location: pointing at
> the html file which is updating every 8
> seconds.
>
> The problem is that the html file does not
> show up until the batch job is complete. I'd
> like to be able to refresh a growing html file.
> I had this working awhile back on AIX but am
> having problems on Redhat.....any help is
> much appreciated.
>
> Here's a code fragment:
>
> $cmd="$CGIdir/batchtest &";
> system($cmd) && exit();
> print("Location: $RunTestHTML_URL\n\n");
i think the problem is that the "batchtest" process is buffering its output.
what happens when you run batchtest on its own, and "tail -f" the output
file - do you see the output file being written immediately?, or only
itermittently or when batchtest finishes altogether?
if batchtest is itself a perl script, you might try switching from the
default block buffering to line buffering (flushing the buffer after each
print). here are a couple of ways to do that:
use FileHandle;
open OUTFILE, ">output.htm" or die "can't open output file: $!";
autoflush OUTFILE;
or
open OUTFILE, ">output.htm" or die "can't open output file: $!";
select((select(OUTFILE),$|=1)[0]);
>
> Thanks,
> djmeyer@bga.com
HTH
-eric
------------------------------
Date: Wed, 29 Jan 2003 00:02:03 GMT
From: "MG" <me@home.com>
Subject: Re: Substituting words in Array with items from a hash?
Message-Id: <%REZ9.7929$mA2.120631@twister.tampabay.rr.com>
First let me say I'm definately an amatuer at this, but here's a couple
thoughts....
> $template = $in{'template'}; # Sets the Template file to be used
> # from hidden form field
> template
>
> open WEBFILE, $template or die $!; # Opens the file if exists
I wouldn't do this. A hidden form field can be faked and someone could
theoretically open any file on your system. Unless there's code you've left
out I would use the $in{template} key to hold a reference number or name
that your code can check against a list of 'valid' templates (such as a
config file with '$valid{key value}='/blah/blah/this.template') Just food
for thought.
> @lines = <WEBFILE>; # Read it into an Array
I noticed you are reading this into a list and didn't see where the list
would pose any advantage over a simple string variable. Such as this:
open (WEBFILE) or (some kind of error handling);
while(<WEBFILE>) {
$filecontents .= $_;
}
close (WEBFILE);
Then you do something like
foreach $key (keys(%in)) {
$filecontents =~ s/$key/$hash{$key}/gi;
}
print $filecontents;
That's it. I've left out a lot of code but the important parts are here.
Unless you need the file in a list, use a single string. Line feeds,
carriage returns, etc. will all be preserved. One other thing, if I'm
displaying data that is from a form submission I usually convert '<' and '>'
to < and > to try my part to keep malicious code from being ran
from/on my server (someone could put <!--#exec cmd="command"--> and
depending on how your sever is setup with SSI it would execute the command
when the interpreter hit the line). This also will prevent them from
embedding java, html and just about anything else in the page. Keep in
mind, I'm a weekend warrior and am FAR from being the source for Perl wisdom
but this is what I use and it seems to be fine for this task.
Joe
------------------------------
Date: Tue, 28 Jan 2003 19:18:38 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Substituting words in Array with items from a hash?
Message-Id: <Xns9311CEF7C625Asdn.comcast@216.166.71.239>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
4.spam@cox.net (Mac) wrote in
news:37da63be.0301280940.db246fa@posting.google.com:
> I have read a file (a template for a webpage) into an array.
> I am now trying to scan this array and substitute words that mach the
> keys in the hash %in and replace the words with the values in the hash
> %in. The goal is to take a web form and using the values replace the
> field names with the field values in a template file.
>
> I guess you could say that I am a newbie so be gentle.
No problem. GENTLEMODE=ON.
> Here is my script so far:
>
> #!/usr/bin/perl
Strongly suggest you add the line
use strict;
and possibly
use warnings;
These lines will catch MANY newbie programming mistakes. They're a pain
in the ass, because they cause your program to crash with sometimes
cryptic messages, but the things that cause them to do that are mistakes
that you are making -- which will trip you up sooner or later.
>
> # Process Form data if present
> ###############################################################
>
> ( ! &ReadParse );
Danger Will Robinson Danger!
"&ReadParse" is a symptom of Bad Web Programming. Chuck the ReadParse
function (no doubt you copied it verbatim from somewhere, right?) -- it
is FULL of bugs.
Use the CGI module instead. If you're doing web programming without the
CGI module, you are either doing something wrong, or you're an expert who
knows when to break the rules. Learn the CGI module -- now.
>
> $template = $in{'template'}; # Sets the Template file to be
used
With CGI, this line becomes
$template = param('template'); # See, not much different!
>
> open WEBFILE, $template or die $!; # Opens the file if exists
This isn't hopeles, but "die" causes cryptic error messages for the web
user. (500 error). Consider more elaborate error handling.
>
> @lines = <WEBFILE>; # Read it into an Array
This is wasteful of memory (although in this case, it's probably not a
big waste, since your web pages are probably relatively small). If
you're just going to loop over them, get out of the habit of slurping
them into memory, and get into the habit of looping over the file one
line at a time.
>
> close(WEBFILE); # Close the file
>
> # Return the basic response page. (this works)
> ###############################################################
>
> print "Content-type: text/html\n\n";
Why code it by hand? You'll make typos sooner or later! Use the
function that CGI provides:
print header;
> print "<title>The Response</title><h1>The Response</h1><hr>";
> print "Here is the form data:<ul>";
>
> # Print list items of the form data in %in
> foreach $key (keys %in) {
> print "<li>$key: $in{$key}";
> }
Under CGI, this would become:
foreach $key (param) {
print "<li>$key: ", param($key);
}
> print "</ul>";
>
> # Substitute keys with values from the hash in the array and return
> # response page(this does not work)
> ###############################################################
>
> foreach $key (@lines) {
> $key =~ s/$key/$in{$key}/g;
> }
Right. You're trying to look up the whole line in the substitution list
-- you're trying to use the entire input line as a key into the hash.
What you want to do is to look for keywords in the input line, and
substitute them.
%in = CGI::Vars;
foreach $line (@lines) {
$line =~ s/(\w+)/$in{$1} || $1/eg;
}
This looks for strings of "word characters" and replaces them with values
from the hash, and leaves them alone if they're not in the hash.
----
That's a start; hope it helps.
- --
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBPjcsXGPeouIeTNHoEQKkhgCg5/+alIVwpvbUK655Hvk+USilplkAoIAF
Xt+VeJ+1RWUaKj1TV0yFIIOd
=jjrq
-----END PGP SIGNATURE-----
------------------------------
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.
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 4479
***************************************