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

I do know some perl, and this is a project i've been working on for some time. The compiler and komodo tell me there is a error on line 140, namely "Useless use of private variable in void context at #PATH#/main.pl line 140." It is a sub, called only once. I am running it in -w mode.
# Called as: my @ch_refnums = &findTickers($file_cont, $anndate); sub findTickers { my ($file, $ann_date) = @_; my $match; my $max_month = 0; my $max_year = 0; @temp = split (/\//, $ann_date); my $month = $temp[0]; my $day = $temp[1]; my $year = $temp[2]; if ($month => 10) { $max_month = $month-9; $max_year = $year+1; } elsif ($month < 10) { $max_month = $month+3; } ==> if ($max_year > 1900) { # Error occurrs here. $match = "[$month-$max_month][\/][1-3]?[0-9][$year|$max_year]" ; } else { $match = "[$month-$max_month][\/][1-3]?[0-9][$year]" ; } }
There is more to the sub and prog. However, I think it is somewhere here that the error is actually occurring. This error does not appear to be fatal, but i would want to know why it occurs... Thanks! Jon

Replies are listed 'Best First'.
Re: Unknown problem, throws "Useless use of privt var in void contxt"
by Joost (Canon) on Jan 21, 2008 at 22:41 UTC
      lol. Don't know how I missed that! Thanks
Re: Unknown problem, throws "Useless use of privt var in void contxt"
by bigmacbear (Monk) on Jan 21, 2008 at 22:43 UTC

    I'd take a look at the line

    if ($month => 10)  {

    which actually boils down to

    if ("$month", 10)  {

    since "=>" is the "fat comma" operator, used in defining hashes, whose only distinction from the comma operator is that it ensures its left-hand argument is a string.

    I think you meant to say

    if ($month >= 10)  {

    instead. Not sure why the error appears so far down, but there you are.

      Your explanation of => is not entirely accurate.
      if ($month => 10) { ... }
      is equivalent to
      if ($month, 10) { ... }
      The => operator does NOT wrap whatever appears on the left into a string. Instead, it allows any expression, but if a bareword is used, it turns the bareword into a string. Any other expression is left as-is, not even coerced into being a string. (Actually rolling a list into a hash will coerce the keys into strings unless the hash has some magic.)

      [ e d @ h a l l e y . c c ]

Re: Unknown problem, throws "Useless use of privt var in void contxt"
by Anonymous Monk on Jan 21, 2008 at 22:38 UTC
    Not sure about the error, per say, but DO NOT DO THIS!
    my @ch_refnums = &findTickers($file_cont, $anndate);
    Do this instead:
    my @ch_refnums = findTickers($file_cont, $anndate);
    Only use & if you are overriding one of Perl's built-in functions.
      Thx for that tip, i've been using an ancient tome to perl, and that's how it says to do it. One q, why would it be important to remove the &? thx Jon
Re: Unknown problem, throws "Useless use of privt var in void contxt"
by starX (Chaplain) on Jan 21, 2008 at 22:48 UTC
    Where else is $match accessed? I would understand that warning to mean that you've saved data to a variable that goes out of scope before you try accessing or returning it. Are you forgetting to return $match at the end of this sub? I ask because I don't see you doing so explicitly.
      The problem was that backwards => (meant to be >=). Thanks anyway!

      I still can't believe I got 3 responses in 10 min, though...