Referencing Control Operators

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

  1. #1

    Default Referencing Control Operators


    Greetings.

    Is it possible to reference a control operator?

    For example, I'd like to take this code:


    sub getFieldFromAllRecords {
    my ($self, $directive, $keyword, $matchCondition) = @_;

    my ($field, $regArr);

    # another public method within same pkg:
    my $allRecs = dumpAllAudits($self);
    my %ar = %$allRecs;

    push @{$regArr}, qr/$keyword/i;

    my %select;
    while( my($key, $value) = each(%ar)) {
    map { next unless ($$value{$directive} =~ /$_/i); } @{$regArr};
    $select{$key} = $$value{$directive};
    }

    return \%select;
    }

    and change it such that the 'unless' conditional operator is
    referenced conceptually similar to the following:

    sub getFieldFromAllRecords {
    my ($self, $directive, $keyword, $matchCondition) = @_;

    my $mc = $matchCondition; # changed: where $mc can be either
    # 'if' or 'unless'.
    my ($field, $regArr);

    # another public method within same pkg:
    my $allRecs = dumpAllAudits($self);
    my %ar = %$allRecs;

    push @{$regArr}, qr/$keyword/i;

    my %select;
    while( my($key, $value) = each(%ar)) {
    map { next $mc ($$value{$directive} =~ /$_/i); } # changed
    @{$regArr};
    $select{$key} = $$value{$directive};
    }

    return \%select;
    }

    If it's not possible to reference operators, I'll rework the code.

    Thanks in advance.
    jab
    John Baker Guest

  2. Similar Questions and Discussions

    1. Referencing a parent control from a child?
      I have two UserControls I'd like to have talk to each other. One of them is contained within the other, in a parent/child relationship. The child...
    2. Referencing Page properties from child control
      I'm declaring public properties in a Page's code behind file (not declaratively). I would like to read and assign these from a child control's cs...
    3. Referencing a TextBox control in Javascript from DataList ItemTemplate
      I have a calendar control which pops up as a new window when i click on my textbox. The calendar control returns a Javascript function whereby...
    4. Referencing Custom Page Class within a Control
      C# code. MyPageClass myPage = ( MyPageClass ) this.Page; myPage now is the instance of MyPageClass the control on. bill
    5. Referencing a control in Javascript
      Hi all, In my code-behind, I validate some controls and if a particular control has a problem, I not only send an alert down to the client via...
  3. #2

    Default Re: Referencing Control Operators

    At 12:12 PM 1/22/2004, John Baker wrote:
    >Greetings.
    >
    >Is it possible to reference a control operator?
    >
    >For example, I'd like to take this code:
    >
    >
    > sub getFieldFromAllRecords {
    > my ($self, $directive, $keyword, $matchCondition) = @_;
    >
    > my ($field, $regArr);
    >
    > # another public method within same pkg:
    > my $allRecs = dumpAllAudits($self);
    > my %ar = %$allRecs;
    >
    > push @{$regArr}, qr/$keyword/i;
    >
    > my %select;
    > while( my($key, $value) = each(%ar)) {
    > map { next unless ($$value{$directive} =~ /$_/i); } @{$regArr};
    > $select{$key} = $$value{$directive};
    > }
    >
    > return \%select;
    > }
    >
    >and change it such that the 'unless' conditional operator is
    >referenced conceptually similar to the following:
    >
    > sub getFieldFromAllRecords {
    > my ($self, $directive, $keyword, $matchCondition) = @_;
    >
    Rather than trying to write code into variables or self-modifying code, I think I would try defining some constants:

    use constant MATCH_IF => 0;
    use constant MATCH_UNLESS => 1;

    Then assign $mc to MATCH_IF or MATCH_UNLESS any way you like

    Then use xor:

    map { next if ( ($$value{$directive} =~ /$_/i) xor $mc); } @{$regArr};

    This is untested, but you get the idea.

    -Mark


    > my $mc = $matchCondition; # changed: where $mc can be either
    > # 'if' or 'unless'.
    > my ($field, $regArr);
    >
    > # another public method within same pkg:
    > my $allRecs = dumpAllAudits($self);
    > my %ar = %$allRecs;
    >
    > push @{$regArr}, qr/$keyword/i;
    >
    > my %select;
    > while( my($key, $value) = each(%ar)) {
    > map { next $mc ($$value{$directive} =~ /$_/i); } # changed
    > @{$regArr};
    > $select{$key} = $$value{$directive};
    > }
    >
    > return \%select;
    > }
    >
    >If it's not possible to reference operators, I'll rework the code.
    >
    >Thanks in advance.
    >jab
    >
    >--
    >To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    >For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    ><http://learn.perl.org/> <http://learn.perl.org/first-response>

    Mark Lobue Guest

  4. #3

    Default Re: Referencing Control Operators


    On Jan 22, 2004, at 4:32 PM, Mark LoBue wrote:
    > At 12:12 PM 1/22/2004, John Baker wrote:
    [..]
    >> and change it such that the 'unless' conditional operator is
    >> referenced conceptually similar to the following:
    >>
    >> sub getFieldFromAllRecords {
    >> my ($self, $directive, $keyword, $matchCondition) = @_;
    >>
    >
    > Rather than trying to write code into variables or self-modifying
    > code, I think I would try defining some constants:
    >
    > use constant MATCH_IF => 0;
    > use constant MATCH_UNLESS => 1;
    >
    > Then assign $mc to MATCH_IF or MATCH_UNLESS any way you like
    >
    > Then use xor:
    >
    > map { next if ( ($$value{$directive} =~ /$_/i) xor $mc); } @{$regArr};
    >
    > This is untested, but you get the idea.
    Fashionable and stylish as ever...

    But it has the problem that it will cause a runtime error.
    The following I think may help the OP:

    use constant MATCH_IF => 0;
    use constant MATCH_UNLESS => 1;

    my @list = qw/foo Foo bar baz/;

    my @foo = if_unless('foo',MATCH_IF,@list);

    my @not_foo = if_unless('foo',MATCH_UNLESS,@list);

    print "foo got $_\n" foreach(@foo);
    print "not_foo got $_\n" foreach(@not_foo);

    #------------------------
    #
    sub if_unless
    {
    my ($token, $mc, @list) = @_;

    grep { $_ if ( ($token =~ /$_/i) xor $mc) } @list;

    } # end of if_unless

    generates:

    foo got foo
    foo got Foo
    not_foo got bar
    not_foo got baz

    whereas changing the 'grep' to a 'map' will generate:

    foo got foo
    foo got Foo
    foo got
    foo got
    not_foo got
    not_foo got
    not_foo got bar
    not_foo got baz

    so there is that point to take into account as to what one is
    really trying to do here.

    [..]
    >> my %ar = %$allRecs;
    >>
    >> push @{$regArr}, qr/$keyword/i;
    >>
    >> my %select;
    >> while( my($key, $value) = each(%ar)) {
    >> map { next $mc ($$value{$directive} =~ /$_/i); } #
    >> changed
    >> @{$regArr};
    >> $select{$key} = $$value{$directive};
    >> }
    [..]

    now the minor question, why assign the %ar? you could
    ahve done the while

    while( my($key, $value) = each %$allRecs) {

    minor nit.

    So what exactly were you planning to assign the output
    of that map to???

    ciao
    drieux

    ---

    Drieux 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