Seeking advice on selecting a sub (possibly/slightly related to cmd line switches)

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. seeking advice user interaction
      Tim: Have you tried Camtasia, Premiere or Windows Media Player/Movie Maker for this sequence? Thanks, rccm
    3. #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: ...
    4. #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: ...
    5. #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...
  3. #2

    Default 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:
    > 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__
    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
    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 <>;

    > 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;
    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).

    - --
    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

  4. #3

    Default 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:
    >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:
    [snip]
    > my $doit = $dispatch{$option};
    > $doit->($_) while <>;
    > # or even:
    > $dispatch{$option}($_) while <>;
    Well, to answer to you and to the others who kindly followed-up my
    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!)
    >> 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).
    Wow, I would have never thought it would be possible *now*. Even if I
    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139