Hi Monks!

I've been working through a Perl intro book as I want to understand Perl better and use it at work. I have developed this simple test file below that uses regular expressions and tags to pull info from a file. Right now, I have to hard code the file name in the Perl script. I'd like to be able to type the file path at the command prompt to point this script at various files in different locations. I thought that I could perhaps use @ARGV to help do this, but perhaps not. Here's what I've done so far:
use strict; @ARGV == 1 or die "Specify a file path\n"; open(FILE, $ARGV) or die "Cannot open file: $!\n"; while (<>) { /(\S+)\s+\S+\s+(\S+)\s+(.*)/; #testing regex and tags print "$1, $2, $3\n"; #testing printing } close FILE; print "Done!\n";
My thinking is that I could use @ARGV to do something like this from the command prompt:
myperlscript.pl open, "C:/path/to/file/on_windows_pc/file.txt"

Right now, my program just reaches the @ARGV == 1 line and prints out the die statement. I've tried variatons of @ARGV like @ARGV == 0 and different command line statements too. I must be using @ARGV incorrectly and would appreciate it if anyone could point out the proper way to dynamically point Perl to different files without coding it in. Thank you!!

In reply to Using @ARGV by Tech77

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.