Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl prog not correctly processing subroutine through require 1

Status
Not open for further replies.

joncom

Programmer
May 27, 2010
2
0
0
GB
I have a perl program that tries to call a subroutine which is available through a require statement. The file is called headers.pl and contains nothing more than a single subroutine called header_print that takes no arguments and just does a print, something like ....

sub header_print {
...
}
1;


My main perl is:

#!/usr/bin/perl
use CGI qw:)standard);
use lib qw(.);
eval { require "headers.pl"};
if ($@) {
print "Failed to load require because: $@"
}
else
{
print "No problem with require";
}
header_print{};

When the perl runs I successfully get the "No problem with require" suggesting the require worked file but then the perl fails with..

"Can't call method "header_print" without a package or object reference at ...pathname... line 12

--------

I am a bit of a beginner at this so I am hoping this is something obvious.

Thanks in anticipation.
 
I know next to nothing about modules/packages/etc, but I managed to get that to work by giving it a package name in headers.pl, and then referencing the function using the package name:

Perl:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]CGI[/green] [red]qw([/red][purple]:standard[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]lib[/green] [red]qw([/red][purple].[/purple][red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/eval.html][black][b]eval[/b][/black][/url] [red]{[/red] [url=http://perldoc.perl.org/functions/require.html][black][b]require[/b][/black][/url] [red]"[/red][purple]headers.pl[/purple][red]"[/red][red]}[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][blue]$@[/blue][red])[/red] [red]{[/red]
        [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Failed to load require because: [blue]$@[/blue][/purple][red]"[/red]
[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]No problem with require[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
headers::header_print[red]{[/red][red]}[/red][red];[/red]
Code:
[url=http://perldoc.perl.org/functions/package.html][black][b]package[/b][/black][/url] [green]headers[/green][red];[/red]
[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]header_print[/maroon] [red]{[/red]
        [black][b]print[/b][/black] [red]"[/red][purple]This is a header[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[fuchsia]1[/fuchsia][red];[/red]

Annihilannic.
 
This seemed to do the trick but I would like to understand why. Thanks for all your help.
 
My guess is that the 'eval' is making an anonymous reference or namespace to your headers_print subroutine. While it exists somewhere, you don't have the pointer to get to it. Adding the line 'package headers' causes perl to give it a reference to call it through.

One other thing to try is to change this
Code:
eval { require "headers.pl"};
to
Code:
require "headers.pl";
and you will not need the 'package' name. Adding namespaces to your code will help ensure that any variable used under 'headers' (in this case) are less likely to accidentally be used by other portions of your code. You would have to use $headers::some_var or &headers::some_sub to get to variables/subs located under that 'package' name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top