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

Hey

i have opened a file as shown in code and called sub routines which need to access the file , but i can't open the file . I am new to perl so any form of help is appreciated

print " Please input your new csv file \n"; my $ csv_name =<>; my $file_csv="$csv_name"; open(FILE_csv,"<","$file_csv")|| die ("can't open the csv file \n" +); @array_A= column_segregation_Spec_values (1); # function call close FILE;

Replies are listed 'Best First'.
Re: opening a file
by choroba (Cardinal) on Jun 13, 2013 at 14:19 UTC
    There are several issues:
    • After reading from the standard input, the line ends in a newline. This newline is not a part of the filename, so remove it. See chomp.
    • Why do you declare the variable $file_csv? Its value is identical to $csv_name (the space after the $ sigil is tolerated by Perl, but I would recommend removing it).
    • You are closing the FILE filehandle, but opening the FILE_csv one. If you turned warnings on, Perl would tell you.
    • Update: It is easier to send lexical filehandles as parameters to subroutines. Do not use bareword filehandles.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ