How do I get rid of this warning?

R

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('', <main::DATA>) || 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
 
R

Ronny

Michele said:
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.
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!
Huh?!? Smell of XY problem here?
(<http://perlmonks.org/?node=XY+problem>)

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
 
T

Tad McClellan

Ronny said:
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".
 
A

anno4000

Ronny said:
Michele said:
On 29 Nov 2006 00:13:22 -0800, "Ronny" <[email protected]>
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
 
B

Ben Morrow

Quoth "Ronny said:
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('', <main::DATA>) || 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
#!/usr/bin/perl

package Data;

use strict;
use warnings;

sub foo {
return "foo";
}

unless (caller) {
print for <DATA>;
}

1;

__DATA__
bar
baz
<<< Data.pm
#!/usr/bin/perl -l

use strict;
use warnings;

use Data;

print Data::foo;

__END__
<<< data

you get

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

Ben
 
R

Ronny

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
 
G

Gunnar Hjalmarsson

Ronny said:
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
 
T

Tad McClellan

Ronny said:
(e-mail address removed)-berlin.de schrieb:


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.
 
R

Ronny

Tad said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top