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

I'm new at this I get a strange value for output. Thanks ill perltut and use your hints

Replies are listed 'Best First'.
Re: using Open
by davorg (Chancellor) on Jul 26, 2004 at 14:46 UTC

    Ok, what you've done there is to open the file and associate it with a filehandle. The filehandle is in $info which is why you're getting the value that you don't understand. To get the data from your file, you need to read from the filehandle using the file input operator.

    while (<$info>) { # puts one line of file into $_ print "$_"; }

    or, to get all of the data into an array

    @file = <$info>;

    You might find it useful to read "perldoc perlopentut".

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: using Open
by diotalevi (Canon) on Jul 26, 2004 at 14:41 UTC
    $tmp contains "your text\n". Use chomp to remove the trailing newline.
Re: using Open
by gaal (Parson) on Jul 26, 2004 at 14:45 UTC
    Please use strict.

    When you say open $info, $file $info gets to hold the filehandle, not the content of the file. When you print it, its value becomes stringified.

    What you want is to read from the file, and read that. To avoid reading the whole file to memory at once, let's do it line by line using the <> operator:

    while (<$info>) { # $_ holds a line print; # print w/o arguments prints $_ }
    More details in perlopentut.
Re: using Open
by wfsp (Abbot) on Jul 26, 2004 at 14:53 UTC
    Also, $info is a file handle, hence the strange output. You can work with the contents of the file like this:
    chomp ( my @output = <$info> ); print "$_\n" for @output;
Re: using Open
by pbeckingham (Parson) on Jul 26, 2004 at 15:25 UTC

    This does what you intended, I think. Consider using taint because of the way you are interacting with the file system directly without untainting your inputs.

    print "whats the File name ? "; chomp (my $tmp = <STDIN>); my $file = "/home/wanderi/$tmp"; open (my $info, '<', $file) or die "cant open $file"; print while <$info>;