Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

smart solution

by Sun751 (Beadle)
on Jun 26, 2009 at 01:56 UTC ( [id://774884]=perlquestion: print w/replies, xml ) Need Help??

Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

I am writing Perl script, and my code is working well with correct out put, But i am wondering if there is some logical way to reduce lines of code doing same process in below code,
sub dispatch_closing { my ($line,$fw,$key,$HR_record,$os)= @_; if ($line =~ /SERVICE_APPLICATION_EXECUTION_COMPONENT) { foreach my $addline(keys %$HR_record) { print $fw "\tENVIRONMENT_VARIABLE Id=\"$addline\">$$HR_ +record{$addline}ENVIRONMENT_VARIABLE>\n"; } $$key = undef; } if ($line =~ /WEB_APPLICATION_EXECUTION_COMPONENT/) { foreach my $addline(keys %$HR_record) { print $fw "\tENVIRONMENT_VARIABLE Id=\"$addline\"$$HR_r +ecord{$addline}ENVIRONMENT_VARIABLE\n"; } $$key = undef; } if ($line =~ /UNX/) { foreach my $addline(keys %$HR_record) { if ($os eq 'unx') { my $Uval = $$HR_record{$addline}; $Uval = substitute($Uval); print $fw "\tRESOURCE Id=\"$addline\" Source=$Uval RE +SOURCE>}\n"; } } $$key = undef; } if ($line =~ /WNT/) { foreach my $addline(keys %$HR_record) { if ($os eq 'win') { my $Wval = $$HR_record{$addline}; $Wval = substitute($Wval); print $fw "\tRESOURCE Id=\"$addline\" Source=$addline + RESOURCE}\n"; } } $$key = undef; } }
Any one have any suggestion, please Cheers

Replies are listed 'Best First'.
Re: smart solution
by ELISHEVA (Prior) on Jun 26, 2009 at 03:52 UTC
    • How are you intending to use $$key = undef? What are you passing to it? In the actual subroutine is there code after all these if's? You do not appear to be using the value of $key (or $$key) anywhere in the function.

      Presumably you are passing something like \$key to this function with the intent of setting it? Is this a necessity because of the calling code? (from the name, it appears to be some sort of callback). If a necessity, you might want to document that in your code or choose a variable name that lets the reader know that $key is a reference to something of interest to the caller, and not the value itself.

      But if it is not a necessity, I would consider a more idiomatic (for Perl) way of getting the value assigned to $key back to the caller: just return the value you want to assign to the key and use an assignment in the calling code:$x=dispatch_closing(...).

      The effect of $$key=undef as the final statement of each if is to make the subroutine return the value assigned to $$key anyway - Perl always returns the value of the last executed statement. If you mean to return undef, then just do it, return undef. The intent is much clearer.

    • Are these if statements meant to be mutually exclusive, i.e. if one succeeds the others should be ignored? If so, you might want to consider using if...elsif...else. See perlintro for an example. (For some bizarre reason the possibility of doing this is not clearly explained in perlsyn). Using if...elsif...else will cause Perl to skip past all of those regexes once the matching one is found. As it stands, you are evaluating all of the regexes, even after you find the first match, e.g.
      if ( some condition ) { } elsif ( some other conditional ) { } elsif ( yet another conditional ) { } else { }

    Best, beth

Re: smart solution
by mzedeler (Pilgrim) on Jun 26, 2009 at 07:38 UTC

    Besides a single >, the thing that the code does in the SERVICE_APPLICATION_EXECUTION_COMPONENT and WEB_APPLICATION_EXECUTION_COMPONENT are identical (as far as I can see). Ensure that the > is really needed there and consider merging the two blocks.

    Having the if($os eq 'unx') inside foreach blocks is not a good idea, since the condition is loop-invariant, which means that the outcome of the check inside the ifs won't change, depending on the values of the iterator (foreach). It should be rewritten as:

    if(...) { foreach ... (...) { ... } }

    And again, the two blocks checking for UNX and WNT are nearly identical. See if you can merge them. Redundant redundant code is redundant redundant is redundant.

Re: smart solution
by Anonymous Monk on Jun 26, 2009 at 04:21 UTC
    In addition to the comments by Beth, the following line should be corrected:
    if ($line =~ /SERVICE_APPLICATION_EXECUTION_COMPONENT)
    to
    if ($line =~ /SERVICE_APPLICATION_EXECUTION_COMPONENT/)
Re: smart solution
by grizzley (Chaplain) on Jun 26, 2009 at 09:12 UTC

    Few improvement ideas. Untested:

    sub dispatch_closing { my ($line,$fw,$key,$HR_record,$os)= @_; my $brace = ($line =~ /SERVICE_APPLICATION_EXECUTION_COMPONENT|UNX +/ ? '>' : ''); if ($line =~ /SERVICE_APPLICATION_EXECUTION_COMPONENT|WEB_APPLICAT +ION_EXECUTION_COMPONENT/) { while(my($k, $v)=each %$HR_record) { print $fw "\tENVIRONMENT_VARIABLE Id=\"$k\">${v}ENVIRONM +ENT_VARIABLE$brace\n" } } if (($line =~ /UNX/ && $os eq 'unx') || ($line =~ /WNT/ && $os eq 'win')) { while(my($k, $v)=each %$HR_record) { print $fw "\tRESOURCE Id=\"$k\" Source=" . substitute($ +v) . " RESOURCE$brace}\n" } } if($line =~ /SERVICE_APPLICATION_EXECUTION_COMPONENT|WEB_APPLICATI +ON_EXECUTION_COMPONENT|UNX|WNT/) { $$key = undef } }
Re: smart solution
by poolpi (Hermit) on Jun 26, 2009 at 12:39 UTC

    Not tested

    sub dispatch_closing { my ( $line, $fh, $HR_record ) = @_; return unless $line =~ /((?:SERVICE|WEB)_APPLICATION_EXECUTION_COMPONENT)|(UNX|WNT)/; my $sep = q{>}; my $env = 'ENVIRONMENT_VARIABLE'; my $res = 'RESOURCE'; if ( defined $1 ) { $sep = ( $1 =~ /SERVICE/ ) ? q{>} : q{}; printf( $fh, "\t%s Id=\"%s\"%c%s%s%c\n", , $env, $_, $sep, $HR_record->{$_}, $env ) for keys %$HR_record; } else { $sep = ( $2 =~ /UNX/ ) ? q{>} : q{}; printf( $fh, "\t%s Id=\"%s\" Source= %s %s%c}\n", $res, $_, substitute( $HR_record->{$_} ), $res, $sep ) for keys %$HR_record; } }


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://774884]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-25 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found