[comp.lang.perl] Getting started with Perl is a mystery to me

montnaro@spyder.crd.ge.com (Skip Montanaro) (08/16/90)

I grabbed the following little pipe off the net some time ago:

    find dir <selection criteria> -print | perl -n -e 'chop;unlink;'

I thought, sort of by extension, that the following would work:

    find dir <selection criteria> -print | perl -n -e 'chop;chmod 044;'

but the modes of the selected files aren't changed. Why? (In case you hadn't
guessed, I'm a Perl novice.) No errors are printed either, so what I've
typed is presumably legal Perl-ese.

--
Skip (montanaro@crdgw1.ge.com)

merlyn@iwarp.intel.com (Randal Schwartz) (08/17/90)

In article <MONTNARO.90Aug16122825@spyder.crd.ge.com>, montnaro@spyder (Skip Montanaro) writes:
| 
| I grabbed the following little pipe off the net some time ago:
| 
|     find dir <selection criteria> -print | perl -n -e 'chop;unlink;'
| 
| I thought, sort of by extension, that the following would work:
| 
|     find dir <selection criteria> -print | perl -n -e 'chop;chmod 044;'
| 
| but the modes of the selected files aren't changed. Why? (In case you hadn't
| guessed, I'm a Perl novice.) No errors are printed either, so what I've
| typed is presumably legal Perl-ese.

The first Perl command is equivalent to:

	perl -ne 'chop($_);unlink($_);'

because of the default parameters for both chop and unlink.  The
second is equivalent to:

	perl -ne 'chop($_);chmod(0644);'

[I presume the 044 was a typo.] which obviously successfully changes
*no* files to mode 0644.  Yuck. :-)

What you want is:

	perl -ne 'chop;chmod(0644,$_);'

or, if you like a little noise in your life:

	perl -ne 'print;chop;chmod(0644,$_);'

There, feel better now? :-)

# requires recent patches...
print pack("C*",grep($_^=1,unpack("C*","Ktru!`onuids!Qdsm!i`bjds-")))
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/