Package Query:: If Package P1 is imported in Package P2, and P2 is imported in module, can we access elements of package P1?

Hello Here is my sample code, for usage of PACKAGES.
Facing a query, while accessing an element from PackageP1, which is imported in PackageP2,
Can you please help me with below code.

package p1;
  integer a = 40;
  integer b = 30;
  integer c = 20;
  integer d = 10;  
endpackage

package p2;
  int a = 33; // IF i redeclare int A,  then i can use in below module
  import p1::*;
               
  task f1;
    $display("B::%0d",b); 
    $display("D::%0d",d); 
  endtask 
  int c = 150; 
int b; // This gives error, "Identifier "b" previously imported from package p1"
endpackage

import p2::*;
module simple_package();
  initial begin
   $display("A::%0d",a);
   f1();
    //  $display("B::%0d",b); // This display gives error "Identifier "d" has not been declared yet.
   $display("C::%0d",c);
    //  $display("D::%0d",d);  // This display gives error "Identifier "d" has not been declared yet.
 end
endmodule

In reply to hvgbl:

You will need to export p1::* inside package p2. As per LRM:

By default, declarations imported into a package are not visible by way of subsequent imports of that package. Package export declarations allow a package to specify that imported declarations are to be made visible in subsequent imports.

So in your code, try:


package p2;
  int a = 33; // IF i redeclare int A,  then i can use in below module
  import p1::*;
  export p1::*;
 
  task f1;
    $display("B::%0d",b); 
    $display("D::%0d",d); 
  endtask 
  int c = 150; 
endpackage

HTH
Srini
www.verifworks.com

In reply to Srini @ CVCblr.com:

Hello Srini,

Thanks for the solution, its working for me, with export.

In reply to hvgbl:

Hi,

If variable ‘integer a’ declared in package p1 need to be accessed from module sample_package,
it can be done using scope resolution operator :: without re declaring it again in package p2.

module simple_package();
initial begin
$display(“A::%0d”,p1::a);
end
endmodule

In reply to Srini @ CVCblr.com:
Please see Package export does not work like I expect | Verification Academy for some more details about the export construct.