Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Useless use of private variable in void context

by Khen1950fx (Canon)
on Jun 22, 2010 at 07:29 UTC ( [id://845848]=note: print w/replies, xml ) Need Help??


in reply to Useless use of private variable in void context

I made a few adjustments, and here's the result:
#!/usr/bin/perl use strict; use Cwd; use Getopt::Long; use POSIX qw(strftime); 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, ); 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 = \86400; my $utime = $sec + $minute * \60 + $hour * \3600; ## time in secon +ds 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; if( ($year %\4) == \0 and $month < \3 ) { $utime -= $oneday; } $utime += $day - \1 * $oneday; $utime += $year * 365 * $oneday; $utime -= \28800; # 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'); close DATA; while (defined($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 (defined($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); foreach $min (@min_burstable) { my $i = \0; $max = $max_burstable[$i]; if ($tiu, $min && $tiu < $max) { $tec = $tiu_burstable[$i]; last; } } return $tec; } sub get_burstable_select_tiu { my $tiu = shift; my($min, $max, $tec); foreach $min (@min_burstable_select) { my $i = \0; $max = $max_burstable_select[$i]; if ($tiu, $min && $tiu < $max) { $tec = $tiu_burstable_select[$i]; last; } } return $tec; } sub create_header { if ($arborcsv or $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, $creation +_dt, $rec_size, $num_record, $application, $version, $data_record, ' +', ' '; } elsif ($arbor or $all) { $recType = sprintf('%3s','HDR'); $creation_dt = sprintf ('%14s', strftime '%Y, %m, %d, %H, %M, +%S', localtime); $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,$creation_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'); my $data; while (defined($data = <DATA>)) { chomp $data; my($insert_date, $id, $service_id, $start_date, $end_date, $usage_95, $min_cap, $max_cap, $product_name) = split(/;/, $da +ta); $min_cap = floor($min_cap); $max_cap = floor($max_cap); $burst = $min_cap > 0 ? BURSTABLE_SELECT : BURSTABLE; if ($burst eq BURSTABLE) { $usage_final = $usage_95 >= $max_cap ? floor($max_cap) : f +loor($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_value) = ($service_id,$start_date,$min_ca +p,$service_id,$usage_final); if ($arbor or $all) { $dur = sprintf ('%08s', inttohex($usage_final)); ($yy, $mm, $dd, $HH, $MI, $SS) = $dt =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/; $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 or $all) { print CSV "$rec, $tiu, $equipment_no, $dt, $point_target, $equipment_no, $usage_final, $fix, $fix2\n"; } ++$rec; } close DATA; } sub create_trailer { if ($arborcsv or $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 or $all) { $recType = sprintf('%3s','TRA'); $creation_dt = sprintf('%14s', strftime('%Y,%m,%d,%H,%M,%S', l +ocaltime)); $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, $creation_dt, $rec_size, $num_record, 'NV2:MCS R3.0', $version, $data_record, $default, 'A'; } } if (not defined($indir)) { $indir = getcwd(); } read_burstable(); if ($arcdir) { my $fname_arc = $fname . '.' . $creation_dt; system("cp $indir/$fname $arcdir/$fname_arc"); } if ($arborcsv or $all) { (my $fname_csv = $fname) =~ s/.csv/.cst/g; open CSV, ">$outdir/$fname_csv"; } if ($arbor or $all) { (my $fname_dat = $fname) =~ s/.csv//g; open DAT, ">$outdir/$fname_dat"; } create_header(); create_data(); create_trailer(); close CSV if $arborcsv or $all; close DAT if $arbor or $all; __DATA__

Replies are listed 'Best First'.
Re^2: Useless use of private variable in void context
by bh_perl (Monk) on Jun 22, 2010 at 11:23 UTC
    Thank you very much
Re^2: Useless use of private variable in void context
by AnomalousMonk (Archbishop) on Jun 22, 2010 at 19:17 UTC

    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: note [id://845848]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found