subroutine explanation

Discussion in 'Perl Misc' started by fred, Apr 22, 2005.

  1. fred

    fred Guest

    I have some code created to takes some rules from external file and
    apply these rules on text file. My perl experience is somewhat limited
    and I was wondering if soneone on this group can give me an
    explanation of it.

    sub apply_rules {
    my ($_txt, $_rules) = @_;
    for (keys %{$_rules}) {
    my $exp = $_;
    $exp =~ s{\s}{\\s*}g;
    my $re = $_rules->{$_};
    my $rslt;
    eval ("\$rslt = \$\$_txt =~ s{$exp}{$re}gis");
    printf " >Replaced '$exp' %d times.\n", $rslt;
    }
    }
     
    fred, Apr 22, 2005
    #1
    1. Advertising

  2. fred

    Anno Siegel Guest

    fred <> wrote in comp.lang.perl.misc:
    > I have some code created to takes some rules from external file and
    > apply these rules on text file. My perl experience is somewhat limited
    > and I was wondering if soneone on this group can give me an
    > explanation of it.
    >
    > sub apply_rules {
    > my ($_txt, $_rules) = @_;
    > for (keys %{$_rules}) {
    > my $exp = $_;
    > $exp =~ s{\s}{\\s*}g;
    > my $re = $_rules->{$_};
    > my $rslt;
    > eval ("\$rslt = \$\$_txt =~ s{$exp}{$re}gis");
    > printf " >Replaced '$exp' %d times.\n", $rslt;
    > }
    > }


    $_rules is a (reference to) a hash that contains pairs consisting of a
    pattern and a replacement. $_txt is a reference to some text. Basically,
    the pattern is replaced with the replacement everywhere it matches
    (case-insensitively) in the text. This happens for every pair in the hash.
    Why the author thought "eval" was needed to achieve this isn't obvious,

    $rslt = $$_txt =~ s{$exp}{$re}gis;

    would do the same thing.

    Before application, each pattern is pre-processed to replace blanks with
    "\s*", which means that when the pattern indicates a single blank, any
    sequence of white space will match. That includes no whitespace at all,
    wonder if that's right.

    After each pattern/replacement pair in the hash, the pattern is shown
    along with the number of replacements that happened.

    The effect of the routine is not well defined if the order of replacements
    matters (if there are re-replacements), because the order of a hash
    isn't well defined.

    Anno
     
    Anno Siegel, Apr 22, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mariusz

    explanation

    Mariusz, Jan 12, 2004, in forum: VHDL
    Replies:
    1
    Views:
    619
    tbx135
    Jan 13, 2004
  2. Kaladhaur Palaniappa

    Need Explanation

    Kaladhaur Palaniappa, Aug 7, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,028
    Kaladhaur Palaniappa
    Aug 7, 2003
  3. Benjie Fallar

    tracing output explanation

    Benjie Fallar, Jul 15, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    373
    Benjie Fallar
    Jul 15, 2003
  4. George Ter-Saakov

    Do you have an explanation for this?

    George Ter-Saakov, Apr 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    619
    Pravin A. Sable
    Apr 30, 2004
  5. king
    Replies:
    5
    Views:
    210
Loading...

Share This Page