Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 4th, 2001, 08:18 PM
rob5408 rob5408 is offline
Certified Genius
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: over there
Posts: 77 rob5408 User rank is Private First Class (20 - 50 Reputation Level)rob5408 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 46 m 57 sec
Reputation Power: 8
Send a message via AIM to rob5408
Question writing modules

i have a large section of coding for outputting webpage headser and footers. i think it may be useful to put it into a module. i have no real use for the power or complexity of modules, i just want to throw some subroutines in a separate file. if modules are the only way to go, is there a perl tutorial for c++ programmers or some other "complete morons" help file. i've read perlmod and perlmodlib and i think i'm more confused now than before...thanks, rob

Reply With Quote
  #2  
Old October 5th, 2001, 08:16 PM
Benahimvp's Avatar
Benahimvp Benahimvp is offline
The Basketball Star
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2001
Location: H-Town
Posts: 550 Benahimvp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via AIM to Benahimvp Send a message via Yahoo to Benahimvp
You can just put lots of subroutines into any text-based file and just use the require command in the Perl file. I usually use the .sub extension, so you would could have a file called stuff.sub and then in the perl script, you'd include it by doing:
Code:
require "stuff.sub";

Let's say you had a subroutine in stuff.sub called getinfo. In the Perl scripts all you have to do is do &getinfo; and it'll use the subroutine in stuff.sub as long as there's not one in the Perl script. Does that help?
__________________
-Ben Ilegbodu
(Around the Bend Web Designs)

Reply With Quote
  #3  
Old October 15th, 2001, 12:59 PM
donarb donarb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: Seattle
Posts: 133 donarb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Also, make sure that the last line of your subroutine file has 1; in it. The require command will parse all of the file when it reads it in, and the 1; will signal that the require was successful.

Don

Reply With Quote
  #4  
Old October 15th, 2001, 06:10 PM
Benahimvp's Avatar
Benahimvp Benahimvp is offline
The Basketball Star
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2001
Location: H-Town
Posts: 550 Benahimvp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via AIM to Benahimvp Send a message via Yahoo to Benahimvp
Oh yeah, that's very important. I spent a long time wondering why mine wasn't working until I figured that I needed that 1;.

Reply With Quote
  #5  
Old October 15th, 2001, 07:35 PM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
My anal comment of the day- A module doesn't actually have to have a 1; it just needs to return a true value, in a perlish sense.

You could end your modules with pretty much anything except nothing, '', "", or 0;

Customarily, module authors use 1 and that's the thing I use too. For fun (assuming you have a very loose definition of fun), you could use other "true" values too, like 42; or "The End";

Reply With Quote
  #6  
Old October 19th, 2001, 08:53 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
All hail anality!

Hmm, yes. Anyway, I was wondering if someone could give me a quick hand with "require". I'm fine with importing subroutines with require, but what about scalars or hashes? For example, I want to have a file containing a load of language-specific variables, so one script can display data with the surrounding menus being in different languages. (How) can I do this with require?

i.e. how can I do this sort of thing properly:


main script:
Quote:
require "english";
print $hello;

english:
Quote:
$hello = "Hello there!";
"Moo";

Reply With Quote
  #7  
Old October 19th, 2001, 11:06 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
One way to do it- put your variables into a hash, put that hash into a subroutine that returns a hash reference, and then use the hash reference in the main body of your script.

Mainscript.pl
Code:
#!/usr/bin/perl -wT
use strict;
require 'poo';

my $variables=get_vars();
print $variables->{'poopoopeedoo'}."\n";
print $variables->{'number2'}."\n";


poo
Code:
sub get_vars{
     my %hash=('poopoopeedoo'=>"Don't you love stupid geek humor?",'number2'=>'I need a hobby');
     return \%hash;
}


the output of mainscript.pl would be
Code:
Don't you love stupid geek humor?
I need a hobby


You can, of course, make your hash more elaborate (that sounds kind of seedy) with array refs and references to other hashes, but this basic idea should do what you want.

All hail references!

Reply With Quote
  #8  
Old October 19th, 2001, 02:28 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
Cheers Perl always looks so simple when you understand it! I need to get myself a book one of these days... man perl just isn't getting me too far lol

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > writing modules


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT