How do I get rid of this warning?

How do I get rid of this warning?

am 29.11.2006 09:13:22 von Ronny

Patterned after the solution suggested in the perlmodlib man page, I
wrote the following code:

==================== This is file b.pm
#!/usr/local/bin/perl -w
use strict;
package b;
sub f { print "BBBBBBBB @_\n"; }
eval join('', ) || die $@ unless caller();
1
__END__
package main;
b::f(@ARGV);
=================================

When called this from the command line, say:

b.pm X

it works fine. But when I use it as a module, say
from this context:

===================== This is file p.pl
#!/usr/local/bin/perl -w
use strict;
use b;
b::f('X');
=================================

when executing

p.pl

I get the warning

Name "main::DATA" used only once: possible typo at b.pm line 10.

How can I get rid of this warning? I can't use "use vars" here to
"predeclare"
main::DATA, because it is a file handle.

Ronald

Re: How do I get rid of this warning?

am 29.11.2006 11:43:06 von Michele Dondi

On 29 Nov 2006 00:13:22 -0800, "Ronny"
wrote:

>==================== This is file b.pm
>#!/usr/local/bin/perl -w
>use strict;

BTW:

use warnings; # it's better nowadays

(And I would be surprised if the shebang line did matter in .pm files,
unless of course you're also using them as scripts.)

>package b;

This is probably a minimal example, but do not use lowercase only
names for packages, as those are reserved for pragmas. BTW: do not use
B, since it does actually exist.

>sub f { print "BBBBBBBB @_\n"; }
>eval join('', ) || die $@ unless caller();
>1
>__END__
>package main;
>b::f(@ARGV);

Huh?!? Smell of XY problem here?
()

What's wrong with

f(@ARGV) unless caller;

?


>it works fine. But when I use it as a module, say
>from this context:
>
>===================== This is file p.pl
>#!/usr/local/bin/perl -w
>use strict;
>use b;
>b::f('X');
>=================================
>
>when executing
>
> p.pl
>
>I get the warning
>
> Name "main::DATA" used only once: possible typo at b.pm line 10.

#!/usr/bin/perl

use strict;
use warnings;

use lib sub {
pop eq 'BB.pm' ?
do { open my $fh, '<', \<<'.EOM' or die "$!\n";
# -----------------
use strict;
use warnings;
package BB;
local $/;
print ;
print ;
1;
__DATA__
BB::whatever...
.EOM
# -----------------
$fh } : undef
};

use BB;

__DATA__
main::whatever...


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: How do I get rid of this warning?

am 30.11.2006 07:59:52 von Ronny

Michele Dondi schrieb:
> On 29 Nov 2006 00:13:22 -0800, "Ronny"
> wrote:
>
> >==================== This is file b.pm
> >#!/usr/local/bin/perl -w
> >use strict;
>
> BTW:
>
> use warnings; # it's better nowadays

Ah, thank you, never heard of it before.

> (And I would be surprised if the shebang line did matter in .pm files,
> unless of course you're also using them as scripts.)

Exactly that's what I wanted to do.

>
> >package b;
>
> This is probably a minimal example, but do not use lowercase only
> names for packages, as those are reserved for pragmas.

Correct. The "real" example has a longer name, starting with an
uppercase
letter.

> BTW: do not use
> B, since it does actually exist.

Indeed! Thank you for telling me - I wouldn't have guessed that!

>
> >sub f { print "BBBBBBBB @_\n"; }
> >eval join('', ) || die $@ unless caller();
> >1
> >__END__
> >package main;
> >b::f(@ARGV);
>
> Huh?!? Smell of XY problem here?
> ()

Not really, but here is the X, to give you some background to my
question:

While the file is indeed primarily supposed to be used as a module, I
also
want to invoke it occasionally from the command line (this is a common
technique in, say, Python or Ruby, though I recognize it might not be
so
popular in the Perl community, where people would rather write a
separate
..pl file which uses that module).

When researching for a Perl-feature which would allow me to do this,
I found several solutions, which all worked well. Actually, the one
we are talking about here, works too, only that it exhibits the
warning.
Hence my interest is *really* in how to get rid of this warning - not
so
much because I would need to know this for my particular problem at
hand (the two other ways to use a .pm file as a standalone program
work well too without giving a warning), but because I am a curious
person and am interested in knowing such things.

I know for example how to get rid of a warning when a *variable*
is used only once in a program (use vars qw(...)), but here we have
a file handle, and I would like to find out how to remove the warning
in this case.

Actually, as you pointed out "use warnings" to me, I tried the
following approach:

{
no warnings;
eval ...
}

It's a bit of an overkill (it disables *all* warnings in this block),
but at
least this works.

Ronald

Re: How do I get rid of this warning?

am 30.11.2006 12:45:00 von Tad McClellan

Ronny wrote:
> Michele Dondi schrieb:
>> On 29 Nov 2006 00:13:22 -0800, "Ronny"
>> wrote:
>>
>> >==================== This is file b.pm
>> >#!/usr/local/bin/perl -w
>> >use strict;
>>
>> BTW:
>>
>> use warnings; # it's better nowadays
>
> Ah, thank you, never heard of it before.



> I know for example how to get rid of a warning when a *variable*
> is used only once in a program (use vars qw(...)), but here we have
^^^^^^^^
> a file handle, and I would like to find out how to remove the warning
> in this case.


It also appears that you haven't heard of:

perldoc -f our

which is almost always preferred over "use vars".


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Re: How do I get rid of this warning?

am 30.11.2006 14:17:52 von anno4000

Ronny wrote in comp.lang.perl.misc:
> Michele Dondi schrieb:
> > On 29 Nov 2006 00:13:22 -0800, "Ronny"
> > wrote:

[...]

> Actually, as you pointed out "use warnings" to me, I tried the
> following approach:
>
> {
> no warnings;
> eval ...
> }
>
> It's a bit of an overkill (it disables *all* warnings in this block),
> but at
> least this works.

Then make it specific:

{
no warnings 'once';
# ...
}

Anno

Re: How do I get rid of this warning?

am 01.12.2006 03:10:02 von Ben Morrow

Quoth "Ronny" :
> Patterned after the solution suggested in the perlmodlib man page, I
> wrote the following code:
>
> ==================== This is file b.pm
> #!/usr/local/bin/perl -w
> use strict;
> package b;
> sub f { print "BBBBBBBB @_\n"; }
> eval join('', ) || die $@ unless caller();
> 1
> __END__
> package main;
> b::f(@ARGV);
> =================================

If you use __DATA__ instead of __END__, it will honour the current
package, and doesn't produce 'used once' warnings. So, for example, with

>>> Data.pm
#!/usr/bin/perl

package Data;

use strict;
use warnings;

sub foo {
return "foo";
}

unless (caller) {
print for ;
}

1;

__DATA__
bar
baz
<<< Data.pm

>>> data
#!/usr/bin/perl -l

use strict;
use warnings;

use Data;

print Data::foo;

__END__
<<< data

you get

~% ./Data.pm
bar
baz
~% ./data
foo
~%

Ben

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ benmorrow@tiscali.co.uk ~ Jorge Luis Borges, 'The Babylon Lottery'

Re: How do I get rid of this warning?

am 05.12.2006 11:36:17 von Ronny

anno4000@radom.zrz.tu-berlin.de schrieb:
> Ronny wrote in comp.lang.perl.misc:
> > Michele Dondi schrieb:
> > > On 29 Nov 2006 00:13:22 -0800, "Ronny"
> > > wrote:
> > {
> > no warnings;
> > eval ...
> > }
> >
> > It's a bit of an overkill (it disables *all* warnings in this block),
> > but at
> > least this works.
>
> Then make it specific:
>
> {
> no warnings 'once';
> # ...
> }

Works well!!! I already suspected that such a specification exists, but
where are these documented? I did a "perldoc warnings", but id doesn't
list any categories (?) such as 'once'...

Ronald

Re: How do I get rid of this warning?

am 05.12.2006 12:42:58 von Gunnar Hjalmarsson

Ronny wrote:
> anno4000@radom.zrz.tu-berlin.de schrieb:
>>
>> {
>> no warnings 'once';
>> # ...
>> }
>
> Works well!!! I already suspected that such a specification exists, but
> where are these documented? I did a "perldoc warnings", but id doesn't
> list any categories (?) such as 'once'...

perldoc perllexwarn

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

Re: How do I get rid of this warning?

am 05.12.2006 12:51:00 von Tad McClellan

Ronny wrote:
> anno4000@radom.zrz.tu-berlin.de schrieb:


>> {
>> no warnings 'once';
>> # ...
>> }
>
> Works well!!! I already suspected that such a specification exists, but
> where are these documented? I did a "perldoc warnings", but id doesn't
> list any categories (?) such as 'once'...


You didn't read it carefully enough:

... See perllexwarn for more information.

So see the "Category Hierarchy" section there.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Re: How do I get rid of this warning?

am 06.12.2006 13:43:28 von Ronny

Tad McClellan schrieb:
> Ronny wrote:
> > I did a "perldoc warnings", but id doesn't
> > list any categories (?) such as 'once'...
>
>
> You didn't read it carefully enough:
>
> ... See perllexwarn for more information.
>
> So see the "Category Hierarchy" section there.

I admit, I have overlooked this.

But I must say that even perllexwarn is only marginally helpful.
The categories are listsed, but not explained; so one has to
guess from the names (of the categories, or the wording of the
warining message), which is the right one to use....

Thank you anyway for pointing this out.

Ronald