|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|
#2
|
||||
|
||||
|
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? |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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;.
|
|
#5
|
||||
|
||||
|
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"; |
|
#6
|
||||
|
||||
|
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:
english: Quote:
|
|
#7
|
||||
|
||||
|
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! |
|
#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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > writing modules |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|