open

Discussion in 'Perl Misc' started by narenkumaraguru@yahoo.co.uk, May 26, 2005.

  1. Guest

    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.
    , May 26, 2005
    #1
    1. Advertising

  2. 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::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;
    ----------------------------------------



    > 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, May 26, 2005
    #2
    1. Advertising

  3. <> wrote:

    > I've uploaded a t/c.



    What is a "t/c" ?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 27, 2005
    #3
  4. Guest

    a testcase...
    , May 30, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page