Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: File read and strip

by Andrew_Levenson (Hermit)
on Nov 14, 2006 at 19:54 UTC ( [id://584045]=note: print w/replies, xml ) Need Help??


in reply to Re: File read and strip
in thread File read and strip

I'm sorry, but... what?
The files are user-input, so the $file=<> has worked fine for me in the past.

Sorry, but I have no clue what else you said besides that. :(
What does shift @ARGV do? What is it calling from? what does the $! in the die function do?

Sorry if these are stupid questions, and thanks.

Replies are listed 'Best First'.
Re^3: File read and strip
by Joost (Canon) on Nov 14, 2006 at 20:03 UTC
    shift @ARGV removes the first element from @ARGV and returns it. @ARGV contains all the arguments provided to your program.

    I was asssuming you wanted to call your program like this:

    my_program inputfile outputfile
    anyway,

    my $whatever = <>;
    will read a single line from the file(s) as specified in @ARGV. in other words, $whatever will not be the filename but the next (or first) line of the file you passed as an argument. if you don't pass any arguments <> will read from STDIN, which means it will usually wait for you to enter a line. See perlop (quoted below:) Note that lines read usually end in a newline character ("\n") while filenames usually do not.
    while (<>) { ... # code for each line } is equivalent to the following Perl-like pseudo code: unshift(@ARGV, ’-’) unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }

    $! will contain the last filesystem error, which will make it a lot easier to see what's going wrong if you pass the wrong filename to open(), or don't have permissions etc. See also perlvar.

Re^3: File read and strip
by runrig (Abbot) on Nov 14, 2006 at 20:12 UTC
    The files are user-input, so the $file=<> has worked fine for me in the past.
    I think he was assuming that the file name was coming from the command line. @ARGV is the array of command line arguments, so "shift @ARGV" removes and returns the first argument. If you want the user to enter a file name, then it would be a good idea to prompt for it, e.g.:
    print "Enter filename: "; chomp(my $file = <STDIN>);
    what does the $! in the die function do?
    See perlvar. It is the error message for system/library calls. You want to check to see if open fails, and if it does, see why it failed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://584045]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-18 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found