Skip to content

used only once vs. uninitialized messages #7831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
p5pRT opened this issue Mar 8, 2005 · 15 comments
Closed

used only once vs. uninitialized messages #7831

p5pRT opened this issue Mar 8, 2005 · 15 comments

Comments

@p5pRT
Copy link

p5pRT commented Mar 8, 2005

Migrated from rt.perl.org#34381 (status was 'rejected')

Searchable as RT34381$

@p5pRT
Copy link
Author

p5pRT commented Mar 8, 2005

From @jidanni

True, one should say
$ seq 3|perl -nwe 'BEGIN{$i=0};print ++$i." $_"'
But the message seen upon
$ seq 3|perl -nwe 'print ++$i." $_"'
Name "main​::i" used only once​: possible typo at -e line 1.
1 1
2 2
3 3
Doesn't seem the most ideal. Probably it is a one-size-fits-all message.
Probably there should be the 'use of uninitialized ...' message.

@p5pRT
Copy link
Author

p5pRT commented Mar 8, 2005

From @nwc10

On Tue, Mar 08, 2005 at 05​:25​:57PM -0000, Dan Jacobson wrote​:

# New Ticket Created by Dan Jacobson
# Please include the string​: [perl #34381]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=34381 >

True, one should say
$ seq 3|perl -nwe 'BEGIN{$i=0};print ++$i." $_"'
But the message seen upon
$ seq 3|perl -nwe 'print ++$i." $_"'
Name "main​::i" used only once​: possible typo at -e line 1.
1 1
2 2
3 3
Doesn't seem the most ideal. Probably it is a one-size-fits-all message.

Well, it's totally consistent with how that message gets generates for other
cases of a single reference to a variable, such as​:

$ perl -wle 'print ++$i for 0..1'
Name "main​::i" used only once​: possible typo at -e line 1.
1
2

It's only a warning, and there are other less strange constructions
that can trigger it. In particular referencing a variable that will be
initialised in another module that will be C<require>d in the future.

Probably there should be the 'use of uninitialized ...' message.

No. It's clearly documented that ++ does not warn about uninitialized values​:

  C<undef> is always treated as numeric, and in particular is changed
  to C<0> before incrementing

Nicholas Clark

@p5pRT
Copy link
Author

p5pRT commented Mar 8, 2005

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Mar 8, 2005

From @tamias

On Tue, Mar 08, 2005 at 05​:25​:57PM -0000, Dan Jacobson wrote​:

# New Ticket Created by Dan Jacobson
# Please include the string​: [perl #34381]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=34381 >

True, one should say
$ seq 3|perl -nwe 'BEGIN{$i=0};print ++$i." $_"'
But the message seen upon
$ seq 3|perl -nwe 'print ++$i." $_"'
Name "main​::i" used only once​: possible typo at -e line 1.
1 1
2 2
3 3
Doesn't seem the most ideal. Probably it is a one-size-fits-all message.
Probably there should be the 'use of uninitialized ...' message.

Auto-decrement and auto-increment are intentionally excluded from giving
'use of uninitialized value' warnings, so that they can be conveniently
used in constructs such as​:

while (<>) {
  $seen++;
}

Ronald

@p5pRT
Copy link
Author

p5pRT commented Mar 8, 2005

From @smpeters

[jidanni@​jidanni.org - Tue Mar 08 09​:25​:56 2005]​:

True, one should say
$ seq 3|perl -nwe 'BEGIN{$i=0};print ++$i." $_"'
But the message seen upon
$ seq 3|perl -nwe 'print ++$i." $_"'
Name "main​::i" used only once​: possible typo at -e line 1.
1 1
2 2
3 3
Doesn't seem the most ideal. Probably it is a one-size-fits-all message.
Probably there should be the 'use of uninitialized ...' message.

...or you could use the warning that occurs elsewhere in perlrun.pod. I
also added an aside that declaring your variables in a BEGIN will
eliminate this warning.

Inline Patch
--- perlrun.pod.orig    Tue Mar  8 11:38:44 2005
+++ perlrun.pod Tue Mar  8 11:45:39 2005
@@ -651,7 +651,10 @@
 overrides a B<-n> switch.

 C<BEGIN> and C<END> blocks may be used to capture control before or after
-the implicit loop, just as in B<awk>.
+the implicit loop, just as in B<awk>.  Also, when using this option on
a script \+with warnings enabled you may get a lot of spurious "used only once" \+warnings\. Declaring your variables in a C\ block will eliminate these \+warnings\.

=item B<-P>

@p5pRT
Copy link
Author

p5pRT commented Mar 8, 2005

From @ysth

On Tue, Mar 08, 2005 at 05​:46​:42PM -0000, Steve Peters via RT wrote​:

[jidanni@​jidanni.org - Tue Mar 08 09​:25​:56 2005]​:

True, one should say
$ seq 3|perl -nwe 'BEGIN{$i=0};print ++$i." $_"'
But the message seen upon
$ seq 3|perl -nwe 'print ++$i." $_"'
Name "main​::i" used only once​: possible typo at -e line 1.
1 1
2 2
3 3
Doesn't seem the most ideal. Probably it is a one-size-fits-all message.
Probably there should be the 'use of uninitialized ...' message.

...or you could use the warning that occurs elsewhere in perlrun.pod. I
also added an aside that declaring your variables in a BEGIN will
eliminate this warning.

--- perlrun.pod.orig Tue Mar 8 11​:38​:44 2005
+++ perlrun.pod Tue Mar 8 11​:45​:39 2005
@​@​ -651,7 +651,10 @​@​
overrides a B<-n> switch.

C<BEGIN> and C<END> blocks may be used to capture control before or after
-the implicit loop, just as in B<awk>.
+the implicit loop, just as in B<awk>. Also, when using this option on
a script
+with warnings enabled you may get a lot of spurious "used only once"
+warnings. Declaring your variables in a C<BEGIN> block will eliminate
these
+warnings.

I'd like people to assume that "declaring a variable" normally means
with "my", which won't help here, and may hurt. Using "our" doesn't
require a BEGIN block (though it might be infinitesimally faster using
one). And using "use vars" doesn't benefit from a BEGIN block at all.
So I'd remove that part.

In fact, I'd remove the whole thing, in favor of adding something
specific about -n and -p to the perldiag entry.

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2005

From [email protected]

Yitzchak Scott-Thoennes <sthoenna@​efn.org> wrote

In fact, I'd remove the whole thing, in favor of adding something
specific about -n and -p to the perldiag entry.

But as Nicholas points out, the problem isn't specific to -n or -p.

I don't see why this case needs any extra documentation.

Mike Guy

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2005

From @samv

Dan Jacobson (via RT) wrote​:

True, one should say
$ seq 3|perl -nwe 'BEGIN{$i=0};print ++$i." $_"'
But the message seen upon
$ seq 3|perl -nwe 'print ++$i." $_"'
Name "main​::i" used only once​: possible typo at -e line 1.
Doesn't seem the most ideal. Probably it is a one-size-fits-all message.
Probably there should be the 'use of uninitialized ...' message.

I guess this seems to contradict sense, because a post-increment is
actually *two* references to a variable - one read access, and "silent"
write access after the statement. So, it is in fact being used twice
despite being mentioned in the source only once.

On the other hand, is it worth making yet another exception for the
post/pre-increment operators? It's difficult to say which principle of
least surprise is being violated more.

Have you considered trying doing this sort of feedback and commentary
for Perl 6, where these fundamentals are more actively being developed
and hence more open to feedback? Just a thought.

Sam.

@p5pRT
Copy link
Author

p5pRT commented Mar 10, 2005

From @jidanni

D> Have you considered trying doing this sort of feedback and commentary
D> for Perl 6, where these fundamentals are more actively being developed
D> and hence more open to feedback? Just a thought.
$ apt-cache policy perl
perl​:
  Installed​: 5.8.4-6
i.e., "your suggestion, though well intended, does not apply to little
old end user me."

@p5pRT
Copy link
Author

p5pRT commented Mar 16, 2005

From @jidanni

Found another​:
$ echo|perl -pwe 'BEGIN{undef$/;my $c=0}s/./$c++/ge'
Name "main​::c" used only once​: possible typo at -e line 1.

@p5pRT
Copy link
Author

p5pRT commented Mar 16, 2005

From [email protected]

On Mar 17, Dan Jacobson said​:

$ echo|perl -pwe 'BEGIN{undef$/;my $c=0}s/./$c++/ge'
Name "main​::c" used only once​: possible typo at -e line 1.

That's not unexpected. The lexical $c exists only inside the BEGIN { }
block, and the global $c is only found once in the source code.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http​://japhy.perlmonk.org/ % have long ago been overpaid?
http​://www.perlmonks.org/ % -- Meister Eckhart

@p5pRT
Copy link
Author

p5pRT commented Mar 24, 2005

From @jidanni

D> That's not unexpected. The lexical $c exists only inside the BEGIN { }
D> block, and the global $c is only found once in the source code.
Indeed, the user thinks -w is blabbing 'off the wall'​:
$ echo|perl -nlwe 'BEGIN{my $c=0}print $c++'
Name "main​::c" used only once​: possible typo at -e line 1.
0
However we see indeed the 'program has serious problems'​:
$ echo|perl -nlwe 'BEGIN{my $c=5}print $c++'
Name "main​::c" used only once​: possible typo at -e line 1.
0

OK never mind, whatever. Always use strict, etc.

@p5pRT
Copy link
Author

p5pRT commented Mar 26, 2005

From @smpeters

[jidanni@​jidanni.org - Thu Mar 24 15​:13​:36 2005]​:

D> That's not unexpected. The lexical $c exists only inside the BEGIN { }
D> block, and the global $c is only found once in the source code.
Indeed, the user thinks -w is blabbing 'off the wall'​:
$ echo|perl -nlwe 'BEGIN{my $c=0}print $c++'
Name "main​::c" used only once​: possible typo at -e line 1.
0
However we see indeed the 'program has serious problems'​:
$ echo|perl -nlwe 'BEGIN{my $c=5}print $c++'
Name "main​::c" used only once​: possible typo at -e line 1.
0

OK never mind, whatever. Always use strict, etc.

As none of these cases seem to be functioning as documented, ticket closed.

@p5pRT
Copy link
Author

p5pRT commented Mar 26, 2005

@smpeters - Status changed from 'open' to 'rejected'

@p5pRT p5pRT closed this as completed Mar 26, 2005
@p5pRT
Copy link
Author

p5pRT commented Mar 26, 2005

From @nwc10

On Sat, Mar 26, 2005 at 04​:00​:50AM -0000, Steve Peters via RT wrote​:

OK never mind, whatever. Always use strict, etc.

As none of these cases seem to be functioning as documented, ticket closed.

Isn't there one too many negatives in that sentence? Or one too few​:

"As none of these cases seem to be functioning as anything other than
documented"

Bug re-closed.

Nicholas Clark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant