Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two hashes say fruit and vegetable in a.pl file how can i access these in another file say b.pl?

What I have tried:

#a.pl file
%fruit=('apple',1,'mango',2,'grapes',3);
%vegetable=('carrot',4,'potato',5);
%flowers=('rose',6,'jasmine',7);

#b.pl file
$praticular_product="apple";
#check $praticular_product belongs to fruit hash or vegetable hash
if(exists($fruit{$praticular_product})) 
{ 
    print "fruit\n"; 
} 
elsif(exists($vegetable{$praticular_product}))
{ 
    print "vegetable\n"
}
elsif((exists($flowers{$praticular_product})))
{
    print "flower\n"
}
else
{
    print "none\n"
}
print $praticular_product;
Posted
Updated 25-Aug-20 2:39am

1 solution

I would suggest you to pick some book or tutorial to go through it first. See what all is already there and how they can be used. This would help you know basic stuff.

For now, above query, look details here: Using variable from modules[^]
Quote:
We can also use variables from different packages. But we need to declare them first before using them. We do this by use vars qw($scalar @array %hash) and we can also use our ($scalar @array %hash) with Perl v5.6.0 or higher versions.

Example:
PERL
#This is p.pm

use strict;
use warnings;

package b;

our ($var_name);

sub Hello{
  print "Hello $var_name\n";
}

1;

PERL
#p.pm is in parent directory

use strict;
use warnings;

#using package p
use p;

#using var_name from p
$p::var_name = "Sam";
#Function Hello of p
p::Hello();

Output:
Hello Sam
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900