Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: write command failing

by roboticus (Chancellor)
on May 08, 2018 at 14:35 UTC ( [id://1214221]=note: print w/replies, xml ) Need Help??


in reply to write command failing

cmora111:

I've looked over your code a bit, and would like to give a better critique of it, but I don't really have the time for that right now. What I had time for, though, was this:

  • Change the bit from:
    my $type = ""; my $name = ""; my $action = "";

    to:
        our ($type,$name,$action);and you'll get some values in your output file. The scoping is getting confused a bit, but I don't know why. (There are a few oddities in your program that make it unclear to me at present.)
  • You didn't provide the source code to X10::Item, so I had to comment out the references to it in the code you did provide, and that seemed to be enough for some simple debugging.
  • In X10::Config::printConfig, your if statement for the ZONE case doesn't set $action so it's using the default value from the HU item, which appears to be undefined for a zone.
  • For debugging output formats, I frequently find it useful to add some unique text at the end of each format line for debugging (to ensure that the correct format line is selected and is printing), kind of like this:
    format fmtZone = @<<<< @<<<<<< @<<<<< dbgZONE $type, $name, $action . format fmtGroup = @<<<< @<<<<<< @<<<<<< dbgGROUP $type, $name, $action .
    Your program was generating a file of blank lines, so without the debug text on the format lines, I couldn't tell which bits of code were actually executing.
  • While I'm personally a fan of the format statements, you'll likely get a few messages suggesting you use printf statements. It's not a bad idea to consider them. Another interesting alternative can be pack. I generally use printf statements because:
    1. the format is immediately next to the code generating the report, and
    2. I frequently generate the formats on the fly to accommodate the data widths in the data for which I'm creating my reports.

I really don't expect to have any time for it today, but if the chance arises, I'll look over the code again later and try to offer a few more pointers.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: write command failing
by cmora111 (Novice) on May 08, 2018 at 23:34 UTC
    Thank you very much for your input. It is very helpful. I am enclosing package X10::Item.pm.
    package X10::Item; #--------------------------------------------------------------------- +---------# # Libraries. + # #--------------------------------------------------------------------- +---------# use 5.006; use version; our $VERSION = qv('0.01'); use lib qw'lib/ ../lib'; use strict; use warnings; use Data::Dumper; #--------------------------------------------------------------------- +---------# # Attributes. + # #--------------------------------------------------------------------- +---------# sub new { my $self = {}; $self->{PARENT} = undef; $self->{TYPE} = undef; $self->{NAME} = undef; $self->{STATE} = undef; $self->{HU} = undef; $self->{DIMLEVEL} = undef; bless($self); return $self; } sub parent { my $self = shift; if (@_) { $self->{PARENT} = shift } return $self->{PARENT}; } sub type { my $self = shift; if (@_) { $self->{TYPE} = shift } return $self->{TYPE}; } sub name { my $self = shift; if (@_) { $self->{NAME} = shift } return $self->{NAME}; } sub state { my $self = shift; if (@_) { $self->{STATE} = shift } return $self->{STATE}; } sub hu { my $self = shift; if (@_) { $self->{HU} = shift } return $self->{HU}; } sub dimlevel { my $self = shift; if (@_) { $self->{DIMLEVEL} = shift } return $self->{DIMLEVEL}; } sub timer { my $self = shift; if (@_) { $self->{TIMER} = shift } return $self->{TIMER}; } 1;
Re^2: write command failing
by cmora111 (Novice) on May 09, 2018 at 01:35 UTC
    I modified printConfig and printList as follows and things are working now without the write format.
    sub printList { my $self = shift; my $fh = shift; foreach my $struct (values %{$self->list}) { $type = $struct->type; $name = $struct->name; $action = $struct->hu; if ( $struct->type =~ /LIGHT|FAN|CAMERA|CHRISTMAS/ ) { printf($fh " %s %-22s %s\n", $struct->type, +$struct->name,$struct->hu); } if ( $struct->type =~ /ZONE/ ) { printf($fh "ZONE %-22s %s\n", $struct->na +me,$struct->hu); printf $fh " SECURITY %-22s %s\n", $struct->na +me,$struct->security; printf $fh " STATE %-22s %s\n", $struct->na +me,$struct->state; printf $fh " BATTERY %-22s %s\n", $struct->na +me,$struct->battery; &printList($struct,$fh); } if ( $struct->type =~ /GROUP/ ) { printf($fh " GROUP %-22s %s\n", $struct->na +me,$struct->hu); printf $fh " SECURITY %-22s %s\n", $struct->na +me,$struct->security; printf $fh " STATE %-22s %s\n", $struct->na +me,$struct->state; printf $fh " BATTERY %-22s %s\n", $struct->na +me,$struct->battery; &printList($struct,$fh); } if ( $struct->type =~ /AREA/ ) { printf($fh " AREA %-22s %s\n", $struct->na +me,$struct->hu); printf $fh " SECURITY %-22s %s\n", $struct->na +me,$struct->security; printf $fh " STATE %-22s %s\n", $struct->na +me,$struct->state; printf $fh " BATTERY %-22s %s\n", $struct->na +me,$struct->battery; &printList($struct,$fh); } } } sub printConfig { my $configfile = shift; my $house = shift; my $space = ""; open(my $fh, ">$configfile"); printf($fh "ZONE %-22s %s\n", $house->name,$house +->hu); printf $fh " SECURITY %-22s %s\n", $house->name,$house +->security; printf $fh " STATE %-22s %s\n", $house->name,$house +->state; printf $fh " BATTERY %-22s %s\n", $house->name,$house +->battery; &printList($house,$fh,$house->type); close($fh); }
    The Config file now prints the way I wanted. I am sure there is a more compact way to do it though.
    ZONE Master G1 SECURITY Master OFF STATE Master OFF BATTERY Master 20180410 ZONE Outdoor G2 SECURITY Outdoor OFF STATE Outdoor OFF BATTERY Outdoor 20180410 GROUP Frontdoor G5 SECURITY Frontdoor OFF STATE Frontdoor OFF BATTERY Frontdoor 20180410 GROUP Deck G6 SECURITY Deck OFF STATE Deck OFF BATTERY Deck 20180410 GROUP Basement G7 SECURITY Basement OFF STATE Basement OFF BATTERY Basement 20180410 ZONE Indoor G3 SECURITY Indoor OFF STATE Indoor OFF BATTERY Indoor 20180410 GROUP SecondFloor G10 SECURITY SecondFloor OFF STATE SecondFloor OFF BATTERY SecondFloor 20180410 GROUP FirstFloor G9 SECURITY FirstFloor OFF STATE FirstFloor OFF BATTERY FirstFloor 20180410 AREA Livingroom G14 SECURITY Livingroom OFF STATE Livingroom OFF BATTERY Livingroom 20180410 AREA EntryWay G13 SECURITY EntryWay OFF STATE EntryWay OFF BATTERY EntryWay 20180410 LIGHT Entryway Light N1 GROUP Basement G11 SECURITY Basement OFF STATE Basement OFF BATTERY Basement 20180410 GROUP Attic G8 SECURITY Attic OFF STATE Attic OFF BATTERY Attic 20180410 ZONE Garage G4 SECURITY Garage OFF STATE Garage OFF BATTERY Garage 20180410

      my $f0="%-22s %s\n"; my $f1='%-22s' .$f0; my $f2=' %-18s' .$f0; my $f3=' %-14s' .$f0; my $f4=' %-10s' .$f0; sub printList { my $self = shift; my $fh = shift; foreach my $struct (sort values %{$self->list}) { my $type = $struct->type; if ( $type =~ /LIGHT|FAN|CAMERA|CHRISTMAS/ ) { printf $fh $f4, $type,$struct->name,$struct->hu; } else{ my ($ftop,$fline); if ( $type =~ /ZONE/ ) { $ftop=$f1; $fline=$f2;} if ( $type =~ /GROUP/ ) { $ftop=$f2; $fline=$f3;} if ( $type =~ /AREA/ ) { $ftop=$f3; $fline=$f4;} printSet($struct,$fh,$ftop,$fline); printList($struct,$fh); } } } sub printSet { my $struct=shift; my $fh=shift; my $ftop=shift; my $fline=shift; $name = $struct->name; printf $fh $ftop, $struct->type,$name,$struct->hu; for my $method (qw/security state battery/) { printf $fh $fline,uc($ +method),$name,$struct->$method; } } sub printConfig { my $configfile = shift; my $house = shift; my $space = ""; open(my $fh, ">$configfile"); printSet($house,$fh,$f1,$f2); printList($house,$fh,$house->type); close($fh); }
      Of most interest here is that "methods" are nothing but string literals.

Log In?
Username:
Password:

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

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

    No recent polls found