Shebang line for modules?

Shebang line for modules?

am 08.01.2008 22:31:51 von Steve Roscio

Does it makes sense to include a shebang line at the top of a .pm file?
For example:

#!/usr/bin/perl -w
use strict;
.
.
.


Or is it more appropriate to omit the shebang line, since the .pm isn't
"executed" per-se, and instead 'use warnings;'?

- Steve

Re: Shebang line for modules?

am 08.01.2008 22:58:12 von Gunnar Hjalmarsson

Steve Roscio wrote:
> Does it makes sense to include a shebang line at the top of a .pm file?
> For example:
>
> #!/usr/bin/perl -w
> use strict;
> .
> .
> .
>
>
> Or is it more appropriate to omit the shebang line, since the .pm isn't
> "executed" per-se, and instead 'use warnings;'?

That sounds very much as a SAQ (Self Answered Question) to me. ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Shebang line for modules?

am 09.01.2008 01:06:58 von Steve Roscio

Gunnar Hjalmarsson wrote:
> Steve Roscio wrote:
>> Does it makes sense to include a shebang line at the top of a .pm
>> file? For example:
>>
>> #!/usr/bin/perl -w
>> use strict;
>> .
>> .
>> .
>>
>>
>> Or is it more appropriate to omit the shebang line, since the .pm
>> isn't "executed" per-se, and instead 'use warnings;'?
>
> That sounds very much as a SAQ (Self Answered Question) to me. ;-)
>
OK, I'll take that to mean:
nuke the shebang lines, use warnings.
Thanx.

Re: Shebang line for modules?

am 09.01.2008 09:36:52 von sisyphus359

On Jan 9, 11:06=A0am, Steve Roscio wrote:
> Gunnar Hjalmarsson wrote:
> > Steve Roscio wrote:
> >> Does it makes sense to include a shebang line at the top of a .pm
> >> file? =A0For example:
>
> >> #!/usr/bin/perl -w
> >> use strict;
> >> =A0 =A0.
> >> =A0 =A0.
> >> =A0 =A0.
>
> >> Or is it more appropriate to omit the shebang line, since the .pm
> >> isn't "executed" per-se, and instead 'use warnings;'?
>
> > That sounds very much as a SAQ (Self Answered Question) to me. ;-)
>
> OK, I'll take that to mean:
> =A0 =A0nuke the shebang lines, use warnings.

Except that there *are* differences between "use warnings" and the "-
w" flag. So I guess that if you wanted to enforce the "-w" switch in
preference to "use warnings", then you would have to do it via a
shebang line. (Read that last sentence as a question ... as much as an
assertion.)

I seem to recall that some people take exception to modules turning on
the "-w" switch. I think some people might even object to "use
warnings" in modules ... but I'm not au fait with the reasoning (re
either situation) off the top of my head.

Cheers,
Rob

Re: Shebang line for modules?

am 09.01.2008 10:45:06 von Gunnar Hjalmarsson

sisyphus wrote:
> On Jan 9, 11:06 am, Steve Roscio wrote:
>> OK, I'll take that to mean:
>> nuke the shebang lines, use warnings.

Yes, that's what I meant to say.

> Except that there *are* differences between "use warnings" and the "-
> w" flag. So I guess that if you wanted to enforce the "-w" switch in
> preference to "use warnings", then you would have to do it via a
> shebang line.

Not quite true. You can set the $^W variable directly.

package MyPackage;
use strict;
local $^W = 1;

Btw, by localizing it, warnings would not be enabled outside the
package, right?

OTOH, I think that would only catch compile time warnings. (Don't know
if that's true also for the "-w" switch.)

Conclusion: If you want to enable warnings within a module, "use
warnings" is the preferred way, as long as you don't care about
backwards compatibility with pre 5.6.0 versions of Perl.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Shebang line for modules?

am 09.01.2008 15:51:19 von Claudio Calvelli

On 2008-01-09, Gunnar Hjalmarsson wrote:
> Not quite true. You can set the $^W variable directly.
>
> package MyPackage;
> use strict;
> local $^W = 1;
>
> Btw, by localizing it, warnings would not be enabled outside the
> package, right?

They would not even be enabled inside the package though. The "local" will
mean that $^W is one during the module initialization, but not when its
code is called later. For example:

------ TEST.pm -------
package TEST;

use strict;
local $^W = 1;

print "in TEST: \$^W=$^W\n";

sub w {
print "in TEST:w(): \$^W=$^W\n";
}

1;
----------------------

-------- test --------
#!/usr/bin/perl

use TEST;

TEST::w();
print "outside TEST: \$^W=$^W\n";
----------------------

Now type "perl test" or your OS's equivalent and you get:

------ results -------
in TEST: $^W=1
in TEST::w(): $^W=0
outside TEST: $^W=0
----------------------

note how $^W is zero when TEST::w() is called.

You could add "local $^W = 1" at the start of every sub within your package,
but that would enable warnings in any subroutine called from it, even if it
is in another package, which is probably bad manners.

C.
--
Real email address: join('@', 'intercal', 'sdf.lonestar.org')
It helps the spam filter if you include the word CAMEL in the subject.

Re: Shebang line for modules?

am 09.01.2008 16:25:28 von Peter Scott

On Tue, 08 Jan 2008 14:31:51 -0700, Steve Roscio wrote:
> Does it makes sense to include a shebang line at the top of a .pm file?

Maybe:

http://www.ddj.com/web-development/184416165

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

Re: Shebang line for modules?

am 09.01.2008 22:21:05 von Steve Roscio

Thanx everyone! In my case it was a "stylistic" question, I had
wondered what was the defacto convention. I changed all my stuff
to 'use warnings' since that suited.

Re: Shebang line for modules?

am 10.01.2008 00:10:21 von Gunnar Hjalmarsson

Claudio Calvelli wrote:
> On 2008-01-09, Gunnar Hjalmarsson wrote:
>> You can set the $^W variable directly.
>>
>> package MyPackage;
>> use strict;
>> local $^W = 1;
>>
>> Btw, by localizing it, warnings would not be enabled outside the
>> package, right?
>
> They would not even be enabled inside the package though. The "local" will
> mean that $^W is one during the module initialization, but not when its
> code is called later.

Which wouldn't have been necessary to point out, if you hadn't snipped
my next sentence ("... only catch compile time warnings"). :(

> You could add "local $^W = 1" at the start of every sub within your package,

Those subs that are intended to be called from outside the package would do.

> but that would enable warnings in any subroutine called from it, even if it
> is in another package, which is probably bad manners.

That's a philosophical aspect on which I for one have no firm opinion. ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Shebang line for modules?

am 10.01.2008 09:50:29 von Claudio Calvelli

On 2008-01-09, Gunnar Hjalmarsson wrote:
> Claudio Calvelli wrote:
> Which wouldn't have been necessary to point out, if you hadn't snipped
> my next sentence ("... only catch compile time warnings"). :(

Sorry about that - I failed completely to even see it.
Now, if newsreader software could have a "warning: posting before
full caffeination" this wouldn't happen.

C
--
Real email address: join('@', 'intercal', 'sdf.lonestar.org')
It helps the spam filter if you include the word CAMEL in the subject.