Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

opening a file

by torres09 (Acolyte)
on Jun 13, 2013 at 14:08 UTC ( #1038748=perlquestion: print w/replies, xml ) Need Help??
torres09 has asked for the wisdom of the Perl Monks concerning the following question:


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 (Bishop) 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.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038748]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-06-24 11:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.