Click here to get back home

strategys other than subroutine and OO?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
strategys other than subroutine and OO? Ela 03-20-2008
Posted by szr on March 25, 2008, 3:11 am
Please log in for more thread options
Willem wrote:
> Peter wrote:
> )> Actually it would have to read like:
> )>
> )> while (<>) {
> )> some_code();
> )> if ($x) {
> )> more_code();
> )> }
> )> elsif ($x && $y) {
> )> even_more_code();
> )> }
> )> elsif ($x && $y && $z) {
> )> if_all_goes_well();
> )> }
> )> }

See below...


> )> in order to equate to:
> )>
> )> while (<>) {
> )> some_code();
> )> if ($x) {
> )> more_code();
> )> if ($y) {
> )> even_more_code();
> )> if ($z) {
> )> if_all_goes_well();
> )> }
> )> }
> )> }
> )> }
> )>
> )
> ) Nope. Anyone up for a third try?
> )
> ) hp
>
> Assuming more_code and even_more_code have no effect on $y and $z:
> (Which is an unwarranted assumption, by the way)
>
> while (<>) {
> some_code();
> if ($x && not $y) {
> more_code();
> }
> elsif ($x && not $z) {
> more_code();
> even_more_code();
> }
> elsif ($x) {
> more_code();
> even_more_code();
> if_all_goes_well();
> }
> }
>
> Which is quite silly, of course.

Actually I should of written it as:

while (<>) {
some_code();
if ($x) {
more_code();
}
if ($x && $y) {
even_more_code();
}
if ($x && $y && $z) {
if_all_goes_well();
}
}

That way it falls through giving the same effect as the original and is
possibly a bit easier to read.

--
szr



Posted by Joost Diepenmaat on March 20, 2008, 8:20 am
Please log in for more thread options

> I have 300 lines of codes that are used roughly twice. Some of the lines
> have delicate difference in execution (e.g. ~6 variables have to be replaced
> and conditional statements are also different) and therefore many arguments
> need to be passed into it in order to differentiate the execution parts. OO
> development is analogously also difficult. I wonder if there is any good way
> to reuse the codes, e.g. some sort of "goto" rather than duplicating these
> lines. Whenever there is any change, I find that is really error-prone.

Sounds like using closures/passing functions as arguments could be very
helpful here, but without seeing your code it's impossible to be sure.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by Willem on March 20, 2008, 8:22 am
Please log in for more thread options
Ela wrote:
) I have 300 lines of codes that are used roughly twice. Some of the lines
) have delicate difference in execution (e.g. ~6 variables have to be replaced
) and conditional statements are also different) and therefore many arguments
) need to be passed into it in order to differentiate the execution parts. OO
) development is analogously also difficult. I wonder if there is any good way
) to reuse the codes, e.g. some sort of "goto" rather than duplicating these
) lines. Whenever there is any change, I find that is really error-prone.

Maybe you can take the lines that are exactly duplicated and put those
into sub functions. If you define the sub functions inside the main
function they can access the main function's 'my' variables, so you
don't need to pass them as arguments.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Posted by Ela on March 20, 2008, 9:00 am
Please log in for more thread options
> Maybe you can take the lines that are exactly duplicated and put those
> into sub functions. If you define the sub functions inside the main
> function they can access the main function's 'my' variables, so you
> don't need to pass them as arguments.
>

From perlintro:
A Perl script or program consists of one or more statements. These
statements are simply written in the script in a straightforward fashion.
There is no need to have a main() function or anything of that kind.

So would you mind giving a very simple example? i guess your suggestion is
very close to the final solution since i never have the concept you suggest.



Posted by Tad J McClellan on March 20, 2008, 9:13 pm
Please log in for more thread options
>> Maybe you can take the lines that are exactly duplicated and put those
>> into sub functions. If you define the sub functions inside the main
>> function they can access the main function's 'my' variables, so you
>> don't need to pass them as arguments.
>>
>
> From perlintro:
> A Perl script or program consists of one or more statements. These
> statements are simply written in the script in a straightforward fashion.
> There is no need to have a main() function or anything of that kind.


The main referred to in perlintro is not the main in Ela's followup.

The main function that perlintro mentions is a concept
common is several other programming languages.

Ela's main function is referring to the "parent" or "outer" functions,
that is, it is a colloquial use of the word "main" rather than a
technical use.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Similar ThreadsPosted
help me pass argument to the subroutine and then return the value from that subroutine to another. October 14, 2006, 1:35 am
A subroutine for gcd July 17, 2006, 9:38 am
subroutine July 20, 2006, 5:36 pm
"Undefined subroutine" November 21, 2004, 8:48 pm
How to tell if a subroutine arg is a constant February 24, 2005, 9:13 am
Subroutine Function March 3, 2005, 5:14 pm
subroutine explanation April 22, 2005, 8:40 am
problam in subroutine? December 5, 2005, 1:40 pm
calling subroutine January 3, 2006, 6:35 pm
subroutine doesn't seem to run after calling it. May 9, 2006, 4:04 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap