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

problem with module and sub

Status
Not open for further replies.

donny750

Programmer
Joined
Jul 13, 2006
Messages
145
Location
FR
hello,

I've script like this;
In my script i want to put the sub in a .pm.
I've created a .pm with my sub and i call my sub in my script but i've an error that i don't understand.

This is my script :

Code:
#!/usr/bin/perl


use fonction;

my @tab = (

    {
        "name"         => "Brian",
        "age"        => "25"
       
    },

    {
        "name"          => "tommy",
        "age"         => "29"
    }
                
                );
                
my %name;
foreach (@tab) {
    $name{ $_->{'name'} } = $_;
}


               
sub fonc {

my @tablo = @_;
my $var;


 foreach $var (@tablo) {
        
       
           
      
&fonction::test($var->{'name'});
      

        print  "$var->{'name'}=$var->{'age'}\n";
        
       
 }

}    




&fonc(@tab);

This is my module:

Code:
#!/usr/bin/perl



package fonction;

use warnings;

use strict;


use File::Basename;

use English;

sub test 
{
my $varname = shift;

if  ($name{"$varname"}{"age"} < 30 )
{
print "too young\n";
}

}

This is the error:

Global symbol "%name" requires explicit package name at fonction.pm line 20.
Compilation failed in require at C:\Documents and Settings\donny\Bureau\work\fonction.pl line 4.
BEGIN failed--compilation aborted at C:\Documents and Settings\donny\Bureau\work\fonction.pl line 4.

I don't undestand why ??

Thanks
 
I'm guessing it's because you're calling fonction.pm, before declaring %name, try the code below and see how you get on
Code:
#!/usr/bin/perl
package fonction;
use warnings;
[b]use strict;[/b]
use File::Basename;
use English;
sub test {
  my $varname = shift;
  if  ($name{"$varname"}{"age"} < 30 ) {
    print "too young\n";
  }
}
Code:
#!/usr/bin/perl
[COLOR=red]my %name;[/color]
use fonction;
my @tab = (
    {
        "name"         => "Brian",
        "age"        => "25"
    },
    {
        "name"          => "tommy",
        "age"         => "29"
    }
  );
foreach (@tab) {
    $name{ $_->{'name'} } = $_;
}
sub fonc {
my @tablo = @_;
my $var;
 foreach $var (@tablo) {
    &fonction::test($var->{'name'});
    print  "$var->{'name'}=$var->{'age'}\n";
 }
}    
&fonc(@tab);

HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
i must modify my .pm like this :

Code:
#!/usr/bin/perl



package fonction;

use warnings;

use strict;


use File::Basename;

use English;

sub test 
{
my $varname = shift;
my %var_name = %_;

if  ($var_name{"$varname"}{"age"} < 30 )
{
print "too young\n";
}

}

1;
 
i've modify like this

Code:
#!/usr/bin/perl



package fonction;

use warnings;

use strict;


use File::Basename;

use English;

sub test 
{
my $varname = shift;
my %var_name = %_;

if  ($var_name{"$varname"}{"age"} < 30 )
{
print "too young\n";
}

}

1;
 
To comment something on how you had it originally:

Code:
sub test
{
my $varname = shift;

if  ($name{"$varname"}{"age"} < 30 )
{
print "too young\n";
}

}

If you want to read or manipulate a variable from your main Perl script (%name), you need to prepend "main::" to it (as "main" is the default package name)

Code:
if ($main::name{"$varname"}{"age"} < 30)

Most included modules have a "package Some::Module;" at least once in their code (usually near the top), and it puts all their subroutines and variables in that name space as opposed to "package main", such that "$LWP::Simple::VERSION" and "$HTTP::Request::VERSION" are two different variables, while "$VERSION" would be one your own script could define for the "main::" namespace ($main::VERSION)

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Your problem is that you do not even need the "%name" data structure that you've included in your main script file. Instead, simply change your "sub test" so that it receives the entire record for a person as a hash reference. Here is the code so modified.

Code:
#!/usr/bin/perl

use fonction;

my @tab = (
	{
		"name"	=> "Brian",
		"age"	=> "25"
	},
	{
		"name"	=> "tommy",
		"age"	=> "29"
	}
);
[COLOR=red]
# This Data structure is unneeded
my %name;
foreach (@tab) {
	$name{ $_->{'name'} } = $_;
}[/color]

sub fonc {
	my @tablo = @_;
	my $var;

	foreach $var (@tablo) {
		[COLOR=green]&fonction::test($var);[/color]
		# Removed: [COLOR=red]&fonction::test($var->{'name'});[/color]

		print  "$var->{'name'}=$var->{'age'}\n";
	}
}

&fonc(@tab);

Code:
#!/usr/bin/perl

package fonction;
use warnings;
use strict;

use File::Basename;
use English;

sub test {
	[COLOR=green]my $record = shift;[/color] # This is passed a hash reference
	[COLOR=red]# Removed: my $varname = shift;[/color]

	[COLOR=red]# Removed: if ($name{"$varname"}{"age"} < 30 ) {[/color]
	[COLOR=green]if ($record->{"age"} < 30) {[/color]
		print "too young\n";
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top