Reuse of componants 
Author Message
 Reuse of componants

Dear all,

I would like to know if there is a proper way to recover subroutines coded
in other perl files rather than copy-paste them in my new perl file. For
instance, imagine I have written scripts A2B.pl and A2C.pl that converts
some data file from format A to format B or C. Now I want to create the
super A2any.pl that lazily but cleverly reuse all the good subs of the
previous scripts. At the same time I want A2B and A2C to continue to work as
independant programs. How to do that?

For those who know python, I am searching something similar to the if
__name__ == "__main__" trick...

Thanks

--
Remove the mammal in my address to answer me



Tue, 02 Nov 2004 15:20:27 GMT  
 Reuse of componants

Quote:

> I would like to know if there is a proper way to recover subroutines
> coded in other perl files rather than copy-paste them in my new perl
> file. For instance, imagine I have written scripts A2B.pl and A2C.pl
> that converts some data file from format A to format B or C. Now I
> want to create the super A2any.pl that lazily but cleverly reuse all
> the good subs of the previous scripts. At the same time I want A2B
> and A2C to continue to work as independant programs. How to do that?

> For those who know python, I am searching something similar to the
> if __name__ == "__main__" trick...

More or less the same trick:

    if ($0 eq __FILE__) { ...

And then you can do($filename) or require($filename) to include
the other scripts.

OTOH it would be better form to put your reusable subs in a module.

Rip the subs out of A2A.pl and A2B.pl, put them in A2.pm, and then
update all three scripts to use() that module.

HTH
--
Steve

perldoc -qa.j | perl -lpe '($_)=m("(.*)")'



Tue, 02 Nov 2004 15:53:14 GMT  
 Reuse of componants

Quote:

> Dear all,

> I would like to know if there is a proper way to recover subroutines
> coded in other perl files rather than copy-paste them in my new perl
> file. For instance, imagine I have written scripts A2B.pl and A2C.pl
> that converts some data file from format A to format B or C. Now I want
> to create the super A2any.pl that lazily but cleverly reuse all the good
> subs of the previous scripts. At the same time I want A2B and A2C to
> continue to work as independant programs. How to do that?

> For those who know python, I am searching something similar to the if
> __name__ == "__main__" trick...

The best way it to extract the reusable functions into a module.

See "perldoc perlmod".

Dave...

--
  Don't dream it... be it



Tue, 02 Nov 2004 15:59:31 GMT  
 Reuse of componants

Quote:


>>I would like to know if there is a proper way to recover subroutines
>>coded in other perl files rather than copy-paste them in my new perl
>>file. For instance, imagine I have written scripts A2B.pl and A2C.pl
>>that converts some data file from format A to format B or C. Now I
>>want to create the super A2any.pl that lazily but cleverly reuse all
>>the good subs of the previous scripts. At the same time I want A2B
>>and A2C to continue to work as independant programs. How to do that?

> [...] put your reusable subs in a module.

> Rip the subs out of A2A.pl and A2B.pl, put them in A2.pm, and then
> update all three scripts to use() that module.

I agree with that approach, *if* there are any other programs that might
want access to the conversion functionality. If it's only going to be
these three, I'd consider a different approach:

Merge the functionality into A2any.pl, and define the interface so that
it can do either A2B or A2C depending on how it's called. Then make
A2B.pl and A2C.pl wrapper scripts that simply call A2any.pl with the
appropriate arguments.

Of course, there's nothing that says you can't do both -- Put the common
stuff in a module used by A2any.pl and still make A2B.pl and A2C.pl
wrappers that call it.

This doesn't get around cutting and pasting, but sometimes you have to
get your hands dirty to clean things up.

-mjc



Tue, 02 Nov 2004 16:52:47 GMT  
 Reuse of componants
I could not easily get modules working across Windows, where I do
development, and various Unix paltforms.  I do something non-standard.

I have several named common sections across my code.  The names are
formatted comments that start and end the section.  I then run a program to
scan the programs, list them, and mark which sections differ.  A click then
updates the sections with what I choose.

It is more complicated to describe than it is to use.

It also has the *major* advantage that all the source used by any program is
in that file, enabling me to do edits and searches on that file only.

--

Dennis Reinhardt


http://www.dair.com



Tue, 02 Nov 2004 19:38:21 GMT  
 Reuse of componants

Quote:

>I could not easily get modules working across Windows, where I do
>development, and various Unix paltforms.  I do something non-standard.

>I have several named common sections across my code.  The names are
>formatted comments that start and end the section.  I then run a program to
>scan the programs, list them, and mark which sections differ.  A click then
>updates the sections with what I choose.

>It is more complicated to describe than it is to use.

>It also has the *major* advantage that all the source used by any program is
>in that file, enabling me to do edits and searches on that file only.

>--

>Dennis Reinhardt


>http://www.dair.com

You wouldn't consider *publishing* that program/scheme/examples
to this newsgroup, would you?

Perhaps as a followup to this very thread?

Sounds pretty cool.

David

PS: Oh, by the way, how does this differ in
ease-of-use, power, etc, in a language (poor, poor
perl!) that has a real macro and conditional-assembly
feature?

Or maybe by some scheme using m4?

(Of course, your own solution looks pretty good.)



Mon, 22 Nov 2004 22:18:18 GMT  
 Reuse of componants

Quote:



>>I could not easily get modules working across Windows, where I do
>>development, and various Unix paltforms.

Strange, seeing as how easy that is.

Quote:
>Sounds pretty cool.

No it doesn't.  It sounds like a messy, kludgy, hack.

--
Regards, Helgi Briem
helgi AT decode DOT is



Tue, 23 Nov 2004 12:12:54 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. non TK GUI componants for Win95?

2. Non-reuse of memory for lexically-scoped variables?

3. Reusing array refs

4. How to reuse sub-regex / name a regex ?

5. Reusing the same subroutine

6. LWP - https and reuse of session -ID

7. Object Reuse with Net::FTP

8. reused functions.

9. REUSE CODE (BEGINNER LEVEL)

10. reusing a HTTP connection

11. CPAN entries added to Reuse Encyclopedia

12. perl+Postgres+apache+asp : can't reuse a scalar

 

 
Powered by phpBB® Forum Software