in reply to How do I call a DOS command with a /C variable in PERL ?
below is some code that shows you the different ways of accomplishing your task.
the output is:#!perl -w use strict; my $file = 'c:\autoexec.bat'; my $pattern = 'PATH'; my $seperator = '----------'; print "\n---find 1: using system\n"; my $t=system("find /c \"$pattern\" $file") ; print "->", $t, "<-\n"; print "\$t is the exit code\n"; print "\n---find 2: using `backticks`\n"; my $u=`find /c "$pattern" $file`; print "->", $u, "<-\n"; print "\$u is the output\n"; print "\n---find 3: 100% pure perl\n"; open(FILE, "< $file") or die("ERROR: failed to open file, $!"); # $count is initialized to prevent print from complaining # if no matches are found my $count=0; while( <FILE> ) { ++$count if /$pattern/; } close(FILE) or die("ERROR: failed to close file, $!"); print "->", $count, "<-\n"; print "\$count is the match count\n"; print " -or, if you really want the same output format as find-\n"; print "$seperator $file: $count\n";
on my windows98 box.C:\WINDOWS\Desktop>perl test.pl ---find 1: using system ---------- c:\autoexec.bat: 6 ->0<- $t is the exit code ---find 2: using `backticks` -> ---------- c:\autoexec.bat: 6 <- $u is the output ---find 3: 100% pure perl ->6<- $count is the match count -or, if you really want the same output format as find- ---------- c:\autoexec.bat: 6
~Particle
|
|---|