[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] droplet



Simon,

(Note to Chris & Vicki, Perhaps you could include the following
explanation in the 2ed of MPaE so folks learn what's going on with
droplets.)

As others have mentioned, the filename (or if you select more than one
file and drop the group on at the same time) each show up as one
element in @ARGV -- so the number of filenames dropped is @#ARGV + 1,
The first file selected in the group is $ARGV[0],
The second file selected in the group is $ARGV[1], ...
(Just like you typed 'em in on the command line in UNIX, so you can
 do whatever any Perl book says you can do with ARGV.)

An interesting tip:  If you create empty files with funny names like
"-foobar", and select these files in the order you want the options
to show up, you can also have "command line flags" ala UNIX
(Chris' idea, I think).  Then 'shift' or 'splice' out the flag arguments
or use command line processing libraries.

The special Perl construction:

        #!perl -w
        while (<>) {
                print $_;
        }

Opens each file in @ARGV, in turn and puts one line at a time into
the special variable $_ for the programmer to munch on.
If ARGV is empty, the "while(<>){}" will read in lines typed into
the STDIN (MacPerl) window until a control-D is typed at the beginning
of a line. The above, saved as a droplet,
 behaves like the unix 'cat' command.

        #!perl -w
        while (<>) {
                print $ARGV, ':', $_
        }

Would behave like the unix 'grep' command with the pattern '' (i.e.
match all lines), print out the filename, a colon, then the contents
of each line, because the special variable $ARGV is set to the
current filename when processing lines from while (<>){}.

This UNIX-ish kind of behavior isn't explained in _MacPerl Power and Ease_.

The following droplet illustrates handling the MacPerl-input window
case gracefully, doing something with each filename, then outputting
the contents of all the files with continuous line numbering.

#!perl -w

#
# If no files dropped, notify that the user should type stuff in.
#
print( STDERR  <<__EndNoFilesMessage__ ) if $#ARGV < 0;

No files dropped onto <$0>.
Please type your input into this window...
Terminate your input with a control-D at the beginning of a line.

__EndNoFilesMessage__

#
#
# Print out each filename.
#
my ( $nn, $i) = ('',0);
foreach $nn ( @ARGV ) {
        printf "file \$ARGV[%d] = %s\n", $i++, $nn
}

#
# Print out the contents of all files with continuous line numbers.
#
while (<>) {
        printf " %5d %s", $., $_
}
__END__
-- Larry

At 2:35 PM 10/1/99, Simon Martel wrote:
>Hi!
>
>I am new to programming and english isn't my native langage. I learn
>programming with MacPerl and some JavaScript. I want to make a droplet
>to clean some html files. My question is : What is the way to tell the
>droplet it should read the file i drop on it. I already have the book
>"MacPerl, Power and Ease".
>
>Thanks ! :-).
>
># ===== Want to unsubscribe from this list?
># ===== Send mail with body "unsubscribe" to macperl-request@macperl.org

--
Regards, Larry F. Allen-Tonar        (larryat@cts.com) +1 760/746-6464 (voice)
         Principal Designer                              +1 760/746-0766 (FAX,
         P.O. Box 463072                                         upon request)
         Escondido, CA  92046-3072
"Futuaris nisi irrisus ridebis.", Carlton in _The Road to Mars_ by Eric Idle



# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org