[6889] in Perl-Users-Digest
Perl-Users Digest, Issue: 514 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 22 01:07:23 1997
Date: Wed, 21 May 97 22:00:23 -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 Wed, 21 May 1997 Volume: 8 Number: 514
Today's topics:
Re: Change or Delete a line in a FILE <usenet-tag@qz.little-neck.ny.us>
Re: Change or Delete a line in a FILE (Tung-chiang Yang)
Re: Help with delimited fields... (Tad McClellan)
Hide Perl Code? Perl for NT (Tracy Bednar)
Locking my Log file... silver@silverbergcom.com
Re: machttp and perl (Joel Klecker)
Merging reply email with proper sent email (TAN N. NGUYEN)
Re: Merging reply email with proper sent email (Tung-chiang Yang)
Re: New draft of scripting white paper <cimarron@dis.org>
Re: Normal Array to Associative Array <friedman@uci.edu>
Re: Q: Untainting an environment variable without chang (Tung-chiang Yang)
Re: Q: Untainting an environment variable without chang (Mike Stok)
Re: RCS-style version mgmt implementation in Perl? (Paul D. Smith)
REGEXP Help Needed (Richard Kennedy)
Re: Regular expression problem (Chipmunk)
script to parse from lines? <matthew@spam.davin.ottawa.on.ca>
Re: syntax for subroutine reference with sort <friedman@uci.edu>
Re: What do you do in place of 'case' or 'switch' (Mike Stok)
Re: What do you do in place of 'case' or 'switch' (Tad McClellan)
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 22 May 1997 02:00:35 GMT
From: Eli the Bearded <usenet-tag@qz.little-neck.ny.us>
Subject: Re: Change or Delete a line in a FILE
Message-Id: <5m09c3$8m6$1@news.netusa.net>
Tung-chiang Yang <tcyang@netcom.com> wrote:
>You need to tell us what kind of line you want to delete, say,
>
>(1) an empty line,
>(2) a line with the keyword "tcyang", or
>(3) the 103rd line.
How about lines with phone numbers on them?
m:\b([(1]*(888|900|800|809)[-\s\)/\.]+\d{3}[-\s/\.]*\d{4}|
011[\s.\/+-]*(\()?\d{3}(\))?[\s.\/+-]*\d{2,}[\s.\/+-]*\d+)\b:x
Any obvious variations (besides different area codes) that I missed?
Elijah
------
knows that is not a stunning example of efficient regexp craftwork
------------------------------
Date: Thu, 22 May 1997 02:22:33 GMT
From: tcyang@netcom.com (Tung-chiang Yang)
Subject: Re: Change or Delete a line in a FILE
Message-Id: <tcyangEAK99L.KJE@netcom.com>
You need to pay me $100 to make me take a look at your pattern :)
(how come there is an alt.fan.e-t-b group?)
Have you considered the pattern with letters (like 1(800)CALLATT)?
===================================
Eli the Bearded (usenet-tag@qz.little-neck.ny.us) wrote:
: How about lines with phone numbers on them?
: m:\b([(1]*(888|900|800|809)[-\s\)/\.]+\d{3}[-\s/\.]*\d{4}|
: 011[\s.\/+-]*(\()?\d{3}(\))?[\s.\/+-]*\d{2,}[\s.\/+-]*\d+)\b:x
: Any obvious variations (besides different area codes) that I missed?
--
Tung-chiang Yang tcyang@netcom.com
soc.culture.taiwan, soc.culture.china (by SCC FAQ Team) FAQ's:
http://www.clever.net/tcyang/Taiwan_faq.shtml, China_faq.shtml
------------------------------
Date: Wed, 21 May 1997 20:48:19 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Help with delimited fields...
Message-Id: <3l80m5.q24.ln@localhost>
Paul W. Campbell (campbell@getnet.com) wrote:
: (sorry about the formatting)
: I want to take this:
: --------------------
: login|98-P49344W|CFI_FB3.0|1.1|96/10/02 04:08:23||96/10/0215:29:46|sgc/bin/login|1.1|96/10/02 04:08:23||sgc/bin/login||0644||||||||||||||||||||
: cshrc|98-P49344W|CFI_FB3.0|1.3|96/10/02 04:02:44||97/02/2005:28:48|sgc/bin/cshrc|1.3|96/10/02 04:02:44||sgc/bin/cshrc||0644||||||||||||||||||||
: And make it look like:
: ----------------------
: 1.1 sgc/bin/login
: 1.3 sgc/bin/cshrc
: ---
: As you'll note, I want only fields 9 & 8 (respectively) and it will
: alwways be delimited by a "|". What is the best was to do this?
split()
----------------------------------
#! /usr/bin/perl -w
while (<DATA>) {
($file, $num) = (split /\|/)[7,8]; # split on vert bars, take 8th
print "$num $file\n"; # and 9th fields
}
__DATA__
login|98-P49344W|CFI_FB3.0|1.1|96/10/02 04:08:23||96/10/0215:29:46|sgc/bin/login|1.1|96/10/02 04:08:23||sgc/bin/login||0644||||||||||||||||||||
cshrc|98-P49344W|CFI_FB3.0|1.3|96/10/02 04:02:44||97/02/2005:28:48|sgc/bin/cshrc|1.3|96/10/02 04:02:44||sgc/bin/cshrc||0644||||||||||||||||||||
----------------------------------
: I appreciate any help!
--
Tad McClellan SGML Consulting
Tag And Document Consulting Perl programming
tadmc@flash.net
------------------------------
Date: Tue, 20 May 1997 14:51:04 GMT
From: spectran@netcom.com (Tracy Bednar)
Subject: Hide Perl Code? Perl for NT
Message-Id: <spectranEAHIL5.BqM@netcom.com>
Is there a way to "tokenize" perl code to hide the actual source? I
saw a program, but it doesn't seem to be around anymore.
Also where can you download the perl executable for NT 4.0.
Thank you,
- T
------------------------------
Date: Wed, 21 May 1997 20:42:25 +0100
From: silver@silverbergcom.com
Subject: Locking my Log file...
Message-Id: <33835005.6E62@silverbergcom.com>
I have a logging script that opens up a text file & writes to it every
time it gets a hit. I understand I should lock the text file it so that
it doesn't get confused if it is called twice at the same time. The code
I have looks like this:
open (LOGGER, ">>$log_file");
flock(<LOGGER>,LOCK_EX);
print LOGGER "$date\t";
print LOGGER "$ENV{'REMOTE_HOST'}\t";
print LOGGER "$query_string\t";
print LOGGER "$ENV{'HTTP_USER_AGENT'}\n";
flock(<LOGGER>,LOCK_UN);
close (LOGGER);
But I get error messages that say "Argument "LOCK_EX" isn't numeric in
flock at logger.pl line 71." I looked in the perl man pages but all it
has is some stuff using subroutines and scalars. I don't want subs, I
just want to use flock straight. Does anybody know what I am doing
wrong?
Thanks,
Eric Silverberg
------------------------------
Date: Wed, 21 May 1997 20:32:22 -0700
From: jk@esperance.com (Joel Klecker)
Subject: Re: machttp and perl
Message-Id: <jk-2105972032220001@ip-salem4-10.teleport.com>
In article <acs-2005972313080001@port135.bitstream.net>,
schneider@pobox.com wrote:
>You may also want to check out Quid Pro Quo, a freeware Web server that's
>a little easier to set up than MacHTTP....
It's also much faster, only WebStar 2.0 beats it.
--
Joel Klecker (mailto:jk@esperance.com) <URL:http://www.esperance.com/>
The [US] Senate trying to govern cyberspace is analogous to King George
believing he could still govern the colonies even though he had never been
there. -- John Perry Barlow
------------------------------
Date: 22 May 1997 03:49:48 GMT
From: tnguy1@osf1.gmu.edu (TAN N. NGUYEN)
Subject: Merging reply email with proper sent email
Message-Id: <5m0fos$h7t@portal.gmu.edu>
Hi,
Could anyone recommend a Perl script that will monitor all replied Email
then search the sent mailbox to merge with the right email sent. For
example, I sent to JohnDoe@company.com an email and JohnDoe replied my
email. I would like to catch JohnDoe's response email, search for my
email that I sent to him, then merge my mail with his response.. then
for example save it to a mail-merge directory.
Thank you.
Tan Nguyen
tnguy1@gmu.edu
------------------------------
Date: Thu, 22 May 1997 04:45:15 GMT
From: tcyang@netcom.com (Tung-chiang Yang)
Subject: Re: Merging reply email with proper sent email
Message-Id: <tcyangEAKFvF.y5@netcom.com>
You can use perl to do this, but this question is not just perl's work.
You also need to keep a copy of the E-mails you sent to John Doe somewhere
in a predetermined directory. Besides, when his reply comes back, you
need to locate the filename of the previous copy and append his reply
there. Both of these are not easy with Perl but with procmail or sendmail.
The only thing Perl can do for you is matching the two subjects
(You sent to John) A Simple Request for you
(John replies you) Re: A Simple Request for you
And all the other messy works are for procmail or other mail filters.
Try to post this question with some mail program groups.
===================================
TAN N. NGUYEN (tnguy1@osf1.gmu.edu) wrote:
: Hi,
: Could anyone recommend a Perl script that will monitor all replied Email
: then search the sent mailbox to merge with the right email sent. For
: example, I sent to JohnDoe@company.com an email and JohnDoe replied my
: email. I would like to catch JohnDoe's response email, search for my
: email that I sent to him, then merge my mail with his response.. then
: for example save it to a mail-merge directory.
: Thank you.
: Tan Nguyen
: tnguy1@gmu.edu
--
Tung-chiang Yang tcyang@netcom.com
soc.culture.taiwan, soc.culture.china (by SCC FAQ Team) FAQ's:
http://www.clever.net/tcyang/Taiwan_faq.shtml, China_faq.shtml
------------------------------
Date: Wed, 21 May 1997 19:22:34 -0700
From: Cimarron Taylor <cimarron@dis.org>
Subject: Re: New draft of scripting white paper
Message-Id: <3383ADEA.58FA@dis.org>
Bob De Wolf wrote:
> Cimarron Taylor, [... Tcl is really just a variation of /bin/sh ...]
>
> Not really. The statements provided by /bin/sh et all are hard coded in
> the interpreter. The tcl interpreter does not define language
> statements, only language structure. Statements and procedures are the
> same in tcl. With the tcl 'proc' command you can add a new statement
> (you can also do this with 'c'). To demonstrate this, you can add an
> 'until' statment to tcl with a four or five lines of tcl in a 'proc'
> command (you could also use 'c'). 'until' does the same thing as 'while'
> except that the logic inverted. I don't think you can do this kind of
> thing with /bin/sh or anything derived from it. You can't do it with c
> or c++, pascal, ... etc, etc.
You can do some of these things in sh and other shells but
it's a little more clumsy. If you're willing to resort
to using the preprocessor you can do this in C as well.
I believe the original implementation of /bin/sh actually
did this! The code I read used directives such as
#define BEGIN { and #define END } to make C look like sh.
But I digress...
My main objection here is to the use of the term "paradigm".
The point is that Tcl, sh, csh, ksh, command.com, ... are all
variations of shell languages. They all provide the bare essentials
(expressions and control structures) you need to write simple
programs and also provide mechanisms to interact with other code.
This is what they are designed to do and this is what they do well.
However, creating new procedures and alternate ways of expressing
flow control are all well and good but to say these techniques
allow you to extend the "programming paradigm" is too much of a
stretch for me.
Cimarron Taylor
cimarron@dis.org
------------------------------
Date: 22 May 1997 02:55:22 GMT
From: "Eric D. Friedman" <friedman@uci.edu>
Subject: Re: Normal Array to Associative Array
Message-Id: <5m0ciq$j8o@news.service.uci.edu>
In article <5lvetb$2k3@news.tamu.edu>,
Michael Caver <ratboy@cyberenet.net> wrote:
>>I'm trying to move the text content of a normal array to an associative array
>>without a lot of "foreach" statements. I saw some posts here that said use:
use map to do the foreach'ing somewhat more tersely:
my @ab = qw(two three four);
my %ba = map { $_ , 1 } @ab;
my $k;
foreach $k (keys %ba) { print "$k $ba{$k}\n"; }
--
Eric D. Friedman
friedman@uci.edu
------------------------------
Date: Thu, 22 May 1997 02:15:40 GMT
From: tcyang@netcom.com (Tung-chiang Yang)
Subject: Re: Q: Untainting an environment variable without changing it
Message-Id: <tcyangEAK8y4.K8A@netcom.com>
Somewhere in the Camel Book (1st edition) it says that the only way
to untaint a variable is passing it through a pattern matchinng, if
I am right.
==================================
Andrew Pollock (apollock@bit.net.au) wrote:
: Heaven forbid that I've asked a FAQ here, but...
: I'm writing a setuid CGI script that needs access to $ENV{'REMOTE_USER'}.
: How can I untaint this environment variable? A perl -T is spewing when I
: make a system call using this variable as a parameter.
--
Tung-chiang Yang tcyang@netcom.com
soc.culture.taiwan, soc.culture.china (by SCC FAQ Team) FAQ's:
http://www.clever.net/tcyang/Taiwan_faq.shtml, China_faq.shtml
------------------------------
Date: 22 May 1997 02:52:13 GMT
From: mike@stok.co.uk (Mike Stok)
Subject: Re: Q: Untainting an environment variable without changing it
Message-Id: <5m0cct$goc@news-central.tiac.net>
In article <01bc6651$452a5e80$045e12cb@minotaur>,
Andrew Pollock <apollock@bit.net.au> wrote:
>Heaven forbid that I've asked a FAQ here, but...
>
>I'm writing a setuid CGI script that needs access to $ENV{'REMOTE_USER'}.
>How can I untaint this environment variable? A perl -T is spewing when I
>make a system call using this variable as a parameter.
One of the reasons for tainting a vairable is to make sure that external
data is vetted in some way, consider this where a variable mustn't be
empty or contain any of the unsafe charcaters in the character class:
#!/usr/local/bin/perl -wT
foreach (qw/PATH HOME/) {
print "$_ is tainted\n" if &is_tainted ($ENV{$_});
if ($ENV{$_} =~ /^([^'"\\:\000-\037\200-\377]+)$/) {
$copy = $1;
print "$_ contains no 'unsafe' characters\n";
print "copy is untainted\n" unless &is_tainted ($copy);
}
}
sub is_tainted {
return ! eval { () = join('',@_), kill 0; 1; };
}
__END__
Hope this helps,
Mike
--
mike@stok.co.uk | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ | PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/ | 65 F3 3F 1D 27 22 B7 41
stok@psa.pencom.com | Pencom Systems Administration (work)
------------------------------
Date: 22 May 1997 00:13:14 -0400
From: psmith@baynetworks.com (Paul D. Smith)
To: vlb@apple.com (Vicki Brown)
Subject: Re: RCS-style version mgmt implementation in Perl?
Message-Id: <p5enb0nnz9.fsf@baynetworks.com>
Uh, I think it'd probably be faster and easier to port RCS itself to the
Mac. Heck, maybe someone's already done it (try asking on
gnu.utils.bug).
Aside from a working diff there's nothing in RCS that's very
system-dependent. If it works on DOS (and it does) it could surely work
on Macs.
--
-------------------------------------------------------------------------------
Paul D. Smith <psmith@baynetworks.com> Network Management Development
"Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
These are my opinions--Bay Networks takes no responsibility for them.
------------------------------
Date: Wed, 21 May 1997 21:42:15 -0700
From: Richard_Kennedy@ci.des-moines.wa.us (Richard Kennedy)
Subject: REGEXP Help Needed
Message-Id: <9705212142.AA15068@desmoine.aa.net>
Keywords: regexp help
The following regexp attempts to check for a valid e-mail address:
if ($in{'from'} !~ m/^[^\s\n@]+\@(([a-z0-9][a-z0-9-]*[a-z0-9]\.)+[a-z]{2,3}|\
[(\d{1,3}\.){3}\d{1,3}\])$/) {
# If the E-mail address is wrong, return an error message
.
.
.
However, it incorrectly rejects the following valid e-mail address:
student@u.washington.edu
Any suggestions for correcting the above problem will be appreciated!
Richard_Kennedy@ci.des-moines.wa.us
------------------------------
Date: 22 May 1997 01:15:27 GMT
From: Ronald.J.Kimball@dartmouth.edu (Chipmunk)
Subject: Re: Regular expression problem
Message-Id: <5m06nf$pht$1@dartvax.dartmouth.edu>
In article <slrn5o55d0.57f.gossamer@amarok.glasswings.com.au>
gossamer@glasswings.com.au (Bek Oberin) writes:
> Excuse me for being dim (it's my best act :) but could you explain the
> lookaheads? It's not something I've come across before and not in my
> (old) Camel - another Perl5 extension? I would have also expected the
> secondlast line to end with $4 instead of $2 but I'll assume that's part
> of the same explanation..
I have the old Camel book too, but I found information about all the
nifty new expansions to regexps in the perlre man pages.
$2 is used instead of $4 because (?!whatever) doesn't assign to the $N
variables.
Chipmunk
------------------------------
Date: 22 May 1997 02:46:35 GMT
From: Matthew Darwin <matthew@spam.davin.ottawa.on.ca>
Subject: script to parse from lines?
Message-Id: <5m0c2b$ed8$1@bear.davin.ottawa.on.ca>
Is there a script out there to parse "From:" lines?
Basically I would like a script to parse things like:
Joe User <e-mail@site.com>
"Joe H. User" <e-mail@site.com>
'"Joe H. User"' <e-mail@site.com>
"Joe H. User's House" <e-mail@site.com>
e-mail@site.com
e-mail@site.com (Joe A. User)
person%site.com@some-remailer.com (Joe User)
site.com!e-mail
etc...
And work like this:
sub from_line {
my ($line) = @_
my ($name, $address);
...........
return ($name, $address);
}
Is there anything already out there, or do I need to write something
from scratch? I checked CPAN, but couldn't find anything. Seems like
something that lots of people could use.......
Thanks in advance,
--
Matthew Darwin
matthew@spam.davin.ottawa.on.ca
remove "spam" from e-mail address to reply.
------------------------------
Date: 22 May 1997 03:05:33 GMT
From: "Eric D. Friedman" <friedman@uci.edu>
Subject: Re: syntax for subroutine reference with sort
Message-Id: <5m0d5t$ji5@news.service.uci.edu>
[Mailed, posted]
In article <Pine.GSO.3.96.970521133951.735L-100000@kelly.teleport.com>,
Tom Phoenix <rootbeer@teleport.com> wrote:
> sort { &$my_sub_ref } @list;
But using a typeglob is still more efficient, no? I asked the same
question about two months ago (which the original poster would know,
had he bothered to check dij`news), and the general consensus was
that Tom's solution was not the best for this reason.
Here are the relevant articles, fyi.
Note that the second article shows how to use a local typeglob and
includes >gasp< a reference to the free documentation.
Eric
Date: Mon, 31 Mar 1997 02:22:01 GMT
From: aml@world.std.com (Andrew M. Langmead)
>From aml@world.std.com Sun Mar 30 23: 04:55 PST 1997
Article: 72048 of comp.lang.perl.misc
Newsgroups: comp.lang.perl.misc
Organization: The World @ Software Tool & Die
Subject: Re: Why is no subscripting allowed in custom sort routines?
friedman@medusa.acs.uci.edu (Eric D. Friedman) writes:
>Consequently the following does not work:
>use strict;
>my @foo = qw( cat mouse dog fish bird squirrel zebra );
>my $routine = sub { $a cmp $b };
>my @sorted_foo = sort &$routine @foo;
>My question: how can I write an anonymous sort subroutine that is NOT
>an in-line BLOCK? I want to be able to create a reference to a default
>sort routine which I could then change if one condition or another is
>met.
One suggestion is:
my @sorted_foo = sort { &$routine } @foo;
But it is a little slower because there is an extra subroutine
call. (The block calls the subroutine by reference)
In "Programming Perl" 2ed, it says that the scalar can contain a
reference to a subroutine, so maybe its just another case of the
implementation needing to catch up to the documentation, and someday
you will be able to just say
my @sorted_foo = sort $routine @foo;
Another idea is to keep the name of the sort subroutine fixed, and
eval a string that creates a subroutine of that name at runtime.
$sortsub = $opt_s || '$a cmp $b';
eval "sub by_specified_criteria { $sortsub }";
die $@ if $@;
foreach (sort by_specified_criteria @dirs) {
# do stuff
}
Even your idea of eval'ing the entire sort statement could be made a
little easier by taking the sorting out of the for loop.
if ($opt_s) {
@sorted_dirs = eval "sort $opt_s \@dirs";
}
else {
@sorted_dirs = sort @dirs; # same as 'sort { $a cmp $b } @dirs'
}
foreach (@sorted_dirs) {
# do stuff
}
--
Andrew Langmead
Date: 30 Mar 1997 23:27:37 PST
From: mconst@soda.CSUA.Berkeley.EDU (Michael Constant)
>From mconst@soda.CSUA.Berkeley.EDU Mon Mar 31 09: 35:45 PST 1997
Article: 72070 of comp.lang.perl.misc
Newsgroups: comp.lang.perl.misc
Organization: Society for the Prevention of Cruelty to Vegetables, UC Berkeley
NNTP-Posting-Host: soda.csua.berkeley.edu
Subject: Re: Why is no subscripting allowed in custom sort routines?
Eric D. Friedman <friedman@medusa.acs.uci.edu> wrote:
> my $routine = sub { $a cmp $b };
> my @sorted_foo = sort &$routine @foo;
>
>My question: how can I write an anonymous sort subroutine that is NOT
>an in-line BLOCK?
Give your anonymous subroutine a name, for a while:
local *anonymous = $routine;
sort anonymous @foo;
See perlmod(1) under "Symbol Tables" for an explanation of what it
means to assign to a typeglob.
--
Michael Constant (mconst@soda.csua.berkeley.edu)
--
Eric D. Friedman
friedman@uci.edu
------------------------------
Date: 22 May 1997 02:07:01 GMT
From: mike@stok.co.uk (Mike Stok)
Subject: Re: What do you do in place of 'case' or 'switch'
Message-Id: <5m09o5$cnp@news-central.tiac.net>
In article <33838D4C.4E9A@roguewave.com>,
Steve Moody <moody@roguewave.com> wrote:
>I see that there is not a "case" or "switch" statement
>in Perl. That puzzles me since that is a fairly common construct
>in other languages. I figure that there must be an equivalent that
>I am overlooking or am unaware of.
>
>What I am needing to do is to parse out a series of command
>line options, and based on their value enact a specific routine
>within my program. In C this was handled nicely with "switch"
>statements in ksh with "case" statements.
>
>So what do yall do in Perl? Hmm...?
>I am hoping that some wonderous nugget awaits and that
>my fears of "if" ladders are unjustified.
If you have a recent perl or the camel book then the perlsyn man page or
the page referred to by the "case structure" index entry will lead you to
something like this shamelessly stolen chunk:
The BLOCK construct is particularly nice for doing case
structures.
SWITCH: {
if (/^abc/) { $abc = 1; last SWITCH; }
if (/^def/) { $def = 1; last SWITCH; }
if (/^xyz/) { $xyz = 1; last SWITCH; }
$nothing = 1;
}
There is no official switch statement in Perl, because
there are already several ways to write the equivalent.
In addition to the above, you could write
SWITCH: {
$abc = 1, last SWITCH if /^abc/;
$def = 1, last SWITCH if /^def/;
$xyz = 1, last SWITCH if /^xyz/;
$nothing = 1;
}
(That's actually not as strange as it looks once you
realize that you can use loop control "operators" within
an expression, That's just the normal C comma operator.)
or
SWITCH: {
/^abc/ && do { $abc = 1; last SWITCH; };
/^def/ && do { $def = 1; last SWITCH; };
/^xyz/ && do { $xyz = 1; last SWITCH; };
$nothing = 1;
}
or formatted so it stands out more as a "proper" switch
statement:
SWITCH: {
/^abc/ && do {
$abc = 1;
last SWITCH;
};
/^def/ && do {
$def = 1;
last SWITCH;
};
/^xyz/ && do {
$xyz = 1;
last SWITCH;
};
$nothing = 1;
}
or
SWITCH: {
/^abc/ and $abc = 1, last SWITCH;
/^def/ and $def = 1, last SWITCH;
/^xyz/ and $xyz = 1, last SWITCH;
$nothing = 1;
}
or even, horrors,
if (/^abc/)
{ $abc = 1 }
elsif (/^def/)
{ $def = 1 }
elsif (/^xyz/)
{ $xyz = 1 }
else
{ $nothing = 1 }
A common idiom for a switch statement is to use foreach's
aliasing to make a temporary assignment to $_ for
convenient matching:
SWITCH: 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'";
}
Another interesting approach to a switch statement is
arrange for a do block to return the proper value:
$amode = do {
if ($flag & O_RDONLY) { "r" }
elsif ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" }
elsif ($flag & O_RDWR) {
if ($flag & O_CREAT) { "w+" }
else { ($flag & O_APPEND) ? "a+" : "r+" }
}
};
Hope this helps,
Mike
--
mike@stok.co.uk | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ | PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/ | 65 F3 3F 1D 27 22 B7 41
stok@psa.pencom.com | Pencom Systems Administration (work)
------------------------------
Date: Wed, 21 May 1997 21:00:27 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: What do you do in place of 'case' or 'switch'
Message-Id: <rb90m5.344.ln@localhost>
Steve Moody (moody@roguewave.com) wrote:
: I see that there is not a "case" or "switch" statement
: in Perl. That puzzles me since that is a fairly common construct
: in other languages. I figure that there must be an equivalent that
: I am overlooking or am unaware of.
: What I am needing to do is to parse out a series of command
: line options, and based on their value enact a specific routine
: within my program. In C this was handled nicely with "switch"
: statements in ksh with "case" statements.
: So what do yall do in Perl? Hmm...?
: I am hoping that some wonderous nugget awaits and that
: my fears of "if" ladders are unjustified.
if-elsif-else chains are one way ;-)
: I am sure that you have dealt with this before and if the
^^^^ ^^^^^^^^^^^^^^^^^^^^^^
Wonderous nuggets are awaiting all new Perl programmers.
They are all (well, OK, many anyway) collected in one place!
The Perl FAQ.
A word search for 'switch' in the Perl FAQs leads me to this
in part 7:
--------------------------
=head2 How do I create a switch or case statement?
This is explained in more depth in the L<perlsyn>. Briefly, there's
no official case statement, because of the variety of tests possible
in Perl (numeric comparison, string comparison, glob comparison,
regexp matching, overloaded comparisons, ...). Larry couldn't decide
how best to do this, so he left it out, even though it's been on the
wish list since perl1.
Here's a simple example of a switch based on pattern matching. We'll
do a multi-way conditional based on the type of reference stored in
$whatchamacallit:
SWITCH:
for (ref $whatchamacallit) {
/^$/ && die "not a reference";
/SCALAR/ && do {
print_scalar($$ref);
last SWITCH;
};
/ARRAY/ && do {
print_array(@$ref);
last SWITCH;
};
--------------------------
There's another way.
If you follow that reference to the 'perlsyn' man page, you'll
see a few more ways...
: question appears newbie-ish... well it is. I have my pen and paper
: ready (or rather my keyboard and screen) please share some
^^^^^
: of your wisdom with me. And if I must be resigned to a
^^^^^^
: "if" hell then hopefully some of you can make the pain more
: bearable.
Use the docs, Luke.
: In any case thanks in advance for your help and assistance
: please email me to the address below in addition to posting
: to the group.
: Thanks one and all
Thanks to all those who took the time to collect all that
accumulated Perl wisdom for us to use.
Wish it got more use though...
--
Tad McClellan SGML Consulting
Tag And Document Consulting Perl programming
tadmc@flash.net
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V8 Issue 514
*************************************