For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2005 > open









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author open
narenkumaraguru@yahoo.co.uk

2005-05-26, 8:56 am

Hi,
I'm new to OOP. I'm having trouble with open. It is not behaving as
expected.
I've uploaded a t/c. It is available in the following link...
www.geocities.com/narenkumaraguru/test.tar.gz
Please download it and try to run it.
What I find is that the file t1 is empty and t2 is having both the
lines. Rather t1 and t2 should have one line each.
Please try yourself and get back...
Thanks,
Naren.

Brian McCauley

2005-05-26, 3:58 pm



narenkumaraguru@yahoo.co.uk wrote:

> I'm new to OOP. I'm having trouble with open. It is not behaving as
> expected.


Your problem has nothing to do with OOP or open.

> I've uploaded a t/c. It is available in the following link...
> www.geocities.com/narenkumaraguru/test.tar.gz


Good marks for a minimal but complete stript. But you loose marks by
not putting it in your Usenet post. You should always aim to minimize
the effort that people will need to put into help you.

For the help of others I've included your examples.

Please put "use strict" and "use warnings" in all your Perl source files
so that you get as much help as possible from perl. Remeber, you should
always aim to minimize the effort that _people_ will need to put into
help you (but maximise the effort machines will put in).

Perl will then point out your mistake - that you are using the
undeclared package variable %myclass::obj (global to your package)
rather than the lexically scoped variable $obj.

$obj{fh} should say $$obj{fh} or $obj->{fh}

Note: FileHandle has been replaced by IO::File and that in turn has been
rendered largley redundant by autovivification in open().

open( $obj->{fh},'|-',"cat > t$arg") or die "Can't open file with $arg\n";

(I'm assuming the usless use of cat is just an example)

----------test.pl--------------
#! /usr/bin/perl -w -I.

use myclass;

my $mc1 = new myclass;
my $mc2 = new myclass;

$mc1->fopen(1);
$mc2->fopen(2);

$mc1->print(1);
$mc2->print(2);

$mc1->fclose;
$mc2->fclose;
-------------------------------

-------myclass.pl--------------
use FileHandle;

package myclass;

sub new {
my $class = shift;
my $obj = {};
return bless $obj, $class;
}

sub fopen {
my $obj = shift;
my $arg = shift;

$obj{fh} = new FileHandle;
$obj{fh}->open(" | cat > t$arg") or die "Can't open file with $arg\n";

}

sub print {
my $obj = shift;
my $arg = shift;

$obj{fh}->print("inputs to FH $arg\n");
}

sub fclose {
my $obj = shift;
$obj{fh}->close;
}

1;
----------------------------------------



> Please download it and try to run it.
> What I find is that the file t1 is empty and t2 is having both the
> lines. Rather t1 and t2 should have one line each.
> Please try yourself and get back...
> Thanks,
> Naren.
>


Tad McClellan

2005-05-27, 3:59 am

narenkumaraguru@yahoo.co.uk <narenkumaraguru@yahoo.co.uk> wrote:

> I've uploaded a t/c.



What is a "t/c" ?


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
narenkumaraguru@yahoo.co.uk

2005-05-30, 8:57 am

a testcase...

Tad McClellan

2005-05-30, 3:57 pm

narenkumaraguru@yahoo.co.uk <narenkumaraguru@yahoo.co.uk> wrote:

> a testcase...



A killfile entry...


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com