Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 20th, 2013, 01:15 AM
fuzzybunny's Avatar
fuzzybunny fuzzybunny is offline
C Neophyte.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Melbourne Australia
Posts: 403 fuzzybunny User rank is Sergeant Major (2000 - 5000 Reputation Level)fuzzybunny User rank is Sergeant Major (2000 - 5000 Reputation Level)fuzzybunny User rank is Sergeant Major (2000 - 5000 Reputation Level)fuzzybunny User rank is Sergeant Major (2000 - 5000 Reputation Level)fuzzybunny User rank is Sergeant Major (2000 - 5000 Reputation Level)fuzzybunny User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 1 h 34 m 55 sec
Reputation Power: 45
Storing handles in hashrefs

I'm trying to put together a class to act as a "reporting" class that I can use to sort my report files out

1. Am I reinventing the wheel?

2. How do I store a filehandle in a blessed hash in a way that works?

from this quote: "If you're storing handles in an array or hash, or in general whenever you're using any expression more complex than a bareword handle or a plain, unsubscripted scalar variable to retrieve it, you will have to use a block returning the filehandle value instead,"

What am I doing wrong?


perl Code:
Original - perl Code
  1.  
  2. use strict;
  3. use warnings;
  4.  
  5. package Reporter {
  6.  
  7.     sub new {
  8.         my ($class, $fileName) = @_;               
  9.         my $self = {};       
  10.         bless $self, $class;       
  11.         $self->open($fileName) if(defined $fileName);      
  12.         return $self;
  13.     }
  14.    
  15.    
  16.     sub printReport {
  17.         my($self, $str) = @_;
  18.         if(defined $self->{fh} {           
  19.             print {$self->{fh}} $str;
  20.         }
  21.     }
  22.    
  23.     sub open {
  24.         my($self, $fileName) = @_;   
  25.        
  26.         open my $fh, "<", $fileName or die "could not open $fileName to write log";  
  27.         $self->{fh} = $fh;
  28.     }
  29.    
  30.     sub close {
  31.         my($self) = @_
  32.         close $self->{fh};
  33.     }
  34.  
  35. }
  36.  
  37. 1;
  38.  
  39.  
  40.  
__________________
For one to know everything, first one must accept he knows nothing.

Reply With Quote
  #2  
Old March 20th, 2013, 08:44 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,650 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 2 h 37 m 31 sec
Reputation Power: 1170
You have several syntax issues which are causing compilation errors.

c:\testing>perl -c fuzzybunny.pl
Quote:
syntax error at fuzzybunny.pl line 20, near "}"
syntax error at fuzzybunny.pl line 35, near "}"
fuzzybunny.pl had compilation errors.


In addition to those 2 errors, your syntax on the 'package' statement is wrong. That statement should terminate with a semicolon; it is not a block statement.

Try this (untested) adjusted version.
Code:
package Reporter;

use strict;
use warnings;


sub new {
    my ($class, $fileName) = @_;
    my $self = {};
    bless $self, $class;
    $self->open($fileName) if(defined $fileName);
    return $self;
}

sub printReport {
    my($self, $str) = @_;
    if(defined $self->{fh}) {
        print {$self->{fh}} $str;
    }
}

sub open {
    my($self, $fileName) = @_;

    open my $fh, "<", $fileName or die "could not open $fileName to write log $!";
    $self->{fh} = $fh;
}

sub close {
    my($self) = @_;
    close $self->{fh};
}

1;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Storing handles in hashrefs

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap