http://qs321.pair.com?node_id=760610

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

This is my module as Objects.pm

package Objects; sub New { bless {},shift; } 1;

This is my program handler.pl using this module

use strict; use warnings; use Objects; use Data::Dumper; func(); sub func { $main::Objects = New Objects; print "This is in \"func\" function.\n"; }

I was getting the warning message like Name "main::Objects" used only once: possible typo at handler.pl line 31. why any thing is wrong here

Vinoth,G

Replies are listed 'Best First'.
Re: "main::Objects" used only once: possible typo
by kennethk (Abbot) on Apr 28, 2009 at 14:33 UTC
    This warning is being issued by the warnings pragma because you are only referencing the $Objects scalar in the main package once. It is not clear to me why you would use the fully-qualified path name in that context, as you would get exactly the same result with a my declaration without the error message. As well, if you used the variable more than once (i.e. to do something), it would not return that error either. If you use diagnostics (diagnostics), a more verbose error message will be printed that will make any message issued by strict and warnings clearer (or least more verbose).
Re: "main::Objects" used only once: possible typo
by hilitai (Monk) on Apr 28, 2009 at 14:36 UTC

    It's not an error, it's a warning. You're getting it because you're using 'use warnings;'. The compiler noticed that there is only one use of $main::Objects in your script -- you create it, but never use it for anything -- and emitted the warning because normally when an object is created, it is eventually used in some way.

    If you use $main::Objects, the warning will go away.

Re: "main::Objects" used only once: possible typo
by mikeraz (Friar) on Apr 28, 2009 at 17:14 UTC

    It's good to start small and build, checking for problems as you go. In this case the smallness is the issue.

    There's nothing "wrong" here. The full message, at least on my system with your posted code, is
    Name "main::Objects" used only once: possible typo at ./handler.pl line 9.
    Note that it warns "possible typo". There's nothing wrong there. Think about the future. You add to this start. It gets big. On some run Perl gives you a warning like
    Name "main::Obects" used only once: possible typo at ./handler.pl line 99.
    Without the warning you might spend a bunch of time looking for the problem in your code - because it is no longer functioning as expected. All because when you meant to type Objects you typed Obects. In that (all too familiar to me) scenario you become very thankful that Perl warnings will tell you about the error.


    Be Appropriate && Follow Your Curiosity
Re: "main::Objects" used only once: possible typo
by targetsmart (Curate) on Apr 29, 2009 at 06:01 UTC
    Other monks have given suitable answers to you.
    my part of wisdom to you is warnings::unused
    this module complains for all the variables which is just used only once and nowhere else in the program.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: "main::Objects" used only once: possible typo
by Anonymous Monk on Apr 28, 2009 at 13:32 UTC