linux poison RSS
linux poison Email

Perl Script: Include external Perl script withing Perl script

This is something different that including the package/modules, the require function provides a way to break your program into separate files and create libraries of functions.

NOTE: The last expression evaluated inside a file included by require becomes the return value. The require function checks whether this value is zero, and terminates if it is so, in this case make sure to return some non-zero value from withing your include file and the best way to do is to insert the 1; at the end of this include file.

Below Perl script show the way to include another Perl script within the Perl script.


Include File: cat include.pl
#!/usr/bin/perl

print "Inside the include file \n";
$var = 20;

1;

Source: cat require.pl
#!/usr/bin/perl

require ("include.pl");
$var = $var + 20;
print "final value of var is : $var \n";

Output: perl require.pl
Inside the include file
final value of var is : 40




0 comments:

Post a Comment

Related Posts with Thumbnails