[BNE-PM] The Perl one-liner

Derek Thomson derek at wedgetail.com
Wed Sep 18 23:14:06 CDT 2002


Mike Bissett wrote:

> 
>>If you wanted to do this for some reason, wouldn't it  
> 
> just be:  
>    
> 
>>$ ls | perl -e '$p = shift @ARGV; while (<>) {print if  
> 
> /$p/}' Foo  
>  
>  
> Unfortunately this doesnt work perl assumes the Foo to be 
> a file for interpreting, 

It works fine for me! Did you try my example exactly? Notice the use of 
"shift @ARGV". This doesn't just give you the first item in the array, 
but removes it as well!

So, if @ARGV initially contained ("Foo", "file1", "file2", "file3"), and 
you do "$p = shift @ARGV", $p is now "Foo", and @ARGV is ("file1", 
"file2", "file3").

Now, when you say "<>", reading will start from "file1", as that is the 
first item in @ARGV. The pattern "Foo" isn't even in the array, so it 
can't be used as a file name.

> i found the -s option but that 
> doesnt seem to work either.. 

No, that's for simple command line "switch" parsing. I actually didn't 
know about that one!

--
D.




More information about the Brisbane-pm mailing list