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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Module not getting called

Status
Not open for further replies.

Nutthick

MIS
Jan 14, 2004
126
GB
I'm trying to place a subroutine in a perl module and although I have the syntax correct (nothing in the error log) the routine doesn't seem to be getting called.

The key items from the main script are as follows;
Code:
use filelog;
update_log2("Filename", "Name");
obviously the call is within the code lower down. The routine is getting called, as there is another local one after it that is running correctly.

My module code is in the same directory as my script and is called filelog.pm. It's contents are as follows;
Code:
sub update_log {
	$filename = $_[0];
	$track = $_[1];

	# Read the log and determine the next ID value
	$filelog = "/var/[URL unfurl="true"]www/cgi-bin/files.log";[/URL]
	$fileCount = "";
	$temp = "";
	$line2 = "";
	$line3 = "";

	open (FILES, $filelog) || die "Could not open file";
	while ($temp = <FILES>) {
		$fileCount = $temp;
		$line2 = <FILES>;
		$line3 = <FILES>;
	}
	close(FILES);

	$fileCount++;

	# Append the file
	open (FILES, ">>" . $filelog) || die "Could not open file for append";
	print FILES "$fileCount\n";
	print FILES "$filename\n";
	print FILES "$track\n";
	close(FILES);

	return;
}
1;
Is there any reason why this wouldn't be getting called?

Thanks
 
Your module needs to export the function. Else you'll have to call it as a function of the module,

filelog::update_log("filename", "name")

Also, in your example you called update_log2 when the function was named update_log, don't know if you intended to do that or not though.
 
You need to export the function. Use the Exporter module.
Code:
package [i]filelog[/i];

use vars qw( @ISA %EXPORT_TAGS @EXPORT_OK );

require Exporter;

@ISA = qw( Exporter );

%EXPORT_TAGS = (
    'all' => [
        qw(
          update_log
          )
    ]
);

@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
[i]
sub update_log {
    $filename = $_[0];
    $track = $_[1];

    # Read the log and determine the next ID value
    $filelog = "/var/[URL unfurl="true"]www/cgi-bin/files.log";[/URL]
    $fileCount = "";
    $temp = "";
    $line2 = "";
    $line3 = "";

    open (FILES, $filelog) || die "Could not open file";
    while ($temp = <FILES>) {
        $fileCount = $temp;
        $line2 = <FILES>;
        $line3 = <FILES>;
    }
    close(FILES);

    $fileCount++;

    # Append the file
    open (FILES, ">>" . $filelog) || die "Could not open file for append";
    print FILES "$fileCount\n";
    print FILES "$filename\n";
    print FILES "$track\n";
    close(FILES);

    return;
}
[/i]
1; # always return true

M. Brooks
 
Well it's working, sort of. I put in the exporter code that mbrooks suggested, but I'm having to call the routine as
Code:
filelog::update_log("Filename","Name");
Does that mean my export code isn't working, or have I screwed up elsewhere?
 
You call it from within your script like this
Code:
#!/usr/bin/perl -w

use filelog qw( update_log );

# use it like
update_log("Filename","Name");

M. Brooks
 
If I call it without filelog:: then I get an error
Code:
Undefined subroutine &main::update_log called at
 
O.K. lets back up here for a moment.

1. Make sure the module and script are located in the same directory.

If not you must specify use lib "path/to/module/dir/"

2. Make sure the module has a 1; located at the bottom of the file.

3. Use this to call the function of the module.
Code:
#!/usr/bin/perl -w

use filelog qw( :all );

# use it like
update_log("Filename","Name");

M. Brooks
 
Well as far as I can see I'm all OK

1. Everything is in the root cgi-bin
2. That last 3 lines of the pm are
Code:
1;
the full listing is below
3. All exactly as written

and my pm is as follows
Code:
package filelog;

use strict;
use vars qw( @ISA %EXPORT_TAGS @EXPORT_OK );
require Exporter;
@ISA = qw( Exporter );
%EXPORT_TAGS = (
	'all' => [
		qw(
		update_log
		)
	]
);

@EXPORT_OK = ( @{EXPORT_TAGS{'all'} } );

sub update_log {
	my ($filename) = $_[0];
	my ($track) = $_[1];

	# Read the log and determine the next ID value
	my ($filelog) = "/var/[URL unfurl="true"]www/cgi-bin/files.log";[/URL]
	my ($fileCount) = "";
	my ($temp) = "";
	my ($line2) = "";
	my ($line3) = "";

	open (FILES, $filelog) || die "Could not open file";
	while ($temp = <FILES>) {
		$fileCount = $temp;
		$line2 = <FILES>;
		$line3 = <FILES>;
	}
	close(FILES);

	$fileCount++;

	# Append the file
	open (FILES, ">>" . $filelog) || die "Could not open file for append";
	print FILES "$fileCount\n";
	print FILES "$filename\n";
	print FILES "$track\n";
	close(FILES);

	return;
}
1;
 
One last thing. Test the module on the command line.

% perl -c filelog.pm

If it still fails. Check your update_log() function.

You should declare your variables with my.

As of now your script would fail under use strict; mode.

M. Brooks
 
This is the test I am running on the CLI on my server. And it works fine.

filelog.pm
Code:
package filelog;

use vars qw( @ISA %EXPORT_TAGS @EXPORT_OK );

require Exporter;

@ISA = qw( Exporter );

%EXPORT_TAGS = (
    'all' => [
        qw(
          update_log
          )
    ]
);

@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

sub update_log {
    my ( $name ) = @_;

    print "Update the log called - $name\n";

}

1; # always return true
filelog.pl
Code:
#!/usr/bin/perl -w

use strict;

use filelog qw( :all );

# use it like
update_log('mbrooks');

M. Brooks
 
Got it, I had typo'ed on your Exporter stuff, and had corrected it slightly based on the error log, so the syntax was perfect, but perfectly incorrect.

Thanks for all your help this week mbrooks. I would be in a very worried place this weekend if you hadn't helped me out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top