Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

user input file

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


I have a small issue that I have written a code where I have predefined my file , but now I want that the file should be entered by the user . So this is my code

my $file="Working_On.csv"; open(FILE,"<","$file"); my @lines=<FILE>;

so what I am doing is converting that csv file to an array . please tell me a way so that Working_On.csv should be entered by the user

Replies are listed 'Best First'.
Re: user input file
by kennethk (Abbot) on Jun 19, 2013 at 13:36 UTC
    The correct method for taking command line parameters into a Perl script is to use the @ARGV array. Your example might look like
    open(FILE,"<", $ARGV[0]) or die "Open failed for $ARGV[0]: $!"; + my @lines=<FILE>;
    which the user would invoke as
    $ perl Working_On.csv
    Note as well that I've added a test to see if the file open succeeded, and added a diagnostic in case it didn't. See Simple Opens in perlopentut.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      I am working on windows , not unix so there will be no invocation . I tried your way but it does not work , may be because of OS

        If you drag a csv file on to the script, it can be configured to read the file name from @ARGV. How are you planning to have the user invoke it?


Re: user input file
by 2teez (Priest) on Jun 19, 2013 at 15:38 UTC

    Hi torres09,
    please tell me a way so that Working_On.csv should be entered by the user

    If what you want is the user responding to "somewhat" like a prompt. The you can do like thus:

    use warnings; use strict; my $file; #create an inifite loop while (1) { # ask the user for filename print "Enter filename > "; chomp( $file = <STDIN> ); # Exit the loop, if the # filename exists and it's a file # else redo if ( -e $file && -f _ ) { #Note HERE last; } else { redo } } open my $fh, '<', $file or die "can't open file: $!"; ...
    Or you check in CPAN for module that does exactly that.
    For the file test on the file entered by the user you can check this for more.

    Update: Note: If you use Perl 5.9.1 or better, you could actually write: if ( -e -f $file ) { ..
    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: user input file
by kcott (Canon) on Jun 19, 2013 at 14:15 UTC

    G'day torres09,

    Update: Having posted this, I now see (from Re^4: user input file) that you only want users to enter the filename, not the data. Your OP asked about "file", not "filename" and "my @lines=<FILE>;" (in your posted code) suggested you were interested in data — you can ignore the remainder of my response (which is now hidden within <spoiler> tags).

    -- Ken

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2016-08-30 11:53 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (417 votes). Check out past polls.