adalamre has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Opsware::NAS::Connect; use File::Compare; my $hostname = $ARGV[0]; my @output; my $con = Opsware::NAS::Connect->new(-user => "admin", -pass => "pass +word", -host => $hostname, -port => "8023"); $con->login(); @output = $con->cmd("help list acl"); open(DATA, "+>", "file.txt") or die "Couldn't open: $!"; select DATA; print join("\n", @output); close DATA; my $file1 = "C:\\newperl\\myoutput.txt"; my $file2 = "C:\\newperl\\file.txt"; if (compare("$file1","$file2") == 0) { print "They're equal\n"; } else { print "failed\n"; } $con->logout(); undef $con; exit(0)
I have copied the content of file.txt to myoutput.txt manually and then trying to compare two files when executing above program; getting below error print() on closed filehandle DATA

Replies are listed 'Best First'.
Re: Getting error when trying to compare two files
by Happy-the-monk (Canon) on Nov 13, 2015 at 07:57 UTC

    DATA is still selected for your print commands, but no longer open. To fix it, insert

    my $old_fh = select(STDOUT);

    before your line

    select DATA;

    Then right before or right after your line saying

    close DATA;

    insert

    select($old_fh);

    That should make the error go away.


    (edit:) Alternatively do not use select but print explicitly to the filehandle instead:

    replace this bit of your code

    select DATA; print join("\n", @output);

    with this bit

    print DATA join("\n", @output);

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

      Yes, this worked. Thank you
      Can you please let me know what needs to be done for comparing the content of the files line by line

        What is the goal you are trying to achieve?

        If you want to know the differences between the files, Algorithm::Diff is better suited than File::Compare.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

        Sure, we can help on that, but first tell us what you really want: do you want to just report that the files are equal or not equal, or do you want to just report where (which line) the first difference is found, or do you want to list all the differences? The first two cases are fairly easy, the last one is more complicated and implies some pre-requirements.
Re: Getting error when trying to compare two files
by stevieb (Canon) on Nov 13, 2015 at 08:17 UTC
    my $file = 'file.txt'; open my $wfh, '+>', $file or die "Couldn't open file $file: $!"; print $wfh join "\n", @output; close $wfh;

    Don't use barename file handles. They are global, and they're unfriendly for a few reasons. Put your handle into a variable.

Re: Getting error when trying to compare two files
by stevieb (Canon) on Nov 13, 2015 at 07:46 UTC

    Welcome adalamre to the Monastery!

    It is impossible to tell what your question is here... please edit your post by putting all code and data within <code></code> tags.

    If you click on the title of your post, you'll be presented with an edit window down below. You've got a nice chunk of code there, but it'll be viewed by more if its presented properly.

    Update: OP fixed the post wonderfully.