On Wed, Feb 06, 2002 at 03:05:29PM +0100, Jorge Goncalvez wrote: > I tried this: > @Symlink=("$program_path\\install.lnk", > "$program_path\\linux.ram.lnk", > "$program_path\\alize\\install.lnk", > "$program_path\\alize\\startup.txt.lnk"); > > foreach (@Symlink) > { > if (-e $_) > { > > system "rm $_"; > > } > } > > but it seems not to work?Why?
Looking at the '\\' in your filenames suggests that you're running Windows? The 'rm' program is a Unix program and although there are versions for Windows too - using Cygwin e.g. - you most probably just don't have 'rm'. Look here: ---------- snip ---------- kanku-dai:~$ perldoc -f unlink unlink LIST unlink Deletes a list of files. Returns the number of files successfully deleted. ... If LIST is omitted, uses $_. ---------- snip ---------- The perl builtin 'unlink' does what 'rm' does, but from within perl, so you don't need to rely on "external" software. Handily, it accepts lists, so given your above example you can remove your 'foreach' with unlink @Symlink; BUT WARNING: The checks are still missing. That's no real problem since 'unlink' simply ignores the missing files, but here's how you could do them anyways: a.) unlink grep { -e $_ } @Symlink; b.) -e $_ && unlink foreach (@Symlink); Check the documentation for grep (perldoc -f grep) to learn more about that. Basically grep gets a piece of code and a list. It runs the code for each element in the list and returns as result a new list that contains all elements that let the passed code return a 'true' value. You could do the same like this: my @newlist; foreach (@Symlink) { if (-e $_) { push @newlist, $_ } } unlink @newlist; b. looks a bit more cryptic but I think still explainable: We use 2 programming idioms here: shortcut evaluation and modifiers. First, lets talk about the modifiers: ---------- snip from 'perldoc perlsyn' ---------- Simple statements ... Any simple statement may optionally be followed by a SIN- GLE modifier, just before the terminating semicolon (or block ending). The possible modifiers are: if EXPR unless EXPR while EXPR until EXPR foreach EXPR ---------- snip ---------- What this short sentence implies is that you can add a clause to any simple expression. if (-e $my_file) { unlink $my_file; } is equivalent to unlink $my_file if -e $my_file; Same goes for the 'foreach' loop: foreach (@Symlink) { unlink } could also be written unlink foreach @Symlink; Above statement already does the job, but we wanted to include the test for existence of the file, which brings us to shortcuts: Shortcut evaluation is the simple concept of bailing out of the condition as soon as it cannot any longer be true. A '&&' test is only true if both sides of it are true. So if the left side of the '&&' fails there's no need to look at the right side, so the whole expression fails immediately. Since functions like 'unlink' return a value, we can use them as part of that '&&' statement, which brings us back to our example: -e $_ && unlink If the file doesn't exist, perl skips the left half of the '&&' so the 'unlink' is never called for non-existant files. Now we have a working expression that we can round up with our 'modifier' foreach -e $_ && unlink foreach (@Symlink) Voila. -- If we fail, we will lose the war. Michael Lamertz | [EMAIL PROTECTED] Nordstr. 49 | http://www.lamertz.net - http://www.perl-ronin.de 50733 Cologne | Priv: +49 221 445420 Germany | Mobile: +49 171 6900 310 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]