[10009] in Perl-Users-Digest
Perl-Users Digest, Issue: 3602 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Sep 1 01:04:37 1998
Date: Mon, 31 Aug 98 22:00:18 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 31 Aug 1998 Volume: 8 Number: 3602
Today's topics:
Re: (no subject) (Martien Verbruggen)
Re: Better Regular Expressions (was: Re: Imagine... a n <uri@sysarch.com>
Re: Better Regular Expressions (was: Re: Imagine... a n <uri@sysarch.com>
Re: Better Regular Expressions (was: Re: Imagine... a n <rootbeer@teleport.com>
Re: Better Regular Expressions (was: Re: Imagine... a n <mee@mine.com>
Re: Better Regular Expressions (was: Re: Imagine... a n <metcher@spider.herston.uq.edu.au>
Re: Better Regular Expressions (was: Re: Imagine... a n <mee@mine.com>
Re: Better Regular Expressions (was: Re: Imagine... a n <mds-resource@mediaone.net>
Re: Compiling XS output fails on SGI with IDO 7.1 <derikson@msn.fullfeed.com>
Re: fast fork problem (was: Re: session id) (Mark-Jason Dominus)
Finding the FAQ (was: Re: No switch statement in Perl?? <rootbeer@teleport.com>
Re: Finding the FAQ (was: Re: No switch statement in Pe <ljz@asfast.com>
FMTEYEWTK on Switch Statements in Perl (was: No switch <tchrist@mox.perl.com>
Re: Funny, that: $#myarray <cabney@cyberpass.net>
Help on running perl scripts on MS-DOS (Win NT4 actuall (Eric Dew)
Re: Help on running perl scripts on MS-DOS (Win NT4 act (Bob Trieger)
Re: Help on running perl scripts on MS-DOS (Win NT4 act (Eric Dew)
Re: No switch statement in Perl?? (Adam Spragg)
Re: Perl Cookbook, does anyone have it? (Mark-Jason Dominus)
Re: Perl Cookbook, does anyone have it? <uri@sysarch.com>
Re: Y2K Date Support <jt@eatel.net>
Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 01 Sep 1998 03:59:47 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: (no subject)
Message-Id: <TiKG1.1135$Gu1.113109@nsw.nnrp.telstra.net>
[Subject: Re: (no subject)]
[You really should put a Subject in your posts]
Please read the following information on how to choose a good subject
line:
http://www.perl.com/CPAN/authors/Dean_Roehrich/subjects.post
In article <6sdq7v$f62$1@sci.cpd.uniovi.es>,
Oscar Fernandez Sierra <oscar@charpy.etsiig.uniovi.es> writes:
> Can anybody show any working example of create and save a file
> from Perl using Win32::OLE package. I can do some operations
> with excel and word, but I'm unable to save a file.
I don't need no Win32::OLE package
#!/usr/local/bin/perl -w
use strict;
my $data;
my $fn = '/path/to/some/file';
# Put all the stuff in $data that you need to go into the file
open(FF, ">$fn") || die "Cannot open $fn for write: $!";
print FF $data;
close(FF);
If you need something with the Win32::OLE package, you will have to
read the documentation. If you need to get word or excel to save a
file, then you will need to find out how to do that with OLE. Not
exactly a perl problem.
> Please, answer by email too. I don't use to read news.
What? But you do _ask_ questions on Usenet, don't you? Post here, read
here.
Martien
--
Martien Verbruggen |
Webmaster www.tradingpost.com.au | "In a world without fences,
Commercial Dynamics Pty. Ltd. | who needs Gates?"
NSW, Australia |
------------------------------
Date: 01 Sep 1998 00:12:46 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Better Regular Expressions (was: Re: Imagine... a non-greedy world!)
Message-Id: <x790k431z5.fsf@sysarch.com>
>>>>> "TP" == Tom Phoenix <rootbeer@teleport.com> writes:
TP> On Mon, 31 Aug 1998, Mee wrote:
>> It's elementary, Mr. Phoenix.
>> To find out what kind of patterns are commonly used today
>> go to CPAN and look (or grep) through widely used modules.
TP> You aren't answering my question. You aren't being specific about what you
TP> want Perl's patterns to look like. I've tried to be receptive to your
TP> ideas, but you don't have any. You are being a troll. Go away.
boy, someone (or thing) got tom p. pissed!! i am truly amazed at the
depth of stupidity of mee. he has never responded to my replies. yet he
seems to read this group. will he EVER post some actual text/code or
anything we can look at? did he read ilya's post about rolling your own
regex engine AND syntax being supported. will he use that feature and
shut up? stay tuned to this group, same perl-time, same perl-channel,
same stupid mee!
mee, you are doing us all a favor by allowing us to flame you. it makes
us appreciate the lesser twits who fill this group with noise. they are
perl gurus compared with you. also you are such an easy target it is
fun and you bring out my venous side. then i won't have to be as nasty
to others. you do us a fine service.
uri
--
Uri Guttman ----------------- SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire ---------------------- Perl, Internet, UNIX Consulting
uri@sysarch.com ------------------------------------ http://www.sysarch.com
The Best Search Engine on the Net ------------- http://www.northernlight.com
------------------------------
Date: 31 Aug 1998 23:14:17 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Better Regular Expressions (was: Re: Imagine... a non-greedy world!)
Message-Id: <x7btp034ol.fsf@sysarch.com>
>>>>> "IZ" == Ilya Zakharevich <ilya@math.ohio-state.edu> writes:
IZ> Hmm, probably neither do I. Perl's RE can parse recursive structures,
IZ> and, starting from 10 days ago, the syntax is not that bad.
>> he juat wants what he wants without concern for how it gets done.
well publish one that can strip HTML as that seems to be the most FAQ re re!
tom c. would love to se that so he can change his standard flame about
that FAQ to "it can be done, bother ilya about it!"
:-)
uri
--
Uri Guttman ----------------- SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire ---------------------- Perl, Internet, UNIX Consulting
uri@sysarch.com ------------------------------------ http://www.sysarch.com
The Best Search Engine on the Net ------------- http://www.northernlight.com
------------------------------
Date: Tue, 01 Sep 1998 03:19:08 GMT
From: Tom Phoenix <rootbeer@teleport.com>
Subject: Re: Better Regular Expressions (was: Re: Imagine... a non-greedy world!)
Message-Id: <Pine.GSO.4.02A.9808312017430.24074-100000@user2.teleport.com>
On Mon, 31 Aug 1998, Mee wrote:
> It's elementary, Mr. Phoenix.
> To find out what kind of patterns are commonly used today
> go to CPAN and look (or grep) through widely used modules.
You aren't answering my question. You aren't being specific about what you
want Perl's patterns to look like. I've tried to be receptive to your
ideas, but you don't have any. You are being a troll. Go away.
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: Mon, 31 Aug 1998 21:01:22 -0700
From: Mee <mee@mine.com>
Subject: Re: Better Regular Expressions (was: Re: Imagine... a non-greedy world!)
Message-Id: <35EB7192.8D754E43@mine.com>
Tom,
If I am not mistaken, "You are a troll" around here
seems to be a standard expression for admitting being
defeated.
Thanks for trying,
Mee
------------------------------
Date: Tue, 01 Sep 1998 14:41:51 +1000
From: Jaime Metcher <metcher@spider.herston.uq.edu.au>
Subject: Re: Better Regular Expressions (was: Re: Imagine... a non-greedy world!)
Message-Id: <35EB7B0F.365FDAD4@spider.herston.uq.edu.au>
It's a 'bot. Not a bad one, but it could do with some work. I wonder
if it's written in perl?
--
Jaime Metcher
Mee wrote:
>
> Tom,
>
> If I am not mistaken, "You are a troll" around here
> seems to be a standard expression for admitting being
> defeated.
>
> Thanks for trying,
>
> Mee
------------------------------
Date: Mon, 31 Aug 1998 19:54:18 -0700
From: Mee <mee@mine.com>
Subject: Re: Better Regular Expressions (was: Re: Imagine... a non-greedy world!)
Message-Id: <35EB61DA.1E04FCF9@mine.com>
Tom,
Since you insist:
It's elementary, Mr. Phoenix.
To find out what kind of patterns are commonly used today
go to CPAN and look (or grep) through widely used modules.
Mee
------------------------------
Date: Mon, 31 Aug 1998 22:59:28 -0500
From: "Michael D. Schleif" <mds-resource@mediaone.net>
Subject: Re: Better Regular Expressions (was: Re: Imagine... a non-greedy world!)
Message-Id: <35EB7120.38DCC09B@mediaone.net>
Usually, I am on the side of the ignorant, proselitizing for compassion
for those who want to program Perl; but, who don't yet know what they
are doing.
However, in the case of dear Mee, the _only_ thing explicit about his
postings is his _dislike_ and _disdain_ for the Perl language. He has
refused, after several patient urgings, to post even _one_ explicit
example that cannot be readily regex'd in Perl. Dear Mee implies that
Perl has taken a regex road of its own, ignoring regex linguistics of
other languages; but, again, has _not_ cited one explicit example. I am
no expert; but, I find that Perl regex's fit sensibly into regex
evolution and historical dialectic -- where regex goes from here, I can
only guess.
Nevertheless, this is an interesting thread, if for no other reason
than, so many Perl afficionados have gone to great lengths to divine
dear Mee's unfathomable objections and, in so doing, a great deal of
passion and abstract theorizing, critical to successful problemsolving
rubrics, have passed over the wire. I look forward to many more
empassioned encounters on this newsgroup . . .
Perhaps, English is a langauge dear Mee ought to master, prior to any
other . . .
Mee wrote:
>
> To find out what kind of patterns are commonly used today
> go to CPAN and look (or grep) through widely used modules.
--
Best Regards,
mds
mds resource
888.250.3987
"Dare to fix things before they break . . . "
"Our capacity for understanding is inversely proportional to how much we
think we know. The more I know, the more I know I don't know . . . "
------------------------------
Date: Mon, 31 Aug 1998 22:45:18 -0500
From: Dave Erickson <derikson@msn.fullfeed.com>
To: daniel@vis.inf.ethz.ch
Subject: Re: Compiling XS output fails on SGI with IDO 7.1
Message-Id: <35EB6DCD.9E206023@msn.fullfeed.com>
Hi,
I got errors similar to this when the compiler dev systems didn't
match the compiler eoe software. It was about a year ago so I don't
remember if it was exactly the message your getting.
dave
------------------------------
Date: 31 Aug 1998 19:10:10 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: fast fork problem (was: Re: session id)
Message-Id: <6sfagi$sio$1@monet.op.net>
In article <6sedvc$qrj$5@info.uah.edu>, Greg Bacon <gbacon@cs.uah.edu> wrote:
>I've seen lots of programs that make the assumption that
>C<time . ".$$"> will be unique.
It doesn't seem to problematic to me.
> I remember Jarkko writing on p5p that
>he'd like to see the machine that is capable of 64 Kforks/second.
Well, it depends on how big your pids are. On many systems, they only
go up to 2**15.
I was also concerned about the idea of a system which might reuse old
pids without waiting for them to cycle around. You never know what
the system is going to do to stiff you, and that sounds like just the
sort of thing that Bill's folks would come up with. ``Windows 00, now
with more efficient reuse of process ID numbers!''
But like I said, it doesn't seem to problematic to me.
> is there a better way to generate unique IDs?
If you're really worried, you can generate a very random random number
and append it. Don't use the pseudo-random generator, since that
typically depends only on the time, the pid, or both. Or, for network
servers, append the client IP address.
------------------------------
Date: Tue, 01 Sep 1998 03:43:20 GMT
From: Tom Phoenix <rootbeer@teleport.com>
Subject: Finding the FAQ (was: Re: No switch statement in Perl??)
Message-Id: <Pine.GSO.4.02A.9808312019420.24074-100000@user2.teleport.com>
On 1 Sep 1998, Adam Spragg wrote:
> It's helpful in a reply like this to also include where the FAQ can be
> found
You're right that it might be helpful. But my thought is: Is there any
reasonable way in which someone may look for the Perl FAQ and not find it?
You can find it on your own system, since it comes with Perl. You can find
it on the web on CPAN and other sites. You can search for it with any of
the web's popular search engines. It's available via FTP or email. You can
find pointers to it frequently posted to this newsgroup and to
comp.lang.perl.announce. It's listed in the index to the Camel book. You
can learn where it is in the helpful mail you'll get back by posting for
the first time to this newsgroup or to comp.lang.perl.moderated. There's
an infobot on IRC's #perl which will tell you. Your local expert probably
knows where it is. Any of the regular readers of this newsgroup knows
where it is. Wally, the programmer in the next cubicle, probably knows
where it is.
If someone reading my postings _still_ can't find the FAQ via in any of
these ways, he or she should write to me. That's one of the reasons my
email address is clearly included in every posting I make.
Of course, you're welcome to post even-more-frequent pointers to the FAQ,
but we're already mentioning it several times every week. I'm not sure
that mentioning it any additional times will help; anyone who still can't
find it can't be paying too much attention. :-)
--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/
------------------------------
Date: 01 Sep 1998 00:19:03 -0400
From: Lloyd Zusman <ljz@asfast.com>
Subject: Re: Finding the FAQ (was: Re: No switch statement in Perl??)
Message-Id: <lt7lzowjm0.fsf@asfast.com>
Tom Phoenix <rootbeer@teleport.com> writes:
> On 1 Sep 1998, Adam Spragg wrote:
>
> > It's helpful in a reply like this to also include where the FAQ can
> >be found
>
> You're right that it might be helpful. But my thought is: Is there any
> reasonable way in which someone may look for the Perl FAQ and not find
> it?
I guess that depends on your definition of "reasonable". My own
definition of that word allows, on occasion, for the possibility of
providing a reference to the Perl FAQ, or perhaps the Perl FAQ Index:
http://language.perl.com/faq/index.html
Of course, given the wide range of meanings commonly assigned to the
word "reasonable", your mileage may vary.
> [ ... ]
--
Lloyd Zusman ljz@asfast.com
perl -e '$n=170;for($d=2;($d*$d)<=$n;$d+=(1+($d%2))){for($t=0;($n%$d)==0;
$t++){$n=int($n/$d);}while($t-->0){push(@r,$d);}}if($n>1){push(@r,$n);}
$x=0;map{$x+=(($_>0)?(1<<log($_-0.5)/log(2.0)+1):1)}@r;print"$x\n"'
------------------------------
Date: 1 Sep 1998 03:09:26 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: FMTEYEWTK on Switch Statements in Perl (was: No switch statement in Perl??)
Message-Id: <6sfoh6$a66$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, spiegler@cs.uri.edu writes:
:I cannot find a switch statement in Perl. Is there one?
man perlsyn or perlfaq7, or look at
http://www.perl.com/CPAN/doc/manual/html/pod/perlsyn.html#Basic_BLOCKs_and_Switch_Statemen
http://www.perl.com/CPAN/doc/manual/html/pod/perlfaq7.html#How_do_I_create_a_switch_or_case
:If not, do you know
:why it was not put into the language?
Because it seems unnecessary as is, and because no decision on something
more powerful has even been decided with.
:Also, does Perl provide any mechanism other than if-elsif-else to deal with
:this?
Many, but all can be placed into one of two classes, either multiple
sequential tests (`linear switches') or else a single that does a multiway
branch (`non-linear switches'). The first sort are all variations on
the same theme. You can affect performance minusculely by putting the
more likely cases first. You make a slight semantic distinction between
simple if()s and if/elsif()s chains. And you can make appearance choices
related to how to the tests are represented syntactically.
The short answer is that the switch statement in Perl is spelled for().
Here's a simple demo:
CASE 1:
for ($where) {
/In Card Names/ && do { push @flags, '-e'; last; };
/Anywhere/ && do { push @flags, '-h'; last; };
/In Rulings/ && do { last; };
die "unknown value for form variable where: `$where'";
}
But there is no performance gain to be had over here over a more
traditional arrangement. If you don't like that style, you can use this:
CASE 2:
SWITCH: for ($where) {
if (/In Card Names/) {
push @flags, '-e';
last SWITCH;
}
if (/Anywhere/) {
push @flags, '-h';
last SWITCH;
}
if (/In Rulings/) {
last SWITCH;
}
die "unknown value for form variable where: `$where'";
}
or you can use this:
CASE 3:
SWITCH: for ($where) {
if (/In Card Names/) {
push @flags, '-e';
}
elsif (/Anywhere/) {
push @flags, '-h';
}
elsif (/In Rulings/) {
; # nothing needed
}
else {
die "unknown value for form variable where: `$where'";
}
}
Or more concisely as this:
CASE 4:
SWITCH: for ($where) {
if (/In Card Names/) { push @flags, '-e'; }
elsif (/Anywhere/) { push @flags, '-h'; }
elsif (/In Rulings/) { } # do nothing
else { die "unknown whence: `$where'" }
}
There's really nothing to terrible with this, and no completely
convincing reason not to just write the elsif()s:
CASE 5:
chomp($answer = <>);
if ("SEND" =~ /^\Q$answer/i) { print "Action is send\n" }
elsif ("STOP" =~ /^\Q$answer/i) { print "Action is stop\n" }
elsif ("ABORT" =~ /^\Q$answer/i) { print "Action is abort\n" }
elsif ("LIST" =~ /^\Q$answer/i) { print "Action is list\n" }
elsif ("EDIT" =~ /^\Q$answer/i) { print "Action is edit\n" }
Or as:
CASE 6:
if ($visibility == SCREEN_DISPLAY) {
# do something
}
elsif ($visibility == SCREEN_HIDDEN) {
# do something
}
elsif ($visibility == SCREEN_CODEGEN) {
# do something
}
else {
# do something drastic
}
Many similar, sequential-testing mechanisms are seen in Perl.
You can use "&& ||" combos, but then you have to be very
careful that the RHS of && is always a true value.
CASE 7:
$dir = 'http://www.wins.uva.nl/~mes/jargon';
for ($ENV{HTTP_USER_AGENT}) {
$page = /Mac/ && 'm/Macintrash.html'
|| /Win(dows )?NT/ && 'e/evilandrude.html'
|| /Win|MSIE|WebTV/ && 'm/MicroslothWindows.html'
|| /Linux/ && 'l/Linux.html'
|| /HP-UX/ && 'h/HP-SUX.html'
|| /SunOS/ && 's/ScumOS.html'
|| 'a/AppendixB.html';
}
This is actually better written with a bunch of ?: constructs instead
in most cases because the RHS of the && might false out on you
in the more general case:
CASE 8:
$which_search = $opts{"t"} ? 'title'
: $opts{"s"} ? 'subject'
: $opts{"a"} ? 'author'
: 'title';
Or if you are assigning a list, you could do this:
CASE 9:
($msg, $defstyle) = do {
$i == 1 ? ("First", "Color" ) :
$i == 2 ? ("Then", "Rarity") :
("Then", "Name" )
};
Or even this, amking more use of the do{}'s blockness:
CASE 10:
($FORMAT_NAME, $FORMAT_TOP_NAME) = do {
if ($long_form) { qw( Long Long_Top ) }
elsif ($just_inode) { qw( Inodes Inodes_Top ) }
elsif ($sys5) { qw( SysV SysV_Top ) }
elsif ($bsd) { qw( BSD BSD_Top ) }
else { die " No format?" }
};
Sometimes one has been known to use to do { } superfluously just to make
things look pretty. Sometimes, aesthetics counts. :-)
CASE 11:
for ($^O) {
*struct_flock = do {
/bsd/ && \&bsd_flock
||
/linux/ && \&linux_flock
||
/sunos/ && \&sunos_flock
||
die "unknown operating system $^O, bailing out";
};
}
The do there is just so the Christmas tree has a star. :-)
Here's another do{} style switch, turned another way:
CASE 12:
$amode = do {
if ($flag & O_RDONLY) { "r" } # XXX: isn't this 0?
elsif ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" }
elsif ($flag & O_RDWR) {
if ($flag & O_CREAT) { "w+" }
else { ($flag & O_APPEND) ? "a+" : "r+" }
}
};
Here's an old example for argument processing:
CASE 13:
while ($ARGV[0] =~ /^-(.+)/ && (shift, ($_ = $1), 1)) {
next if /^$/;
s/a// && (++$autoload, redo);
s/p// && (++$postcompile, redo);
s/f// && do {
$FILE = $_ || shift;
open(FILE) || die "can't open $FILE: $!";
while (<FILE>) {
push(@required, split(' '));
}
redo;
};
die "usage $0 [-pa] [-f module_list] [module] ...\n";
}
And here's another, simpler one:
CASE 14:
while ($ARGV[0] =~ /^-/) {
$ARGV[0] =~ /^-a/ && ($all++,shift,next);
$ARGV[0] =~ /^-d/ && ($debug++,shift,next);
$ARGV[0] =~ /^-n/ && ($idle = 0,shift,next);
$ARGV[0] =~ /^-s/ && ($slumber++,shift,next);
$ARGV[0] =~ /^-i/ && (shift,$idle=$ARGV[0],shift,next);
last;
}
And here's one more. I used to do this a lot. :-)
CASE 15:
OPT: while ($ARGV[0] =~ /^-(.*)/ && (shift, $_ = $1, 1)) {
/^$/ && next OPT;
/^\d+/ && do {
$DEPTH = $_;
next OPT;
};
s/q// && do {
$QUIET = 1;
redo OPT;
};
/d/ && do {
$debug += s/d//g;
redo OPT;
};
&usage("Unknown option: -$_");
}
As you see in the last case, the test doesn't always have to be the
same kind. There are definite advantages to this approach. For example:
CASE 16:
for ( $big->long->hairy->expression ) {
/pat/i && do { ...... };
$_ > 10 && do { ...... };
$_ == 13 && do { ...... };
lc eq "FRED" && do { ...... };
$now > 2*$_ && do { ...... };
# do default
}
In that case, we aren't in each case making the same sort of test (string,
numeric, pattern, etc). Instead, we pick and choose as we go.
But all the preceding versions share one critical trait: they all
perform a series of tests sequentially. That's why we called them
`linear switches' above. That means that they don't scale well to a
large number of cases. Of course, this really never matters all that
much, since in the number of different cases we're talking about here
is virtually always too small to fret over.
But any good programmer, when confronted with extremely similar code,
has a nearly irrepressible urge to unify. As Kernighan has said, one
should capture regularity with data, irregularity with code. That
means that seeing repeated instances of a nearly identical thing,
like this:
if ($who eq "Fred") { ... }
if ($who eq "Barney") { ... }
if ($who eq "Wilma") { ... }
makes any programmer worth his salt say, ``That's too much similarity
in code. It should be in data!'' And indeed, it probably should be,
and like most interesting data structures in Perl, this should likely
be based on a hash. Here's what you could do instead:
CASE 17:
%action = (
"Fred" => \&greet,
"Barney" => \&shuffle,
"Wilma" => \&chat,
# add as you will
);
if ($verb = $action{$who}) {
&$verb();
} else {
die "unknown character: $who";
}
Those of you corrupted into the infinite hunt for a one-liner
to everything might prefer this approach:
CASE 18:
{
"Fred" => \&greet,
"Barney" => \&shuffle,
"Wilma" => \&chat,
}->{$who}->();
Except that now you haven't managed to catch the null case.
CASE 19:
( {
"Fred" => \&greet,
"Barney" => \&shuffle,
"Wilma" => \&chat,
}->{$who} || sub { die: "who's $who?" })->();
But that's getting a bit silly. Better there to
do it in several steps.
These functions can even be defined right there as closures:
CASE 20:
%actions = (
"edit" => \&invoke_editor,
"send" => \&deliver_message,
"list" => sub { system($PAGER, $file) },
"abort" => sub {
print "See ya!\n";
exit;
},
"" => sub {
print "Unknown command: $cmd\n";
$errors++;
},
);
$href = abbrev(keys %actions);
local $_;
for (print "Action: "; <>; print "Action: ") {
s/^\s+//; # trim leading white space
s/\s+$//; # trim trailing white space
next unless $_;
$actions->{ $href->{ lc($_) } }->();
}
Imagine where you had a switch with 100 cases in a tight loop. You'd
definitely want to hash up the cases for instant access rather than
making all the tests each time. In fact, this would scale to any number
of cases (assuming that the hash keys weren't permutations of each other,
which pessimizes perl's hashing).
Here's an example of a non-linear switch with an in-line hash:
CASE 21:
my $newop = {
'==' => 'eq',
'!=' => 'ne',
'>=' => 'ge',
'>' => 'gt',
'<=' => 'le',
'<' => 'lt',
'=~' => '=~',
'!~' => '!~',
}->{$operator} || "MISSING OPERATOR";
It would be marginally better to assign that to a named hash once,
as we did in the previous case.
Those of you who have coded up finite state machines may be familiar
with one final approach:
CASE 22:
while ($state = $action_table{$state}->()) {
print "Moving on to state $state\n";
}
Here, each function state in the action table returns the next state to
transition to. The table would hold as values the function to call at
each state, and those functions would return a string which is the next
state to go to.
So, anybody read this far? :-)
--tom
--
tmps_base = tmps_max; /* protect our mortal string */
--Larry Wall in stab.c from the perl source code
------------------------------
Date: Mon, 31 Aug 1998 21:15:56 -0700
From: "C. Abney" <cabney@cyberpass.net>
To: Tom Phoenix <rootbeer@teleport.com>
Subject: Re: Funny, that: $#myarray
Message-Id: <Pine.LNX.4.02A.9808311931030.10003-100000@rodentia.cyberpass.net>
On Mon, 31 Aug 1998, Tom Phoenix wrote:
> On 1 Sep 1998, C. Abney wrote:
> > ...the following is always true:
> >
> > scalar(@whatever) == $#whatever + 1;
>
> Yes, so long as you haven't been tampering with the special $[ variable.
> But you shouldn't be doing that in any case.
I haven't. I would never knowingly violate the sanctity of zero. :)
> > but my recent experience tells me $#myarray gives me something more
> > like the length of the first element in @myarray than the size of the
> > entire array itself.
>
> The length of an element? Not in a million years. :-)
Just how many monkeys are we talkin' about here? :) I'm telling you,
for the test data, the number printed out for $#myarray/4 is 1
(for "1. e4 c4"?) and scalar(@myarray) is 41 (the whole array). I
shouldn't have mentioned the length thing, it's just confusing things.
Just to pull things even more off track: I could get 1 if $#movelist
were -1, as well (and I would if the array were empty, IIRC.)
Hmm.
> > use integer;
>
> Uh oh. Do you know what you're doing with this?
Yeah, I want to get the number of rows I can print out four columns of
data in. I turn it off right after this section of code.
> > $height = $#movelist / 4 + 1;
> > # ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > # instead of the following, which works:
> > # $height = @movelist / 4 + 1;
>
> Suppose there are 100 elements in @movelist. Your first expression would
> return int(99/4)+1 => 25, but the second should return int(100/4)+1 => 26.
You got me on that one: should be '= ($#movelist + 1)/4 + 1;'. The added
1 is because I need ((#elements/4) + 1) x 4 cells to format the data in,
and I want to display the sequence vertically instead of horizontally.
The 'use integer' isn't strictly necessary for this section, but
makes the value easier for me to understand. Thanks doubly, because that
sort of error would only show up in a small number of results (i.e.,
where ($num + 1) % 4 = 0.) :)
> Which one do you want? My suspicion is that you need to know the number of
> elements in the array, rather than the last array index, but maybe your
> formula is defective. In any case, I see no sign of the length of an
> element having any relevance. Hope this helps!
Ok, you've made me deeply suspicious of my math skills. I'm going to
rig up a test case.
-C
[glad for this chance to parade my new .sig about]
--
Internet Service Providers: MAKE MONEY FAST!!!!!!! TOTALLY LEGAL!
Collect UCE/UBE from your users, and $50 per incidence!
Offer only good in California, Nevada, and Washington. -C. Abney
------------------------------
Date: Tue, 1 Sep 1998 03:26:53 GMT
From: edew@netcom.com (Eric Dew)
Subject: Help on running perl scripts on MS-DOS (Win NT4 actually)
Message-Id: <edewEyL5Kt.G1o@netcom.com>
Hello folks,
I've written a perl script for UNIX and need to port it to MS-DOS/Win NT4.0.
I've got perl up and running on the NT machine, but I don't know how exactly
to get my script to work (as well as some file system questions).
On UNIX, the script is run as such:
% perlscript -d <date> -r <name>
Will I be able to do that straight away on NT, or do I have to say:
C:\perl5>perl perlscript -d <date> -r <name>
(do I have to rename the script to have a proper suffix, like perlscript.pl?)
I understand that I have to run the perl script as a batch file and so I
included the following lines to the top of the file:
@echo off
goto endofperl
@rem';
and to the end of the file:
__END__
:endofperl
perl -S %0.BAT %1 %2 %3 %4 %5 %6 %7 %8 %9
Is that correct? Does that ring a bell? Does that make sense?
Next, I plan to run the perl script on drive D:\, but want it to do
things to files on drive F:\ (say).
So, how to do I tell it to change drives to F:\ ? I'm putting in the
line,
`F:\`;
wherever I need to change to drive F.
In regards to drives and directories, I need to cd down F:\ to some
subdirectories. Can I write:
`F:\`;
chdir('\'); # to cd to top of drive F:
chdir("$dir1\$subdir1\$subdir2");
or do I have to use "\\" in place of "\", as in:
`F:\\`;
chdir('\'); # since single-quotes take a literal with no interpolation
# I can use just one "\", right?
chdir("$dir1\\$subdir1\\$subdir2"); # Note the double slashes.
Of course, I could find out whether one back-slash or two back-slashes will
work, once I can get the script to run, which goes back to the first set
of questions.
Email or generous posting to this ng will be appreciated.
EDEW
------------------------------
Date: Tue, 01 Sep 1998 03:49:22 GMT
From: sowmaster@juicepigs.com (Bob Trieger)
Subject: Re: Help on running perl scripts on MS-DOS (Win NT4 actually)
Message-Id: <6sfr2f$cso$1@ligarius.ultra.net>
[ posted to usenet and courtesy e-mail sent to edew@netcom.com ]
edew@netcom.com (Eric Dew) wrote:
-> Hello folks,
->
-> I've written a perl script for UNIX and need to port it to MS-DOS/Win NT4.0.
-> I've got perl up and running on the NT machine, but I don't know how exactly
-> to get my script to work (as well as some file system questions).
->
-> On UNIX, the script is run as such:
-> % perlscript -d <date> -r <name>
->
-> Will I be able to do that straight away on NT, or do I have to say:
-> C:\perl5>perl perlscript -d <date> -r <name>
-> (do I have to rename the script to have a proper suffix, like perlscript.pl?)
Well, what happened when you tried it?
-> Next, I plan to run the perl script on drive D:\, but want it to do
-> things to files on drive F:\ (say).
->
-> So, how to do I tell it to change drives to F:\ ? I'm putting in the
-> line,
-> `F:\`;
-> wherever I need to change to drive F.
Why are you doing that. Have you tried: chdir "f:/";
-> In regards to drives and directories, I need to cd down F:\ to some
-> subdirectories. Can I write:
-> `F:\`;
-> chdir('\'); # to cd to top of drive F:
-> chdir("$dir1\$subdir1\$subdir2");
->
-> or do I have to use "\\" in place of "\", as in:
-> `F:\\`;
-> chdir('\'); # since single-quotes take a literal with no
-> interpolation
-> # I can use just one "\", right?
-> chdir("$dir1\\$subdir1\\$subdir2"); # Note the double slashes.
You must escape the backwhacks if you insist on using them but it is
much easier to just use a single slash.
IE:
chdir "f:/";
chdir "/$dir1/$dir2/$dir3";
You can try it all in one line but weird things happen on some of my
machine when accessing multiple disks.
-> Of course, I could find out whether one back-slash or two back-slashes will
-> work, once I can get the script to run, which goes back to the first set
-> of questions.
->
-> Email or generous posting to this ng will be appreciated.
Howsabout both?
HTH
Bob Trieger
sowmaster@juicepigs.com
------------------------------
Date: Tue, 1 Sep 1998 04:26:51 GMT
From: edew@netcom.com (Eric Dew)
Subject: Re: Help on running perl scripts on MS-DOS (Win NT4 actually)
Message-Id: <edewEyL8Cr.Hz1@netcom.com>
In article <6sfr2f$cso$1@ligarius.ultra.net> sowmaster@juicepigs.com (Bob Trieger) writes:
>[ posted to usenet and courtesy e-mail sent to edew@netcom.com ]
>edew@netcom.com (Eric Dew) wrote:
>-> Hello folks,
>->
>-> I've written a perl script for UNIX and need to port it to MS-DOS/Win NT4.0.
>-> I've got perl up and running on the NT machine, but I don't know how exactly
>-> to get my script to work (as well as some file system questions).
>->
>-> On UNIX, the script is run as such:
>-> % perlscript -d <date> -r <name>
>->
>-> Will I be able to do that straight away on NT, or do I have to say:
>-> C:\perl5>perl perlscript -d <date> -r <name>
>-> (do I have to rename the script to have a proper suffix, like perlscript.pl?)
>
>Well, what happened when you tried it?
>
If I did C:\per5>perl perlscript ... it said:
Bare word found where operator expected at perlscript.pl line 1, near "echo off"
(Missing operator before off?)
syntax error at perlscript.pl line 2, next token ???
Array found where operator expected at perlscript.pl line 3, near "@rem"
(Missing semicolon on previous line?)
...
>-> Next, I plan to run the perl script on drive D:\, but want it to do
>-> things to files on drive F:\ (say).
>->
>-> So, how to do I tell it to change drives to F:\ ? I'm putting in the
>-> line,
>-> `F:\`;
>-> wherever I need to change to drive F.
>
>Why are you doing that. Have you tried: chdir "f:/";
Because to change to another drive on DOS, one types:
C:\>D:
to get to
D:\>
Thus, using chdir (which I would assume will equal to cd on DOS) will not
work, because
C:\>cd D:
does not change the drive to D:\>.
>
>-> In regards to drives and directories, I need to cd down F:\ to some
>-> subdirectories. Can I write:
>-> `F:\`;
>-> chdir('\'); # to cd to top of drive F:
>-> chdir("$dir1\$subdir1\$subdir2");
>->
>-> or do I have to use "\\" in place of "\", as in:
>-> `F:\\`;
>-> chdir('\'); # since single-quotes take a literal with no
>-> interpolation
>-> # I can use just one "\", right?
>-> chdir("$dir1\\$subdir1\\$subdir2"); # Note the double slashes.
>
>You must escape the backwhacks if you insist on using them but it is
>much easier to just use a single slash.
>
>IE:
> chdir "f:/";
> chdir "/$dir1/$dir2/$dir3";
>
Again, on DOS,
cd dir1/dir2
does not make sense. DOS prompt returns:
The syntax of the command is incorrect
(Meaning the forward slash is wrong.)
So, your suggestion, chdir "f:/"; will result in NOT changing to the F:
drive (at least that's what I'm guessing...as I said before and below, if
I can get it running, even with bugs, I can guess how to fix the paths)
and chdir "/$dir1/$dir2/"; will not result in cd-ing to the proper directory.
Hope that helps some more.
EDEW
------------------------------
Date: 1 Sep 98 03:13:38 GMT
From: spragg@table.jps.net (Adam Spragg)
Subject: Re: No switch statement in Perl??
Message-Id: <35eb6662.0@blushng.jps.net>
Tom Phoenix (rootbeer@teleport.com) wrote:
: On Mon, 31 Aug 1998 spiegler@cs.uri.edu wrote:
: > Subject: No switch statement in Perl??
: This is discussed in the FAQ. Cheers!
It's helpful in a reply like this to also include where the FAQ can be
found (a FAQ in and of itself, no? "That tail looks mighty tasty" said
the snake of his own.)
Off the top of my head, I'd guess http://www.perl.org would be a good
place to start...
Cheers yerself!
Adam
------------------------------
Date: 31 Aug 1998 19:03:23 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Perl Cookbook, does anyone have it?
Message-Id: <6sfa3r$si3$1@monet.op.net>
In article <Pine.SOL.3.96.980829191232.25638A-100000@ascc.artsci.wustl.edu>,
eli <elaine@artsci.wustl.edu> wrote:
>its not complete, however, without a recipe for camel and/or llama from
How about this:
Stuffed Camel
1 whole camel, medium size
1 whole lamb, large size
20 whole chickens, medium size
60 eggs
12 kilos rice
2 kilos pine nuts
2 kilos almonds
1 kilo pistachio nuts
110 gallons water
5 pounds black pepper
Salt to taste
Skin, trim and clean camel, lamb and chicken. Boil until
tender. Cook rice until fluffy. Fry nuts until brown and mix
with rice. Hard boil eggs and peel. Stuff cooked chickens with
hard boiled eggs and rice. Stuff the cooked lamb with stuffed
chickens. Add more rice. Stuff the camel with the stuffed lamb
and add rest of rice. Broil over large charcoal pit until
brown. Spread any remaining rice on large tray and place camel
on top of rice. Decorate with boiled eggs and nuts. Serves
friendly crowd of 80-100.
Shararazod Eboli Home Economist, Dammam, Saudi Arabia
The idea of a camel stuffed with lamb stuffed with chickens stuffed
with eggs sounds appropriately Perlish to me. Personally, I think it
sounds like it needs about a pound of sage, but I realize that that
wuold not be authentic.
If O'Reilly ever has a Perl Conference in Philadelphia, and if Mary
Koutsky volunteers to help me with the roasting, I will cook and serve
this for the Perl 5 Porters. Good thing the recipe calls for a medium
camel; I don't think the local stores carry them any larger.
------------------------------
Date: 01 Sep 1998 00:46:41 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Perl Cookbook, does anyone have it?
Message-Id: <x77lzo30em.fsf@sysarch.com>
>>>>> "MD" == Mark-Jason Dominus <mjd@op.net> writes:
MD> How about this:
MD> Stuffed Camel
<snip of amazing recipe>
MD> Shararazod Eboli Home Economist, Dammam, Saudi Arabia
MD> The idea of a camel stuffed with lamb stuffed with chickens stuffed
MD> with eggs sounds appropriately Perlish to me. Personally, I think it
MD> sounds like it needs about a pound of sage, but I realize that that
MD> wuold not be authentic.
i agree with the perlishness of the recipe. why wasn't it in the
cookbook? my wife asked me how to cook perls when i told her about the
book! :-)
MD> If O'Reilly ever has a Perl Conference in Philadelphia, and if Mary
MD> Koutsky volunteers to help me with the roasting, I will cook and serve
MD> this for the Perl 5 Porters. Good thing the recipe calls for a medium
MD> camel; I don't think the local stores carry them any larger.
i think this should be the main course at the banquet at the perl
conference. other conferences have major banquets with awards, boring
speeches, rubber chicken, etc. we could have this dish and lots of good
beer to wash it down (and maybe drown out the taste ?).
or better yet make <mee> eat the whole thing so he would finally learn
something about camels!!!
uri
--
Uri Guttman ----------------- SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire ---------------------- Perl, Internet, UNIX Consulting
uri@sysarch.com ------------------------------------ http://www.sysarch.com
The Best Search Engine on the Net ------------- http://www.northernlight.com
------------------------------
Date: Mon, 31 Aug 1998 21:56:29 -0500
From: "Jonathan Tapp" <jt@eatel.net>
Subject: Re: Y2K Date Support
Message-Id: <35eb6336.0@news.eatel.net>
Perl is semi-Y2K safe. It's date readout reads out the years past 1900.
Therefore, October 10, 2002 (random date) would be 10/10/102 in Perl. Okay,
It's a little weird. See if you can't snip off the '1' in '102'.
Jonathan Tapp
jonathan-removethisfirst-_tapp@hotmail.com
------------------------------
Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>
Administrivia:
Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.
If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu.
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V8 Issue 3602
**************************************