Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

csv file is not opening using use strict

by ulaksh (Acolyte)
on Sep 06, 2012 at 22:58 UTC ( #992205=perlquestion: print w/ replies, xml ) Need Help??
ulaksh has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to open csv file in windows but I am getting the error "Bareword "file1" not allowed while "strict subs" in use at csvfile.pl line 4."
use warnings; use strict; my $file_c = "file1.csv"; open(my $data, '<', $file_c) or die "Could not open $!\n "; while (my $line = <$data>) { next if ($. == 1); chomp $line; my @fields = split "," , $line; print"$fields[0] fields[1]"; }

my output show

Could not open No such file or directory at csvfile.pl line 4.

Please let me know where I am doing the mistake, script is running on windows

Comment on csv file is not opening using use strict
Download Code
Re: csv file is not opening using use strict
by Rudolf (Monk) on Sep 06, 2012 at 23:13 UTC

    If the file exists your program should open it fine, atleast mine does (win7), however I think your mistake may be in the opening statement. the parameter '<' is the read only .. if the file has not yet been created and you want to create it use the '>' param. also the '>>' is used for appending. Sory if this doesnt help I am thinking of other reasons why you might be getting yelled at.

    open(FILE,'>',"file.csv") or die;

      I don't understand; the OP clearly wants to read a file. Why suggest a change that will write a file? If the file doesn't exist, the code as posted will give an error message to that effect.

Re: csv file is not opening using use strict
by chromatic (Archbishop) on Sep 06, 2012 at 23:35 UTC
    I am getting the error "Bareword "file1" not allowed while "strict subs" in use at csvfile.pl line 4."

    The code you posted doesn't give that error. What's different between the code you're running and the code you posted?


    Improve your skills with Modern Perl: the free book.

Re: csv file is not opening using use strict
by Khen1950fx (Canon) on Sep 07, 2012 at 00:31 UTC
    The problem is in print:
    print "$fields[0] fields[1]";
    That shouuld be
    print "$fields[0] $fields[1]";
    Note that my @fields should be parenthesized to get a list; also, you opened $data, so don't forget to close it.
    #!/usr/bin/perl -l BEGIN { $| = 1; $^W = 1; } use strict; use autodie; use warnings; my $file_c = '/root/Desktop/file1.csv'; open my $data, '<', $file_c or die "Could not open file: $!"; while ( defined( my $line = <$data> ) ) { do { next if ( $. == 1 ); chomp $line; my (@fields) = split( /,/, $line, 0 ); print "$fields[0] $fields[1]"; }; } close $data;

      That is a problem. It is not the problem. The missing $ does not cause any error or warning message to be generated because it is in a string.

      True laziness is hard work
      print "$fields[0] fields1";

      That's not going to cause a bareword error under strict.

      BEGIN { $| = 1; $^W = 1; }

      Why? Especially $^W?

      Note that my @fields should be parenthesized to get a list...

      Why? Under what circumstances does the declaration of an array not provide list context?

      while ( defined( my $line = <$data> ) )

      defined is unnecessary here because Perl will add it for you.

      ... also, you opened $data, so don't forget to close it.

      The filehandle's stored in a lexical variable and it's the end of the program and you're not bothering to check the return value (for a filehandle opened for reading!), and the filehandle's at the end of the file at this point, and you have autodie in effect, so why bother?

Re: csv file is not opening using use strict
by aitap (Deacon) on Sep 07, 2012 at 19:09 UTC
    Well, where does the file exist? What is the working directory of the script you are running? Try:
    use feature 'say'; say for <*>;
    This code will print all file names in the current working directory of the Perl program running, and you will be able to see whether Perl can see this file there, or not.. Perhaps your file is actually in another directory. You can use @ARGV to get the file names from the command line parameners instead of hardcoding them in the script. You can also use Cwd to get the current working directory and use chdir to change it (usually it's not needed).
    Sorry if my advice was wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992205]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2014-09-30 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (385 votes), past polls