Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Useless use of private variable in void context

by bh_perl (Monk)
on Jun 22, 2010 at 04:13 UTC ( #845835=perlquestion: print w/replies, xml ) Need Help??

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

Hi..
I got an error message as below after execute my program:-
Useless use of private variable in void context at /home/ASUS/bin/burs +t2arbor.pl line 114. Useless use of private variable in void context at /home/ASUS/bin/burs +t2arbor.pl line 131.

Otherwise, I have used function converTime to convert date into unixtime in perl, is it correct ?

This is my program
#!/usr/bin/perl -w use warnings; use strict; use POSIX qw(strftime); use Getopt::Long; use Cwd; use POSIX qw(floor ceil); use constant BURSTABLE => "BURSTABLE"; use constant BURSTABLE_SELECT => "BURSTABLE_SELECT"; my ($rec, $all, $arborcsv, $arbor, $indir, $outdir, $arcdir, $fname); my ($dur, $recType, $rec_size, $num_record, $application, $version, $d +ata_record, $default); my $creation_dt = strftime "%Y%m%d%H%M%S", localtime; my $fix = "100"; my $fix2 = "1"; GetOptions ( "all" => \$all, "arborcsv" => \$arborcsv, "arbor" => \$arbor, "i=s" => \$indir, "o=s" => \$outdir, "a=s" => \$arcdir, "f=s" => \$fname, ) or usage(); my @hdrsize = (3, 14, 4, 7, 16, 5, 12); my @trlsize = (6, 28, 8, 14, 32, 10, 24); my @min_burstable = (); my @max_burstable = (); my @tiu_burstable = (); my @min_burstable_select = (); my @max_burstable_select = (); my @tiu_burstable_select = (); sub usage { print "USAGE: $0 -[all|arbor|csv] -f <file name>\n"; } sub convertTime { #expects time in yyyy-mm-dd hh:mm:ss format my( $time ) = shift; my( $year, $month, $day, $hour, $minute, $sec ) = $time =~ /(\d+)- +(\d+)-(\d+) (\d+):(\d+):(\d+)/; my $oneday = 24 * 3600; #for convenience my $utime = $sec + ($minute * 60) + ($hour * 3600); ## time in sec +onds on the day in question $year -= 1970; my @months = (31,28,31,30,31,30,31,31,30,31,30,31); for (my $i=0; $i < ($month-1); $i++ ) { $utime += ($months[$i] * $oneday); } $utime += ((($year - ($year%4))/4) * $oneday); ## take leap years +into account if( ($year%4)==0 && $month < 3 ) { $utime -= $oneday; } $utime += (($day-1) * $oneday); $utime += ($year * 365 * $oneday); $utime -= 8*3600; # TimeZone for Malaysia return $utime; } sub inttohex { my $int = shift; return sprintf ("%02X", $int); } sub read_burstable { my ($data, $min, $max, $tiu); my $num = 0; open (DATA, "/home/ASUS/burstable_select.tab"); while ($data = <DATA>) { chomp($data); next if $data =~ /^#/; ($tiu, $min, $max) = split(/\s/,$data); $min_burstable_select[$num] = $min; $max_burstable_select[$num] = $max; $tiu_burstable_select[$num] = $tiu; $num++; } close(DATA); $num = 0; open (DATA, "/home/ASUS/burstable.tab"); while ($data = <DATA>) { chomp($data); next if $data =~ /^#/; ($tiu, $min, $max) = split(/\s/,$data); $min_burstable[$num] = $min; $max_burstable[$num] = $max; $tiu_burstable[$num] = $tiu; $num++; } close(DATA); } sub get_burstable_tiu { my $tiu = shift; my ($min, $max, $tec); my $i = 0; for $min (@min_burstable) { $max = $max_burstable[$i]; if ($tiu => $min && $tiu < $max) { $tec = $tiu_burstable[$i]; last; } $i++; } return $tec; } sub get_burstable_select_tiu { my $tiu = shift; my ($min, $max, $tec); my $i = 0; for $min (@min_burstable_select) { $max = $max_burstable_select[$i]; if ($tiu => $min && $tiu < $max) { $tec = $tiu_burstable_select[$i]; last; } $i++; } return $tec; } sub create_header { if ($arborcsv || $all) { $recType = "HDR"; $creation_dt = strftime "%Y%m%d%H%M%S", localtime; $rec_size = "0000"; $num_record = "0000000"; $application = "NV2:MCS R3.0"; $version = "01.00"; $data_record = "TE2"; printf (CSV "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", $recType, $creatio +n_dt, $rec_size, $num_record, $application, $version, $data_record, " + ", " "); } elsif ($arbor || $all) { $recType = sprintf("%3s","HDR"); $creation_dt = sprintf ("%14s", strftime "%Y%m%d%H%M%S", local +time); $rec_size = sprintf ("%s","0000"); $num_record = sprintf ("%s", "0000000"); $version = sprintf ("%5s", "01.00"); $data_record = sprintf ("%3s", "TE2"); $default = sprintf ("%12s", " "); printf (DAT pack 'A3 A14 A4 A7 A16 A5 A3 A12 h1', $recType,$cr +eation_dt,$rec_size,$num_record,"NV2:MCS R3.0",$version,$data_record, +$default,"A"); } } sub create_data { my ($usage_cnt, $burst, $usage_final, $tiu); my ($yy, $mm, $dd, $HH, $MI, $SS, $new_dt, $unix_dt); $rec = 0; open (DATA, "$indir/$fname"); while (my $data = <DATA>) { chomp($data); my ($insert_date,$id,$service_id,$start_date,$end_date,$usage_ +95,$min_cap,$max_cap,$product_name) = split(/;/, $data); $min_cap = floor($min_cap); $max_cap = floor($max_cap); my $burst = ($min_cap > 0) ? BURSTABLE_SELECT : BURSTABLE; if ($burst eq BURSTABLE) { $usage_final = ($usage_95 >= $max_cap) ? floor($max_cap) : + floor($usage_95); $tiu = get_burstable_tiu($usage_final); } else { if ($usage_95 >= $max_cap) { $usage_cnt = $max_cap - $min_cap; } else { $usage_cnt= $usage_95 - $min_cap; } $usage_final = ($usage_cnt > 0) ? $usage_cnt : 0; $tiu = get_burstable_select_tiu($min_cap); } my ($equipment_no,$dt,$point_target,$point_origin,$primary_val +ue) = ($service_id,$start_date,$min_cap,$service_id,$usage_final); if ($arbor || $all) { $dur = sprintf ("%08s", inttohex($usage_final)); ($yy, $mm, $dd, $HH, $MI, $SS) = $dt =~ /(\d+)-(\d+)-(\d+) + (\d+):(\d+):(\d+)/; #expects time in yyyy-mm-dd hh:mm:ss format $new_dt = sprintf("$yy-$mm-$dd $HH:$MI:$SS"); $unix_dt = inttohex(convertTime($new_dt)); print (DAT pack "H*", $tiu); print (DAT pack "A16", $equipment_no); print (DAT pack "H*", $unix_dt); print (DAT pack "A16", $point_target); print (DAT pack "A16", $equipment_no); print (DAT pack "H*", $dur); print (DAT pack "H2", "64"); print (DAT pack "H2", "01"); } elsif ($arborcsv || $all) { print (CSV "$rec,$tiu,$equipment_no,$dt,$point_target,$equ +ipment_no,$usage_final,$fix,$fix2\n"); } $rec++; } close(DATA); } sub create_trailer { if ($arborcsv || $all) { $recType = "TRA"; $rec_size = "0000"; $creation_dt = strftime "%Y%m%d%H%M%S", localtime; $num_record = sprintf ("%07s",$rec); $application = "NV2:MCS R3.0"; $version = "01.00"; $data_record = "TE2"; printf (CSV "%s,%s,%s,%s,%s,%s,%s,%s,%s", $recType, $creation_ +dt, $rec_size, $num_record, $application, $version, $data_record, " " +, " "); } elsif ($arbor || $all) { $recType = sprintf("%3s","TRA"); $creation_dt = sprintf ("%14s", strftime "%Y%m%d%H%M%S", local +time); $rec_size = sprintf ("%4s","0000"); $num_record = sprintf ("%07s",$rec); $version = sprintf ("%5s", "01.00"); $data_record = sprintf ("%3s", "TE2"); $default = sprintf ("%12s", " "); printf (DAT pack 'A3 A14 A4 A7 A16 A5 A3 A12 h1', $recType,$cr +eation_dt,$rec_size,$num_record,"NV2:MCS R3.0",$version,$data_record, +$default,"A"); } } if (!defined($indir)) { $indir = getcwd; } read_burstable(); if ($arcdir) { my $fname_arc = $fname . "." . $creation_dt; system("cp $indir/$fname $arcdir/$fname_arc"); } if ($arborcsv || $all) { (my $fname_csv = $fname) =~ s/.csv/.cst/g; open (CSV, ">$outdir/$fname_csv"); } if ($arbor || $all) { (my $fname_dat = $fname) =~ s/.csv//g; open (DAT, ">$outdir/$fname_dat"); } create_header(); create_data(); create_trailer(); close(CSV) if ($arborcsv || $all); close(DAT) if ($arbor || $all);

Replies are listed 'Best First'.
Re: Useless use of private variable in void context
by AnomalousMonk (Archbishop) on Jun 22, 2010 at 04:50 UTC
    $tiu => $min && $tiu < $max

    My guess is, this statement (on the lines mentioned in the warnings) should be
        $tiu>= $min && $tiu < $max

    Note that  => is the 'fat comma', not the  >= 'greater than or equal to' comparator.

    Updates:

    1. BTW: Please use  <readmore> tags when posting a large chunk of code such as that in the OP. Please see Markup in the Monastery.
    2. The statement
          $tiu => $min && $tiu < $max
      in scalar context (as imposed by the  if conditional) is the same as
          ($tiu, $min && $tiu < $max)
      in which the  $tiu scalar is evaluated and its value is thrown away (i.e., 'used uselessly'), and then the clause  $min && $tiu < $max is evaluated and becomes the final value of the statement. I doubt the latter clause is what you intended.

Re: Useless use of private variable in void context
by Khen1950fx (Canon) on Jun 22, 2010 at 07:29 UTC
    I made a few adjustments, and here's the result:
      Thank you very much

      But Khen1950fx's code has  $tiu, $min && $tiu < $max as an  if statement condition expression in two places. (I don't understand why this form is used in place of  $tiu => $min && $tiu < $max of the OP code; am I missing something?)

      Unless evaluation of  $tiu has some side-effect (e.g., if it was a tied scalar – which it doesn't seem to be: as far as I can see, it's always a humble lexical), its evaluation is still useless and the 'useless use of ...' warning will still be generated.

        I think the confusion here is that the OP's $tiu => $min && $tiu < $max is equivalent to $tiu, $min && $tiu < $max.

        What was intended, on the other hand is almost certainly $tiu >= $min && $tiu < $max

        Key issue: >= is very different from =>. The former compares numerically. The latter is a fancy way to say comma.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2023-02-05 13:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?