Ask a Question related to PERL Miscellaneous, Design and Development.
-
Michele Dondi #1
Seeking advice on selecting a sub (possibly/slightly related to cmd line switches)
I'm not sure the subject line is appropriate. Couldn't come up with
anything better...
Well, just to be definite (but not limited to the following example),
suppose I have to accomplish different tasks according to some cmd
line switches for a script. Say all of them involve 'while (<>)
{...}', so I do *not* want to test the switch for every iteration and
I do *not* want to repeat big portions of code either.
It seems to me that the "best" solution that doesn't clobber the main
logic of the program is something along the lines of the following
example. But I'm not sure if it is the "right(TM) thing" to do: can
you comment please?
#!/usr/bin/perl
# Oversimplified example
use strict;
use warnings;
use Term::ANSIColor qw/:constants/;
use Getopt::Std;
sub doit;
my %opt;
getopts 'c', \%opt;
if ($opt{'c'}) {
*doit = sub {
local $_=shift;
print if s/\b(foo)\b/RED.$1.RESET/ge
}
}
else {
*doit = sub {
local $_=shift;
print if /\bfoo\b/;
}
}
doit $_ while <>;
__END__
Also, as an aside, and since I'm very curious, having heard that
basically in Perl6 "everything will be an object", I wonder if it will
provide hooks for the beginning and the end of a sub, or possibly for
a label inside it, as in the following pseudo-code:
sub doit {
...
}
doit.END = {
print STDERR "$.: foo" if $foo;
} if $verbose;
Michele
--
$\=q.,.,$_=q.print' ,\g,,( w,a'c'e'h,,map{$_-=qif/g/;chr
}107..q[..117,q)[map+hex,split//,join' ,2B,, w$ECDF078D3'
F9'5F3014$,$,];];$\.=$/,s,q,32,g,s,g,112,g,y,' , q,,eval;
Michele Dondi Guest
-
Seeking advice for timer design
Hi, I am designing a timer application with Flex Builder 3 which is basically a stop watch that will remember when it was started if the page is... -
seeking advice user interaction
Tim: Have you tried Camtasia, Premiere or Windows Media Player/Movie Maker for this sequence? Thanks, rccm -
#24635 [Ver->Csd]: small block of code causes crash, possibly destructor related.
ID: 24635 Updated by: stas@php.net Reported By: eric at cosky dot com -Status: Verified +Status: ... -
#24635 [Opn->Ver]: small block of code causes crash, possibly destructor related.
ID: 24635 Updated by: sniper@php.net Reported By: eric at cosky dot com -Status: Open +Status: ... -
#24635 [NEW]: small block of code causes crash, possibly destructor related.
From: eric at cosky dot com Operating system: Windows XP SP1 PHP version: 5CVS-2003-07-13 (dev) PHP Bug Type: Reproducible... -
Eric J. Roode #2
Re: Seeking advice on selecting a sub (possibly/slightly related to cmd line switches)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Michele Dondi <bik.mido@tiscalinet.it> wrote in
news:8vvngvkocsj3nsrgeo2jum53o3aipe6j69@4ax.com:
That looks like it'd work. I think I personally would have done it> I'm not sure the subject line is appropriate. Couldn't come up with
> anything better...
>
> Well, just to be definite (but not limited to the following example),
> suppose I have to accomplish different tasks according to some cmd
> line switches for a script. Say all of them involve 'while (<>)
> {...}', so I do *not* want to test the switch for every iteration and
> I do *not* want to repeat big portions of code either.
>
> It seems to me that the "best" solution that doesn't clobber the main
> logic of the program is something along the lines of the following
> example. But I'm not sure if it is the "right(TM) thing" to do: can
> you comment please?
>
> #!/usr/bin/perl
> # Oversimplified example
> use strict;
> use warnings;
> use Term::ANSIColor qw/:constants/;
> use Getopt::Std;
>
> sub doit;
>
> my %opt;
> getopts 'c', \%opt;
> if ($opt{'c'}) {
> *doit = sub {
> local $_=shift;
> print if s/\b(foo)\b/RED.$1.RESET/ge
> }
> }
> else {
> *doit = sub {
> local $_=shift;
> print if /\bfoo\b/;
> }
> }
>
> doit $_ while <>;
> __END__
differently; I'd put a code reference into a scalar variable, $doit, and
then run the main loop as
$doit->($_) while <>;
You could do this via dispatch table:
sub something {...la la la ...}
sub anotherthing {... la la la ...}
sub morethings { ...la la la ...}
my %dispatch = (
one => \&something,
two => \&anotherthing,
three => \&morethings,
);
my $doit = $dispatch{$option};
$doit->($_) while <>;
# or even:
$dispatch{$option}($_) while <>;
You can do this in Perl 5. No need to wait for hell to freeze over,> Also, as an aside, and since I'm very curious, having heard that
> basically in Perl6 "everything will be an object", I wonder if it will
> provide hooks for the beginning and the end of a sub, or possibly for
> a label inside it, as in the following pseudo-code:
>
> sub doit {
> ...
> }
>
> doit.END = {
> print STDERR "$.: foo" if $foo;
> } if $verbose;
which is approximately when Perl 6 will be ready for prime time. See the
Hook::LexWrap module (although the wrappers are not "lexical" as the name
implies, but rather dynamic).
- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBPwyknmPeouIeTNHoEQIkWgCeK7k6eY9ydDezjP8Bk27xOx La3yAAoJNx
BNf6YlcL9eGs7j8nnAOiTRF1
=+nTa
-----END PGP SIGNATURE-----
Eric J. Roode Guest
-
Michele Dondi #3
Re: Seeking advice on selecting a sub (possibly/slightly related to cmd line switches)
On Wed, 09 Jul 2003 18:26:30 -0500, "Eric J. Roode"
<REMOVEsdnCAPS@comcast.net> wrote:
[snip]>That looks like it'd work. I think I personally would have done it
>differently; I'd put a code reference into a scalar variable, $doit, and[snip]>You could do this via dispatch table:Well, to answer to you and to the others who kindly followed-up my> my $doit = $dispatch{$option};
> $doit->($_) while <>;
> # or even:
> $dispatch{$option}($_) while <>;
post, from *my* point of view, all these alternative approaches are
substantially equivalent to my own. That is, I just wanted to know if
"assigning" a suitable coderef is a good way to implement "diversified
behaviours". Say I was quite confidedent this is in fact the case: I
just needed some confirmation...
I wrote "substantially" and not "completely" because actually I've not
played much with typeglobs and I couldn't be 100% sure there wouldn't
have been any issue with (wrt my original code)
sub doit;
...
*doit = sub { ... };
and if there are not, then I will continue using such a technique,
since I feel most comfortable with it as far as code readibility is
concerned. At this point it is largely a matter of personal taste: I
can understand that (e.g.) other people would prefer to tell
user-defined subs from builtin commands, but I prefer to always have
some "degree of freedom" with parentheses and be allowed to ignore
some. (with all my respect to LISP fans out there!)
Wow, I would have never thought it would be possible *now*. Even if I>>> sub doit {
>> ...
>> }
>>
>> doit.END = {
>> print STDERR "$.: foo" if $foo;
>> } if $verbose;
>You can do this in Perl 5. No need to wait for hell to freeze over,
>which is approximately when Perl 6 will be ready for prime time. See the
>Hook::LexWrap module (although the wrappers are not "lexical" as the name
>implies, but rather dynamic).
have a feeling it is a "workaround", I'll try it ASAP. However, as I
said, I'm curious, and in particular I'm curious about Perl6, because
of the various "snippets" I've heard about, and OTOH, you see, this is
the kind of thing one (well, "I", for one) would like to see as a
basic language feature.
Michele
--
$\=q.,.,$_=q.print' ,\g,,( w,a'c'e'h,,map{$_-=qif/g/;chr
}107..q[..117,q)[map+hex,split//,join' ,2B,, w$ECDF078D3'
F9'5F3014$,$,];];$\.=$/,s,q,32,g,s,g,112,g,y,' , q,,eval;
Michele Dondi Guest



Reply With Quote

