Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Putting a filename as a parameter to a perl script then reading from it in the code?

by Mikeyb0y (Initiate)
on Dec 07, 2012 at 11:07 UTC ( #1007712=perlquestion: print w/ replies, xml ) Need Help??
Mikeyb0y has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am learning perl but come from an ICL vme background so our scl scripting is although in some ways similar the way it handles files is easy. How can i write a script that access a filename supplied by the user as the only parameter? then read each line and put the csv fields into variables. if anyone has any quick code they could let me use i would be grateful i just need to crack this and the rest should be easy i hope

Comment on Putting a filename as a parameter to a perl script then reading from it in the code?
Re: Putting a filename as a parameter to a perl script then reading from it in the code?
by moritz (Cardinal) on Dec 07, 2012 at 11:31 UTC
Re: Putting a filename as a parameter to a perl script then reading from it in the code?
by Utilitarian (Vicar) on Dec 07, 2012 at 12:39 UTC
    ICL VME, >shudder<that brings back memories...
    $ARGV[0] is the first paramter supplied to your script.
    When you have to parse a common file format, the first response should be to search CPAN (or for a faster response, metacpan.org)for the format, there you will discover Text::CSV a module which handles all the edge cases that may arise in your data.

    Welcome to Perl, you'll find it much handier to script in than scl once you get the hang of it...

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Putting a filename as a parameter to a perl script then reading from it in the code?
by johngg (Abbot) on Dec 07, 2012 at 13:06 UTC

    The nice thing about Perl is that it very often does what you want without you having to jump through hoops. If you supply a file name (or multiple file names) as an argument, Perl will automatically open the file for you so you can read it using an empty <> (readline) function. The following steps create a simple CSV file and an equally simple script to read it.

    $ cat > xxx.csv Fred,male,25 Beth,female,31 Joe,male,22 $ cat > xxx #!/usr/bin/perl # use strict; use warnings; while ( <> ) # Read the file supplied as argument { chomp; # Remove line terminator my( $name, $sex, $age ) = split m{,}; printf qq{Name: %s\n Sex: %s\n Age: %s\n-----\n}, $name, $sex, $age; } $ chmod +x xxx $ ./xxx xxx.csv Name: Fred Sex: male Age: 25 ----- Name: Beth Sex: female Age: 31 ----- Name: Joe Sex: male Age: 22 ----- $

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Putting a filename as a parameter to a perl script then reading from it in the code?
by space_monk (Chaplain) on Dec 07, 2012 at 13:33 UTC

    Please use Text::CSV or one of its friends to read your CSV file. An example of how to read a CSV file is in the CPAN documentation. Whilst you can simply split on commas as the previous example by johngg showed, it doesn't cater for quoted fields within CSV files. The most important thing to learn about Perl is not that There's More Than One Way To Do It (TMTOWTDI), it is that Someone Has Almost Certainly Done It Before, so use libraries where possible.

    A Monk aims to give answers to those who have none, and to learn from those who know more.

Log In?
Username:
Password:

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

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

    The best computer themed movie is:











    Results (299 votes), past polls