in reply to A Tribute To The Monks Of Wisdom

You could use the fact that <> opens the files for you:

#!/usr/bin/perl use warnings; use strict; # This is a program which reads in a list of file names from the comma +nd # line and prints which files are readable, writable and/or executable +, # and whether each file exists. while ( <> ) { print "$ARGV is readable!\n" if -r ARGV; print "$ARGV is writable!\n" if -w ARGV; print "$ARGV is executable!\n" if -x ARGV; print "$ARGV exists!\n" if -e ARGV; close ARGV; } # end while

Replies are listed 'Best First'.
Re^2: A Tribute To The Monks Of Wisdom
by koolgirl (Hermit) on Nov 17, 2008 at 00:02 UTC
    Wow, the diamond operator opens the files? Is this only true for @ARGV, or can you also open other files without "open"? Is this something sort of like not declaring your variables, in that just because Perl allows it, doesn't mean it's good form, or is it something commonly done when using @ARGV?

      The diamond always works on the files in @ARGV. It also provides another level of magic, if @ARGV is empty, it reads STDIN. This is a very common idiom for writing filter programs.

      Another useful trick is to realize that @ARGV starts out with the command line parameters, but it is just a variable. You can change it. (For example, remove some flags or commands and then process the rest, or use the parameters to construct a list of filenames that replaces @ARGV before processing.

      The last trick is not one to be used lightly, because it's likely to confuse some people. But, sometimes it is exactly the tool you need.

      G. Wade

      I personally believe that the "only" risk associated with it is that you can pass arbitrary code to be executed, so it all depends on whom do you plan to make your script available to and to security settings. Thus

      perl -pe ''

      can be considered as an equivalent to cat(1) (well, except for the bazillion cmd line switches, of course!) but if you do

      perl -pe '' "rm -rfv $HOME |"

      then you're on your own. However FWIW it just boils down to doing

      rm -rfv $HOME

      in the first place. You "just" have to make sure others are not allowed to do the same or similar things, if you use the diamond operator: of course the issue has nothing to do with Perl anymore, but with your system's administration instead.

      --
      If you can't understand the incipit, then please check the IPB Campaign.