in reply to File and Word as Command Line arguments, Output # of Occurences

If you know how to do it with the file hardcoded, just change that line (probably something like my $file = 'myfile.txt';) to
my $file = $ARGV[0];

It's far more common to specify non-file arguments first, i.e. to call the script like

script.pl word file1 file2...

with possibly many files listed (cf. grep). Perl makes this approach easy with the diamond operator:

my $word = shift; while (<>) { # Reads the files line by line, # ... $ARGV contains the filename. }

You can use eof to reset the count if you want to output the count by file.

my declares a variable. It's unrelated to the task; it's possible to write the script without using it (which doesn't mean it's a good idea).

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: File and Word as Command Line arguments, Output # of Occurences
by Specerion (Initiate) on Oct 28, 2015 at 00:35 UTC
    Ahh brilliant, thanks for the tip on the common practice. So if I had a lot of arguments to pass in, shift moves from one to the next every time it is used?
      shift removes the first argument (or array element, if an array was specified) and returns it. The diamond operator iterates over the arguments as specified in I/O Operators.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ