[24347] in Perl-Users-Digest
Perl-Users Digest, Issue: 6536 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri May 7 11:05:44 2004
Date: Fri, 7 May 2004 08:05:11 -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, 7 May 2004 Volume: 10 Number: 6536
Today's topics:
Converting a string into hex chars? <Eric@nowhere.com>
Re: Converting a string into hex chars? <depesz@depesz.pl>
Re: Converting a string into hex chars? <noreply@gunnar.cc>
Re: How to redirect STDOUT to a string? (Mark Jason Dominus)
Re: How to redirect STDOUT to a string? <nobull@mail.com>
Re: How to redirect STDOUT to a string? <jkrugman@yahbitoo.com>
Re: List questions on form <Joe.Smith@inwap.com>
Re: newbie; appending multiple files (Anno Siegel)
Re: newbie; appending multiple files <scobloke2@infotop.co.uk>
Re: newbie; appending multiple files <dwall@fastmail.fm>
Re: Perl Calls the second <bik.mido@tiscalinet.it>
Re: Please Recommend A Good Perl Book. (Peter Scott)
read is blocking on win32 CGI (William)
regex utility (Alythh)
Re: regex utility <richard@zync.co.uk>
Re: regex utility <mark.clements@kcl.ac.uk>
Re: regex utility <richard@zync.co.uk>
Re: regex utility <ThomasKratz@REMOVEwebCAPS.de>
Re: regex utility <thepoet_nospam@arcor.de>
Re: regex utility <ThomasKratz@REMOVEwebCAPS.de>
Re: regex utility <bik.mido@tiscalinet.it>
Re: regex utility <bik.mido@tiscalinet.it>
Re: Trapping warnings <raj.kothary@thus.net>
Re: Trapping warnings <ThomasKratz@REMOVEwebCAPS.de>
Re: Trapping warnings <nobull@mail.com>
Re: Trapping warnings (Anno Siegel)
Using $1, $2 ... but don't know in which order <tore@aursand.no>
Re: win32::ole and excel VBA macro conversion: SmallScr <domenico_discepola@quadrachemicals.com>
Re: win32::ole and excel VBA macro conversion: SmallScr <domenico_discepola@quadrachemicals.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 7 May 2004 12:02:36 +0100
From: "Eric" <Eric@nowhere.com>
Subject: Converting a string into hex chars?
Message-Id: <c7fq7l$t4k$1@news7.svr.pol.co.uk>
Hi,
I need to convert a string into a HTML encoded hex equivalent. I'm trying
things along the lines of:
________________________________________________
my $message = 'This is a message with various characters in it.';
my $encmsg = '';
for (my $i=0; $i<length($message); $i++)
{
$encmsg .= "%" . ord(substr($message, $i, 1));
}
print $encmsg;
________________________________________________
But this is producing decimal codes instead of hex ones.
Is there a better way to do this, that works?
Thanks,
Eric
------------------------------
Date: Fri, 7 May 2004 13:16:05 +0200
From: hubert depesz lubaczewski <depesz@depesz.pl>
Subject: Re: Converting a string into hex chars?
Message-Id: <slrn.pl.c9mrvk.a4p.depesz@master.hq.eo.pl>
Eric wyrzeĽbił(a):
> I need to convert a string into a HTML encoded hex equivalent. I'm trying
> things along the lines of:
> ________________________________________________
> my $message = 'This is a message with various characters in it.';
perl -le '
$message="This is a message with various characters in it.";
$message =~ s/./sprintf("%%%02x", ord($&))/ges;
print $message'
depesz
--
napisanie do mnie na priv daje 99.9% gwarancję braku odpowiedzi
*-----------------------------------------------------------------*
sklep z rzeczami do domu: http://ulek.pl/
------------------------------
Date: Fri, 07 May 2004 13:22:19 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Converting a string into hex chars?
Message-Id: <2g1a5lF38lc0U1@uni-berlin.de>
Eric wrote:
> I need to convert a string into a HTML encoded hex equivalent. I'm
> trying things along the lines of:
> ________________________________________________
> my $message = 'This is a message with various characters in it.';
>
> my $encmsg = '';
> for (my $i=0; $i<length($message); $i++)
> {
> $encmsg .= "%" . ord(substr($message, $i, 1));
> }
>
> print $encmsg;
> ________________________________________________
>
> But this is producing decimal codes instead of hex ones.
>
> Is there a better way to do this, that works?
That works? I can't see that you even tried to convert to hexadecimal
numbers. See the sprintf() function.
$encmsg .= sprintf '%%%02X', ord $_ for split //, $message;
Or check out the URI::Escape module.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 7 May 2004 11:25:48 +0000 (UTC)
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: How to redirect STDOUT to a string?
Message-Id: <c7frns$pee$1@plover.com>
In article <c7esdt$l8j$1@reader2.panix.com>,
J Krugman <jkrugman@yahbitoo.com> wrote:
>I'm using a module whose subs all write to STDOUT. I want to
>capture the output of these subs in a Perl string instead.
sub X::TIEHANDLE { my ($c, $r) = @_; bless $r => $c }
sub X::PRINT { my $r = shift; $$r .= join "", @_ }
tie *STDOUT => "X", \$OUTPUT;
and now everything printed to STDOUT will appear in $OUTPUT.
Use
untie *STDOUT;
to turn it off.
------------------------------
Date: 07 May 2004 12:52:46 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: How to redirect STDOUT to a string?
Message-Id: <u9isf8ihdd.fsf@wcl-l.bham.ac.uk>
J Krugman <jkrugman@yahbitoo.com> writes:
> I'm using a module whose subs all write to STDOUT.
Are you sure?
It would be far more normal for them to write to the currently
selected output handle which need not be STDOUT.
> I want to capture the output of these subs in a Perl string instead.
[ snip - almost correct munging of *STDOUT using IO::Scalar ]
> What's the correct way to do what I want to do?
If they do explicitly write to STDOUT then you can mung *STDOUT as
discussed elsewhere in this thread. If not, then you can simply
change the currently selected output handle with select().
Note: in Perl 5.8 the functionality of IO::Scalar is available
using open().
require 5.8.0;
open my $string_handle, '>', \my $string or die $!;
my $save_selection = select $string_handle;
foo(); # prints to current output; output will be in $string
select($save_selection);
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 7 May 2004 14:07:16 +0000 (UTC)
From: J Krugman <jkrugman@yahbitoo.com>
Subject: Re: How to redirect STDOUT to a string?
Message-Id: <c7g56k$37c$1@reader2.panix.com>
Wow! I've learned so much from all of your replies!
Thank you!!!
In <u9isf8ihdd.fsf@wcl-l.bham.ac.uk> Brian McCauley <nobull@mail.com> writes:
>J Krugman <jkrugman@yahbitoo.com> writes:
>> I'm using a module whose subs all write to STDOUT.
>Are you sure?
>It would be far more normal for them to write to the currently
>selected output handle which need not be STDOUT.
You're 100% right. I totally goofed there when I wrote "write to
STDOUT". The module in question just uses print without an explicit
filehandle argument, which, as you say, doesn't imply that the
output will necessarily go to STDOUT.
One related question: is there a way to get the currently selected
filehandle without using select (and therefore deselecting it)? (I
thought that this would be stuck in some Perl variable, but I can't
find it in the perlvar manpage.)
And one more: can someone point me to where in the Perl documentation
lexical filehandles are discussed? I'm sure it's in there somewhere,
but after a lot of page-flipping and poring over my Camel book's
index, I still can't find anything. (My immediate interest is
confirming my guess that "close" is called automatically on such
handles when they go out of scope, but in general I want to get
the full official lowdown on lexical handles.)
>> I want to capture the output of these subs in a Perl string instead.
>[ snip - almost correct munging of *STDOUT using IO::Scalar ]
I still don't understand why it didn't work... If anyone cares to
throw me another cluebrick, here's the failed strategy again, for
easy reference:
use IO::Scalar;
my $string;
*HOLD = *STDOUT;
$STRING = IO::Scalar->new(\$string);
*STDOUT = *STRING;
foo(); # prints to STDOUT; output should be in $string ???
*STDOUT = *HOLD;
TIA,
jill
--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.
------------------------------
Date: Fri, 07 May 2004 10:48:46 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: List questions on form
Message-Id: <hQJmc.36258$TD4.6147516@attbi_s01>
Roger wrote:
> "Chief S." <chiefS@edu.edu> wrote in message
>
>>Why 17? Are you understanding splice() correctly?
>
> on account of ...
> # remove elements 1 and 2
> # replace with new values
> splice (@rainbow, 1, 2, "yellow", "orange");
That's an unfortunately ambiguous example.
Maybe this will show where you are misunderstanding it.
# remove 2 elements; 7 and 8
# replace with new values
splice (@rainbow, 7, 2, "yellow", "orange");
-Joe
------------------------------
Date: 7 May 2004 11:19:55 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: newbie; appending multiple files
Message-Id: <c7frcr$gv$1@mamenchi.zrz.TU-Berlin.DE>
A. Sinan Unur <1usa@llenroc.ude> wrote in comp.lang.perl.misc:
> sjwallacew@hotmail.com (Steve) wrote in news:eb6d00cc.0405061257.5b167226
> @posting.google.com:
>
> > Hello,
> >
> > I have files 1.txt, 2.txt, 3.txt.......n.txt. Each file contains a
> > single line of text. I need each line of text from all these source
> > files to be appended into a single destination file called bulk.txt.
> >
> > I have found some marginally helpful stuff in the manpages and faqs
> > but not quite what I need. Any pointers are appreciated.
>
> And your Perl question is ...?
>
> cat ?.txt > bulk.txt
>
> ought to do it.
Dunno about the Perl question, but the Perl answer is
perl -e'print while <>' ?.txt
Anno
------------------------------
Date: Fri, 7 May 2004 12:55:25 +0000 (UTC)
From: Ian Wilson <scobloke2@infotop.co.uk>
Subject: Re: newbie; appending multiple files
Message-Id: <c7g0vr$qrr$1@hercules.btinternet.com>
Anno Siegel wrote:
> A. Sinan Unur <1usa@llenroc.ude> wrote in comp.lang.perl.misc:
>
>>sjwallacew@hotmail.com (Steve) wrote in news:eb6d00cc.0405061257.5b167226
>>@posting.google.com:
>>
>>
>>>Hello,
>>>
>>>I have files 1.txt, 2.txt, 3.txt.......n.txt. Each file contains a
>>>single line of text. I need each line of text from all these source
>>>files to be appended into a single destination file called bulk.txt.
>>>
>>>I have found some marginally helpful stuff in the manpages and faqs
>>>but not quite what I need. Any pointers are appreciated.
>>
>>And your Perl question is ...?
>>
>>cat ?.txt > bulk.txt
>>
>>ought to do it.
>
>
> Dunno about the Perl question, but the Perl answer is
>
> perl -e'print while <>' ?.txt
>
> Anno
And 'print while <>' is the -p option ...
$ echo xxx > bulk.txt
$ echo aaa > 1.txt
$ echo bbb > 2.txt
$ echo ccc > 3.txt
$ perl -pe "" ?.txt >> bulk.txt
$ cat bulk.txt
xxx
aaa
bbb
ccc
I suppose this *might* be useful on a platform that has perl but lacks
cat or a decent shell?
------------------------------
Date: Fri, 07 May 2004 14:38:13 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: newbie; appending multiple files
Message-Id: <Xns94E26C347109Bdkwwashere@216.168.3.30>
Ian Wilson <scobloke2@infotop.co.uk> wrote:
> And 'print while <>' is the -p option ...
>
> $ echo xxx > bulk.txt
> $ echo aaa > 1.txt
> $ echo bbb > 2.txt
> $ echo ccc > 3.txt
> $ perl -pe "" ?.txt >> bulk.txt
> $ cat bulk.txt
> xxx
> aaa
> bbb
> ccc
>
> I suppose this *might* be useful on a platform that has perl but
> lacks cat or a decent shell?
Sure, but even in later versions of DOS (at least v3+, IIRC) you
could use
C:\> for %f in (?.txt) do type %f >> bulk.txt
and under win2k at least, 'type ?.txt > bulk.txt' seems to work as
you might expect. The DOS shell is not completely brain-dead, just
mentally retarded. :-)
Not that this has anything remotely to do with Perl...
------------------------------
Date: Fri, 07 May 2004 16:33:48 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Perl Calls the second
Message-Id: <ef7n909o0g2qlpq2vccaahp8alem4sfdj1@4ax.com>
On Thu, 6 May 2004 21:59:31 +0000 (UTC), mgjv@comdyn.com.au (Martien
Verbruggen) wrote:
>> Hello !!!
>> Well, I'm still searcjing for help about the Perl-calls-topic from a
>> HTML-document without having to press a button.
>> AbiGails answer was quite french, I didn´t understood...
[snip]
>Maybe you should go to one of the comp.infosystems.www.* groups, and
>ask there. Your question has absolutely no relevance to perl.
And even less so to mathematics! ;-)
Michele
--
> Comments should say _why_ something is being done.
Oh? My comments always say what _really_ should have happened. :)
- Tore Aursand on comp.lang.perl.misc
------------------------------
Date: Fri, 07 May 2004 12:44:29 GMT
From: peter@PSDT.com (Peter Scott)
Subject: Re: Please Recommend A Good Perl Book.
Message-Id: <NwLmc.375641$Pk3.8789@pd7tw1no>
In article <c7dn26$v3g$1@plover.com>,
mjd@plover.com (Mark Jason Dominus) writes:
>>[*] http://perl.plover.com/book/
>
>The book now has a title. It will be called "Higher Order Perl".
Excellent. HOP is catchier than PATH.
:-)
--
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/
------------------------------
Date: 7 May 2004 06:39:28 -0700
From: gwshieh@hotmail.com (William)
Subject: read is blocking on win32 CGI
Message-Id: <5f261ebe.0405070539.4fada625@posting.google.com>
Hi,
I'm working on a CGI which invoked by IIS6.0. However, in the cgi perl
script, i have a read which should read all POST content. I found 2
things interesting
1. the read becomes blocking if I want to read more than it was
submitted
2. the $ENV{'CONTENT_LENGTH'} is not reliable. In my case, the content
is 16330 bytes but it tells me 16600.
Anyone see this behavior? How to do non-blocking read in CGI?
thanks,
william
------------------------------
Date: 7 May 2004 05:03:54 -0700
From: alythh@netscape.net (Alythh)
Subject: regex utility
Message-Id: <6a25ba72.0405070403.360b150a@posting.google.com>
I am frankly tired to debug my regexes by running the whole program
all the times.
I found some interesting sites where I can form-input a string of text
and a regex - and see if it matches.
Do you know of any utility to be run locally, possibly able evn to
report - it match is OK - the $1, $2, ... values?
Thanks!
Alessandro Magni
------------------------------
Date: Fri, 07 May 2004 13:34:17 +0100
From: "Richard Gration" <richard@zync.co.uk>
Subject: Re: regex utility
Message-Id: <c7fvo2$79d$1@news.freedom2surf.net>
In article <6a25ba72.0405070403.360b150a@posting.google.com>, "Alythh"
<alythh@netscape.net> wrote:
> I am frankly tired to debug my regexes by running the whole program all
> the times.
> I found some interesting sites where I can form-input a string of text
> and a regex - and see if it matches.
> Do you know of any utility to be run locally, possibly able evn to
> report - it match is OK - the $1, $2, ... values? Thanks!
> Alessandro Magni
Well, I thought for a bit and wrote this ...
#!/usr/bin/perl
use strict;
use warnings;
unless ($#ARGV == 1) {
usage();
exit;
}
my $matched = $ARGV[1] =~ m($ARGV[0]);
if ($matched) {
print "Your text matches the pattern\n";
defined $1 and print "match 1: $1\n";
defined $2 and print "match 1: $2\n";
defined $3 and print "match 1: $3\n";
defined $4 and print "match 1: $4\n";
defined $5 and print "match 1: $5\n";
defined $6 and print "match 1: $6\n";
defined $7 and print "match 1: $7\n";
defined $8 and print "match 1: $8\n";
defined $9 and print "match 1: $9\n";
}
sub usage {
print <<EOUSAGE
Usage:
$0 <pattern> <text>
EOUSAGE
}
------------------------------
Date: Fri, 07 May 2004 13:35:49 +0100
From: Mark Clements <mark.clements@kcl.ac.uk>
Subject: Re: regex utility
Message-Id: <409b8296$1@news.kcl.ac.uk>
Alythh wrote:
> I am frankly tired to debug my regexes by running the whole program
> all the times.
> I found some interesting sites where I can form-input a string of text
> and a regex - and see if it matches.
>
> Do you know of any utility to be run locally, possibly able evn to
> report - it match is OK - the $1, $2, ... values?
have you tried the command line?
eg
perl -nle 'print $1 if /(\w+)/'
perldoc perlrun
------------------------------
Date: Fri, 07 May 2004 13:40:34 +0100
From: "Richard Gration" <richard@zync.co.uk>
Subject: Re: regex utility
Message-Id: <c7g03r$7ec$1@news.freedom2surf.net>
Spot the deliberate mistake ...
defined $2 and print "match 1: $2\n";
^
should be 2 of course, etc.
------------------------------
Date: Fri, 07 May 2004 15:44:33 +0200
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: regex utility
Message-Id: <409b942b.0@juno.wiesbaden.netsurf.de>
Richard Gration wrote:
> In article <6a25ba72.0405070403.360b150a@posting.google.com>, "Alythh"
> <alythh@netscape.net> wrote:
>
>
>
>>I am frankly tired to debug my regexes by running the whole program all
>>the times.
>>I found some interesting sites where I can form-input a string of text
>>and a regex - and see if it matches.
>>Do you know of any utility to be run locally, possibly able evn to
>>report - it match is OK - the $1, $2, ... values? Thanks!
>>Alessandro Magni
>
>
> Well, I thought for a bit and wrote this ...
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> unless ($#ARGV == 1) {
> usage();
> exit;
> }
>
> my $matched = $ARGV[1] =~ m($ARGV[0]);
>
> if ($matched) {
> print "Your text matches the pattern\n";
> defined $1 and print "match 1: $1\n";
> defined $2 and print "match 1: $2\n";
> defined $3 and print "match 1: $3\n";
> defined $4 and print "match 1: $4\n";
> defined $5 and print "match 1: $5\n";
> defined $6 and print "match 1: $6\n";
> defined $7 and print "match 1: $7\n";
> defined $8 and print "match 1: $8\n";
> defined $9 and print "match 1: $9\n";
> }
or simpler:
my @matches = $ARGV[1] =~ m($ARGV[0]);
my $i = 1;
print "match $i++: $_\n" for map {
defined($_) ? $_ : 'undef'
} @matches;
print "no matches\n" unless @matches;
Thomas
--
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..
------------------------------
Date: Fri, 07 May 2004 16:02:47 +0200
From: Christian Winter <thepoet_nospam@arcor.de>
Subject: Re: regex utility
Message-Id: <409b9708$0$10895$9b4e6d93@newsread2.arcor-online.net>
Thomas Kratz schrieb:
> or simpler:
>
> my @matches = $ARGV[1] =~ m($ARGV[0]);
> my $i = 1;
> print "match $i++: $_\n" for map {
> defined($_) ? $_ : 'undef'
> } @matches;
> print "no matches\n" unless @matches;
or even:
my $c;
print "match ",++$c,": $_$/" for($ARGV[1]=~m($ARGV[0])g);
-Christian
------------------------------
Date: Fri, 07 May 2004 16:16:36 +0200
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: regex utility
Message-Id: <409b9baf.0@juno.wiesbaden.netsurf.de>
Thomas Kratz wrote:
>
> or simpler:
>
> my @matches = $ARGV[1] =~ m($ARGV[0]);
> my $i = 1;
> print "match $i++: $_\n" for map {
oops, make that:
print "match ", $i++, ": $_\n" for map {
> defined($_) ? $_ : 'undef'
> } @matches;
> print "no matches\n" unless @matches;
Thomas
--
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..
------------------------------
Date: Fri, 07 May 2004 16:33:45 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: regex utility
Message-Id: <o56n90plj3bk45bk02b931qe059l8r0c5a@4ax.com>
On Fri, 07 May 2004 13:34:17 +0100, "Richard Gration"
<richard@zync.co.uk> wrote:
>> I am frankly tired to debug my regexes by running the whole program all
>> the times.
>> I found some interesting sites where I can form-input a string of text
>> and a regex - and see if it matches.
>> Do you know of any utility to be run locally, possibly able evn to
>> report - it match is OK - the $1, $2, ... values? Thanks!
>> Alessandro Magni
>
>Well, I thought for a bit and wrote this ...
[snip]
>my $matched = $ARGV[1] =~ m($ARGV[0]);
Also, see 'perldoc -q pass/return' under the heading "Passing
Regexes", especially the bit about using an exception-trapping eval().
Michele
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__
------------------------------
Date: Fri, 07 May 2004 16:53:48 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: regex utility
Message-Id: <gv7n909u2cqc42mt8el5hg9gb7i8k0subg@4ax.com>
On Fri, 07 May 2004 13:34:17 +0100, "Richard Gration"
<richard@zync.co.uk> wrote:
>#!/usr/bin/perl
>
>use strict;
>use warnings;
>
>unless ($#ARGV == 1) {
Huh?!? Not that this is technically incorrect, but is there any good
reason for not using '@ARGV == 2' instead? (Which IMHO conveys more
naturally the idea that the number of required parameters is two.)
> usage();
> exit;
>}
[...]
>sub usage {
> print <<EOUSAGE
>Usage:
>
> $0 <pattern> <text>
>EOUSAGE
>}
Also, I understand that you're being more verbose than would seem
natural for this simple because you're probably used to write "bigger"
applications, but since it would be better, still IMHO, to die()
anyway, then I'd write:
die <<"EOUSAGE" unless @ARGV == 2;
Usage:
$0 <pattern> <text>
EOUSAGE
Also, you can still adopt a "hybrid" approach writing a usage() sub
to *return* the usage string or "save" that to a $usage scalar in case
you need it in multiple places of your script, e.g.:
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
sub usage(); # Or (maybe better!) 'use constant'...
my %opts;
getopts 'h', \%opts;
print usage and exit if $opts{h};
die usage unless @ARGV == 2;
# ACTUAL PROGRAM...
sub usage () {
<<"EOUSAGE"
Usage: $0 [options] <pattern> <text>
Currently the only option is -h, that
will print this help and exit immediately.
EOUSAGE
}
__END__
BTW: I didn't use it here, but Getopt::Std has its own --help managing
facility, that often come handy and can complement an approach like
that above...
Michele
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"
------------------------------
Date: Fri, 7 May 2004 11:06:21 +0100
From: "Raj" <raj.kothary@thus.net>
Subject: Re: Trapping warnings
Message-Id: <c7fn6k$dif$1$8302bc10@news.demon.co.uk>
"GM" <GM@foo.bar> wrote in message news:1Gumc.43088$I%1.2791545@attbi_s51...
> A. Sinan Unur wrote:
> > "Raj" <raj.kothary@thus.net> wrote in
> > news:c7dqde$fbs$1$830fa7a5@news.demon.co.uk:
> >
> >
> >>Hi,
> >>
> >>I am using some code to talk to a third party server via an HTTPS GET.
> >>
> >>The problem I have is that when the server is unavailable, the object
> >>method thrown an error of the form:
> >>
> >>"(Use of uninitialized value in split at
> >>/opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
> >>
> >>I need to be able to suppress these warnings, but trap them so that I
> >>can report them back via my code gracefully.
> >>
> >>I know I can trap die using eval() but what can I do to deal with the
> >>above warning?
> >
> >
> > Well, how about not using split if there is nothing to split?
> >
>
> Ditto!
>
> You should be looking at what you get back from the server before trying
> to do anything with it.
But it is someone else's PM that I am using:
my $e = GL::eo::new (
host=>"$host", key=>"$key",
) || die("500");
my $path = $cfg->{GL}->{Path};
my ($p,$r,$h) = $e->do_request('GET', $path, undef); # this is the method
in which the split occurs
I cannot change the code for the do_request() method. Can I stop it from
throwing this error?
Regards (and thanks)
Raj
------------------------------
Date: Fri, 07 May 2004 13:23:50 +0200
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: Trapping warnings
Message-Id: <409b7330.0@juno.wiesbaden.netsurf.de>
Raj wrote:
> "GM" <GM@foo.bar> wrote in message news:1Gumc.43088$I%1.2791545@attbi_s51...
>
>>A. Sinan Unur wrote:
>>
>>>"Raj" <raj.kothary@thus.net> wrote in
>>>news:c7dqde$fbs$1$830fa7a5@news.demon.co.uk:
>>>
>>>
>>>
>>>>Hi,
>>>>
>>>>I am using some code to talk to a third party server via an HTTPS GET.
>>>>
>>>>The problem I have is that when the server is unavailable, the object
>>>>method thrown an error of the form:
>>>>
>>>>"(Use of uninitialized value in split at
>>>>/opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
>>>>
>>>>I need to be able to suppress these warnings, but trap them so that I
>>>>can report them back via my code gracefully.
>>>>
>>>>I know I can trap die using eval() but what can I do to deal with the
>>>>above warning?
>>>
>>>
>>>Well, how about not using split if there is nothing to split?
>>>
>>
>>Ditto!
>>
>>You should be looking at what you get back from the server before trying
>>to do anything with it.
>
>
> But it is someone else's PM that I am using:
>
> my $e = GL::eo::new (
> host=>"$host", key=>"$key",
> ) || die("500");
>
> my $path = $cfg->{GL}->{Path};
>
> my ($p,$r,$h) = $e->do_request('GET', $path, undef); # this is the method
> in which the split occurs
>
> I cannot change the code for the do_request() method. Can I stop it from
> throwing this error?
Do you use
#!perl -w
or
use warnings;
in your script?
IIRC the former will turn on warnings for all used modules too, whereas
the latter will only do so in the lexical scope it is used in.
If it happens with 'use warnings', then either
my ($p,$r,$h);
{
no warnings qw/uninitialized/;
($p,$r,$h) = $e->do_request('GET', $path, undef);
}
or trapping the __WARN__ signal with
local SIG{__WARN__} = sub {
...
do something with the warning message in $_[0]
...
}
my($p,$r,$h) = $e->do_request('GET', $path, undef);
will perhaps do what you want.
see also 'perldoc -f warn' and the section on %SIG in 'perldoc perlvar'
Thomas
--
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..
------------------------------
Date: 07 May 2004 12:51:55 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Trapping warnings
Message-Id: <u9llk4ihes.fsf@wcl-l.bham.ac.uk>
"Raj" <raj.kothary@thus.net> writes:
> I am using some code to talk to a third party server via an HTTPS GET.
>
> The problem I have is that when the server is unavailable, the object method
> thrown an error of the form:
>
> "(Use of uninitialized value in split at
> /opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
>
> I need to be able to suppress these warnings, but trap them so that I can
> report them back via my code gracefully.
As others have pointed out, it would seem more logical to cope
gracefully with the situation that lead to the warnings in the first
place instead.
> I know I can trap die using eval() but what can I do to deal with the above
> warning?
1) You can promote warnings to errors and then trap them as errors.
(See perldoc perllexwarn).
2) You can redirect STDERR to a file (or string). This is fiddly and
IIRC a tied STDERR didn't capture warnings until very recently.
3) You can define a warning handler by assigning $SIG{__WARN__}.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 7 May 2004 12:19:55 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Trapping warnings
Message-Id: <c7futb$3bg$1@mamenchi.zrz.TU-Berlin.DE>
Brian McCauley <nobull@mail.com> wrote in comp.lang.perl.misc:
> "Raj" <raj.kothary@thus.net> writes:
>
> > I am using some code to talk to a third party server via an HTTPS GET.
> >
> > The problem I have is that when the server is unavailable, the object method
> > thrown an error of the form:
> >
> > "(Use of uninitialized value in split at
> > /opt/perl5/lib/site_perl/5.6.1/sun4-solaris-64int/eo.pm line 151.)"
> >
> > I need to be able to suppress these warnings, but trap them so that I can
> > report them back via my code gracefully.
>
> As others have pointed out, it would seem more logical to cope
> gracefully with the situation that lead to the warnings in the first
> place instead.
>
> > I know I can trap die using eval() but what can I do to deal with the above
> > warning?
>
> 1) You can promote warnings to errors and then trap them as errors.
> (See perldoc perllexwarn).
You can do that, but it must happen in the lexical scope of the
code that issues the warning. Since the function that issues the
warning is from a module not under OP's control, that won't help here.
> 2) You can redirect STDERR to a file (or string). This is fiddly and
> IIRC a tied STDERR didn't capture warnings until very recently.
Up until 5.8.0 it caught some and didn't catch others in rather
unpredictable ways. In 5.8.1 and better it catches them all. That
is indeed very recently.
> 3) You can define a warning handler by assigning $SIG{__WARN__}.
I'm afraid that is the way to go. Something like
{
local $SIG{ __WARN__} = sub {
return if $_[ 0] =~ /uninitialized/;
warn @_;
};
# call the warning method
}
ought to do it.
Anno
------------------------------
Date: Fri, 07 May 2004 16:35:40 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Using $1, $2 ... but don't know in which order
Message-Id: <pan.2004.05.07.14.35.26.101754@aursand.no>
Hi!
I have a number of old Perl scripts doing fairly the same job; They're
connecting to a (local) web server and retrieves something from it (plain
text, that is).
As I said, each script is doing the same job; Parsing the text, and
writing some "meaningful" data to a MySQL database.
Everything works just great, but I want to put everything these scripts do
into one script, as most of what they do is identical. I have created a
list of hashes which describe each resource I try to parse;
my %sources = (
{
'title' => 'Server #1',
'href' => 'http://.../1/',
'regexp' => '...',
},
{
'title' => 'Server #2',
'href' => 'http://.../1/',
'regexp' => '...',
},
# etc...
);
Iterating through these resources;
foreach ( @sources ) {
my $text = get( $_->{'href'} ); # LWP::Simple
if ( defined $text && length $text ) {
while ( $text =~ m,$_->{regexp},sig ) {
my $foo = $1;
my $bar = $2;
# etc...
}
}
}
This works as expected for the majority of the files I download, but for
some I need to - hmm - match in a different order. Example: For most of
the sites it is suitable to set $foo = $1, but for some $foo should be $2
instead (or $3, whatever).
How should I deal with this in a sexy way? :)
--
Tore Aursand <tore@aursand.no>
"Writing is a lot like sex. At first you do it because you like it.
Then you find yourself doing it for a few close friends and people you
like. But if you're any good at all, you end up doing it for money."
(Unknown)
------------------------------
Date: Fri, 7 May 2004 09:04:41 -0400
From: "Domenico Discepola" <domenico_discepola@quadrachemicals.com>
Subject: Re: win32::ole and excel VBA macro conversion: SmallScroll
Message-Id: <gOLmc.37443$kc2.555611@nnrp1.uunet.ca>
"Bob Walton" <invalid-email@rochester.rr.com> wrote in message
news:409AFF6D.9050208@rochester.rr.com...
> Domenico Discepola wrote:
>
> > "Bob Walton" <invalid-email@rochester.rr.com> wrote in message
> > news:4099940B.10103@rochester.rr.com...
> >
> >>Domenico Discepola wrote:
> ...
> > Thanks Bob - it worked like a charm. Now, here's my next question... I
> > find it very difficult to translate VBA into Perl. The existing
>
>
> The basic steps are outlined in the docs for Win32::OLE. The thing I
> used for your case is the fourth line of the synopsis, for example -- I
> don't see how one could get clearer than that. I have always found what
> I needed to know about Win32::OLE in the Win32::OLE docs. If you study
> the docs and follow them to the letter, you will be *way* better off
> than your admitted "trial and error approach". Also, you will find the
> "OLE Browser" in Excel to be a source of information found nowhere else.
>
> If you want to "drill down into an object's methods and properties", you
> have a couple of choices. One is the Perl debugger. Another is the
> Data::Dumper module. Another is to peruse the module's source code.
> And another is to read the docs. Guess which one is easiest.
>
If I may play devil's advocate... Here's an example of what I consider to
be confusing documentation. On Activestate's website,
"How do I convert a VBA macro to Perl?
If you record a macro in Microsoft Office, this can often be translated
directly into Perl. In Visual Basic for Applications (VBA) the syntax is
like this:
object.method(argument).property = value
In Perl this becomes
object->method(argument)->{property} = value;
"
So, as it was suggested, I used Excel's macro recording feature to record
the following VBA code:
ActiveWindow.SmallScroll ToRight:=-3
So, using the documentation mentioned above, I translated this into:
$excel->ActiveWindow->SmallScroll->{ToRight} = -3;
which didn't work. This seemingly simple example causes newbie programmers
much grief. I saw no mention of adding parenthesis (as you provided in your
solution): $excel->ActiveWindow->SmallScroll({ToRight=>-3});
------------------------------
Date: Fri, 7 May 2004 09:21:13 -0400
From: "Domenico Discepola" <domenico_discepola@quadrachemicals.com>
Subject: Re: win32::ole and excel VBA macro conversion: SmallScroll
Message-Id: <N1Mmc.37446$kc2.555732@nnrp1.uunet.ca>
> I wholeheartedly agree, a good book on the subject would have saved me
weeks
> of experimenting and tinkering.
>
> In february I got thrown into the deep end of the pool with Dave Roth's
win32
> book and Progamming Perl. The assignment was: take these textfiles with
data
> and make us some nice tables and charts in ms excel.
>
> And having no experience with eiter Perl or MSExcel or COM objects I found
it
> quite challenging.
>
> And now they want a gui so I'm having fun with Perl/Tk.
>
> Peter
Finally, someone who agrees ;-)
I had similar "assignments"... The "worst one" was to take csv files and
generate Excel workbooks with pivot tables (try figuring that one out).
Roth's book was very helpful. I would very much like for a guru to write a
book on using Perl with Windows applications (perhaps with a section for
each popular Microsoft Office application - Word, Excel, Outlook, etc. ).
The reason I am stressing this is that my business users are constantly on
the lookout for ways to improve efficiency - and I don't think I am alone.
For example, if they could automatically receive a monthly Excel file with
pre-formatted reports/pivot tables, it would save them lots of time. If our
executives could receive automatically generated/formatted powerpoint files,
they wouldn't have to waste their time creating those. Why would I choose
to do this in Perl (as opposed to other programming languages)? Because
Perl is the best ;-) Perl provides a lot of functionality with very little
code (especially thanks to those regexs and cpan modules).
By the way, I was also asked to create a gui for one of my applications. I
used vb.net for that. It was nothing more than a front-end interface to my
perl programs, with some code for reading/writing parameters to/from the
registry.
Dom
------------------------------
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 6536
***************************************