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

Question about use directive

Status
Not open for further replies.

spookie

Programmer
May 30, 2001
655
IN
Hi,

I have a module which has "used" strict in it. Then in my perl file i use that module, but should it also include strict pragma in my perl file automatically?(it is not)

Why is it so? Below is the sample code

Code:
#test.pm
package test;

use strict;

sub new {
	my($self) = {};
	bless $self;
}

sub returnName
{
	return "XYZ" ;
}


1 ;

#test.pl
use test ;

$testObj = new test;

$Name = $testObj->returnName() ;
print $Name ;

The script runs successfully even if variable scope is not defined.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
simple answer, always use "use strict", it's there for a reason, and that is to protect us from ourselves.


Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
PaulTEG,
Thanks for the answer, but i am trying to find why it has to be "used" in perl script even after i have included it in my module and "used" that module in my perl script?

I guess it's something to be related to the module being loaded in perl script at compile time, but can someone elaborate on it please..?

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
I am pretty sure the compiler directives, like "strict" and "warnings" and such, only affect the script they are loaded in, otherwise you would get into problems of each programs complier directives all stepping on each other as modules were loaded. It also seems like poor design the let modules dictate what environment the main program is compiled under.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I am pretty sure the compiler directives, like "strict" and "warnings" and such, only affect the script they are loaded in
Absolutely, but when i try following code for e.g.

Code:
#test1.pm
package test1;

use strict;

sub new {
	my($self) = {};
	bless $self;
}

sub returnLastName
{
	return "ABC" ;
}


1 ;

#test.pm

package test;

use strict;
use test1 ;

sub new {
	my($self) = {};
	bless $self;
}

1 ;

#test.pl
use test ;

$testObj = new test1;

$LastName = $testObj->returnLastName() ;
print $LastName ;

It works perfectly printing "ABC". The module test1 is automatically loaded in test.pl. As prex1 says correctly, use strict is only active in the enclosing block. So why the different behaviours for pragmas and user defined modules? Or how does use work exactly for the different scenarios?

Thanks in advance.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
prex1,
The issue here is not about 'use strict' in particular but how strict pragma not getting automatically loaded in perl script and user defined module is..

Here in my last post, i have not "used" test1 in my .pl files, but still i can use test1's method, since i have "used" test and test has in turn "used" test1.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
The major difference I believe is that strict is a pragma and the module is not. The issue of "use" is not relevant, the issue of pragma (a compiler directive) vs module (a library file) is. While both pragmas and modules can be loaded with "use" they are not the same thing.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top