open

N

narenkumaraguru

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.
 
B

Brian McCauley

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::eek:bj (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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top