CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Calling subroutines from other files and sorting

 



benn600
User


Jun 16, 2005, 4:23 PM

Post #1 of 7 (1694 views)
Calling subroutines from other files and sorting Can't Post

My entire remake of my website is in one file and it is relatively large at ~ 290 kb. I have found that if I remove the admin section, it drops to ~ 147 kb, or basically half. A pretty big portion of this first section (everything other than admin) is important subs. I use subs for opening fines and splitting them, as well as adding page numbers to my pages. This allows me to just define some variables, like $allarticles = scalar(@..) and then use &pages; and it will automatically add page numbers. I would like to know if I can make an admin perl file (a different one) and then call my original subroutines and use them the same. I love having 5 subs for each file in one spot...open,split,rewrite,append,split...because if I need to add another variable to the split section, I just have to do it in two spots, which are right next to eachother. If I had to copy these into another file, it would be a pain. I might even make another file with these common subs and that might speed it up even more. My site easily doubled in speed by removing the admin section, even though none of it was used. Just being there slows it way down. I tried searching Google for this info, but didn't have much luck. I found something, but wasn't sure how to interpret it.

Sorting is another feature I would love to make as easy as pages, which requires to simple lines and I have automatic page numbers. My .txt files, which store my data, use the split command with | separating fields and I would like to know if I can sort based on one of these other than the first. The alternative is opening it and pushing it into a new @... in the correct order, but this is a real pain.

Thank you very much everyone!
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


davorg
Thaumaturge / Moderator

Jun 17, 2005, 5:51 AM

Post #2 of 7 (1691 views)
Re: [benn600] Calling subroutines from other files and sorting [In reply to] Can't Post

You should look at moving your common functions out into a module and using that module from both of your programs.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


benn600
User


Jun 17, 2005, 7:54 AM

Post #3 of 7 (1689 views)
Re: [davorg] Calling subroutines from other files and sorting [In reply to] Can't Post

There is no question that I would be willing to take suggestions. After many thousands of hours of work on this project, I really want to get these little details worked out and I don't want speed to be an issue. Unfortunately, I do not understand a lot of things that people have talked about. What is a module? Also, is there a way to call a sub from another file and is this a good idea? This is what I'd like to do because I could make a few files and it would speed things up quite a bit.
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


davorg
Thaumaturge / Moderator

Jun 17, 2005, 8:13 AM

Post #4 of 7 (1687 views)
Re: [benn600] Calling subroutines from other files and sorting [In reply to] Can't Post

A module is a reusable library of functions. Which sounds exactly like what you need. Any decent Perl tutorial will explain how to create them (for example, it's chapter 12 in "Learning Perl Objects, References & Modules" - which you can read online at Safari) or you can try reading the "perlmod" and "perlmodstyle" manual pages. A complete reference is in chapter 11 of "Programming Perl".

This is "a way to call a sub from another file" as you ask. And, yes, it's a _very_ good idea. It means that you can reuse your core functions in as many programs as you want.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


benn600
User


Jun 17, 2005, 8:21 AM

Post #5 of 7 (1686 views)
Re: [davorg] Calling subroutines from other files and sorting [In reply to] Can't Post

Is there a website I can go to that will explain how to call subroutines from another file? I have tried searching but all the sites that talk about subs don't say how to call them from another file. Thank you.
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


davorg
Thaumaturge / Moderator

Jun 17, 2005, 8:27 AM

Post #6 of 7 (1685 views)
Re: [benn600] Calling subroutines from other files and sorting [In reply to] Can't Post

Sorry, I thought I was being clear. I'll try to be a bit clearer.

The best way to "call subroutines from another file" is to create a module that contains the subroutines that you want to resue and to use that module in all of the programs where you need those functions.

My previous post contains the names of the the two perldoc pages that you want to read to find out how to create modules (perldoc is installed whenever you install Perl so these pages will already exist on your computer). I also told you which chapters discussed this subject in the two most useful Perl books and gave you a pointer to where these books can be read on the web.

Short of writing the code for you (which I'm not going to do) I really don't know how much more help I can be.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


benn600
User


Jun 17, 2005, 8:49 AM

Post #7 of 7 (1683 views)
Re: [davorg] Calling subroutines from other files and sorting [In reply to] Can't Post

That is fine, thank you. I will check those sites out. I was just hoping that I could do something like &index{subs.pl}; or something easy like that, but it sounds more involved, but I will look at those pages that you told me. Thanks a lot!
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives