469,267 Members | 887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

How to include perl file

I am a new perl programer and I meet a question.
I have a perl file test_1.pl like:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. sub Test;
  3.  
  4. sub Test {
  5.     printf "==========================Hello, perl.\n";
  6.     return 0;
  7. }
  8.  
and In test.pl I want to use the Test function, so I wrote test.pl like:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. #include <test_1.pl>
  4. sub main;
  5. sub main {
  6.     Test;
  7. }
  8.  
  9. main;
  10.  
But this does not work at all.
So can some help me to know how to include a perl file and makes the file mentioned above to work?
Thanks
May 24 '07 #1
4 17575
require '<name of your perl script you want to include>';
May 24 '07 #2
miller
1,089 Expert 1GB
You need to study the following material

perldoc use
perldoc require
cpan Exporter (for the more advanced)

Anyway, here is your code redone. Note that i've renamed the file you were trying to include to Test.pm.

Test.pm
Expand|Select|Wrap|Line Numbers
  1. package Test;
  2.  
  3. sub test {
  4.     printf "==========================Hello, perl.\n";
  5.     return 0;
  6. }
  7.  
  8. 1;
  9.  
  10. __END__
  11.  
test.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Test;
  4.  
  5. sub main {
  6.     Test::test();
  7. }
  8.  
  9. main();
  10.  
- Miller
May 24 '07 #3
Thanks for Miller and other's reply. But after changing the files following Miller's instruction, there still be error :

% perl test.pl
Undefined subroutine &Test::test called at test.pl line 6.

Is there still sth wrong?

Thanks

Files are like below:

test.pl:
#!/usr/bin/perl

use Test;

sub main {
Test::test();
}

main();


test.pm:

package Test;

sub test {
printf "==========================Hello, perl.\n";
return 0;
}

1;

__END__
May 25 '07 #4
miller
1,089 Expert 1GB
test.pm
The test.pm file should be named "Test.pm". Maybe that was just a typo in your post, but that could be a cause of the error.

- Miller
May 25 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by daniel kaplan | last post: by
2 posts views Thread by Amanda aman_DO_da345 | last post: by
4 posts views Thread by Al Franz | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.