Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to read in a file passed to my script on the command line iwth a <, example:

perlscript.pl < input_file

Is there a way to do this without loading any new modules onto a system?

thanks Mike Bond mbond@eudoramail.com

Replies are listed 'Best First'.
(tye)Re: passing files with
by tye (Sage) on Nov 14, 2000 at 05:42 UTC

    It is hard to say what problem you are having. I'll make a guess that you are doing:

    script.pl <infile.txt
    on a Windows platform and the script doesn't appear to read the contents of the "infile.txt" file. If this is indeed your problem, then you have run into a bug in how Microsoft does "file associations", that is, how Microsoft knows to run Perl when you ask it to run a file whose name ends in ".pl".

    A quick solution is to instead use:

    perl script.pl <infile.txt

    You can type the command:

    perldoc pl2bat
    to view the documentation for the pl2bat script. It includes more information about this problem and ways to solve it along with the trade-offs of the different solutions (unless you have an old version of Perl that doesn't include my patches for this).

            - tye (but my friends call me "Tye")
Re: passing files with
by Fastolfe (Vicar) on Nov 14, 2000 at 04:58 UTC
    This type of redirection sends data to a program's "standard input", which can be referenced with the Perl STDIN filehandle:
    while (<STDIN>) { print "Got a line: $_"; }
    Just be aware that each line you read will have its trailing newline. Use chomp to get rid of it.
Re: passing files with
by kilinrax (Deacon) on Nov 14, 2000 at 04:33 UTC
    Do you mean something like this?
    #!/usr/bin/perl -w use strict; my $file = $ARGV[0]; open FILE, $file; my $data = <FILE>: close FILE;
RE: passing files with
by Anonymous Monk on Nov 14, 2000 at 18:32 UTC
    Yes. Short answer: try: $ perldoc perlopentut then search (using ' / ' ) for '<>' (skip the quotes) longer answer: while (<>) { print "current file is: $_ \n"; # do stuff with file }
RE: passing files with
by Anonymous Monk on Nov 14, 2000 at 18:32 UTC
    Yes. Short answer: try: $ perldoc perlopentut then search (using ' / ' ) for '<>' (skip the quotes) longer answer: while (<>) { print "current file is: $_ \n"; # do stuff with file }