Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Useless use of private variable

by Sun751 (Beadle)
on Jul 15, 2009 at 02:38 UTC ( [id://780135]=perlquestion: print w/replies, xml ) Need Help??

Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

I have subroutine called "validate" where I am iterating over my array and doing some processing out of it,
sub validate { my ($p,$s_no) = @_; my $s = 0; for ($s; $s<=$s_no; $s++) { my $command = $CFG::CFG[$s]->{'CMD'}; if ($command =~ /cd /) { $$p = $$p - 1; } } }
but out of above code I am getting following warning:- "Useless use of private variable in void context at test.pl line 12" Any suggestion?Please, Cheers

Replies are listed 'Best First'.
Re: Useless use of private variable
by ikegami (Patriarch) on Jul 15, 2009 at 05:17 UTC
Re: Useless use of private variable
by Sandy (Curate) on Jul 15, 2009 at 02:49 UTC

    The "useless use of private variable" refers to the $s in your for loop because the variable is never being used.

    A more perlish way of looping over a bit of code a fixed number of times (one of many ways actually) is

    foreach (1 .. $s_no)

    Sandy

      as I am using the value of $s as index number for array,
      my $command = $CFG::CFG[$s]->{'CMD'}
      using foreach I don't see any possibility if I want to iterate over my array? Any suggestion?? Cheers.
        $s should be ok here. I suspect that $$p ( $$p=$$p-1;)could be the cause here? I'm not sure as I didn't get that error in my simple testing. I don't see much point in using a ref to $p. I would pass p itself, change this $$p line to $p--; and then return($p); at the end of the sub. I suppose its also possible that you did pass $p instead of \$p and this error message is a referencing error.

        Update: Looked again. $CFG::CFG[$s]->{'CMD'}; also looks a bit odd. I'd like to see some more code and explanation of what you think that this CFG data structure looks like. Maybe you intend an array of Hash? I'm not sure from this code. $CFG isn't defined anywhere that I can see. What I said above shouldn't actually matter from functionality or compile errors. If you are going to use a ref, passing a ref to this $CFG thing is the place. Normally passing a ref to a scalar is not necessary or desirable as Perl can return multiple lvalues.

        Update Again: I didn't see the missing $s=0 in the for loop. Even so this code sans $CFG::CFG[$s]->{'CMD'}; compiled and ran on my Perl 5.10 machine.

Re: Useless use of private variable
by jbt (Chaplain) on Jul 15, 2009 at 03:01 UTC
    $s is only useful in the for loop:

    sub validate { my ($p,$s_no) = @_; for (my $s = 0; $s<=$s_no; $s++) { my $command = $CFG::CFG[$s]->{'CMD'}; if ($command =~ /cd /) { $$p = $$p - 1; } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://780135]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-03-19 04:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found