unlink command: <$files> argument fails

D

dn.perl

$mydir = "/usr/myname" ;
$files = "$mydir/*.cc" ;


a) unlink <$files> ; ===> fails
b) unlink <$mydir/*.cc> ==> works.

Why? What is the difference?

TIA.
 
P

Peter Scott

$mydir = "/usr/myname" ;
$files = "$mydir/*.cc" ;


a) unlink <$files> ; ===> fails
b) unlink <$mydir/*.cc> ==> works.

Why? What is the difference?

You should always put

use warnings;

as one of the first lines of your program.
 
G

Grant

$mydir = "/usr/myname" ;
$files = "$mydir/*.cc" ;


a) unlink <$files> ; ===> fails
b) unlink <$mydir/*.cc> ==> works.

Why? What is the difference?
~$ perldoc -f unlink
unlink LIST
unlink Deletes a list of files. Returns the number of files successfully deleted.

$cnt = unlink 'a', 'b', 'c';
unlink @goners;
unlink <*.bak>;

Note: "unlink" will not attempt to delete directories unless you are superuser
and the -U flag is supplied to Perl. Even if these conditions are met, be
warned that unlinking a directory can inflict damage on your filesystem.
Finally, using "unlink" on directories is not supported on many operating sys-
tems. Use "rmdir" instead.

If LIST is omitted, uses $_.

Why is it you ask somebody else to RTFM for you?

Grant.
 
P

Peter Makholm

~$ perldoc -f unlink [...]
Why is it you ask somebody else to RTFM for you?

Because finding the right FM to R is hard. What you quoted is of no
help. The right documentation to read is the 'I/O Operators' section
of 'perldoc perlop':

If what the angle brackets contain is a simple scalar variable (e.g.,
<$foo>), then that variable contains the name of the filehandle to
input from, or its typeglob, or a reference to the same. For example:

$fh = \*STDIN;
$line = <$fh>;

If what's within the angle brackets is neither a filehandle nor a
simple scalar variable containing a filehandle name, typeglob, or
typeglob reference, it is interpreted as a filename pattern to be
globbed, and either a list of filenames or the next filename in the
list is returned, depending on context. This distinction is determined
on syntactic grounds alone. That means "<$x>" is always a readline()
from an indirect handle, but "<$hash{key}>" is always a glob(). That's
because $x is a simple scalar variable, but $hash{key} is not--it's a
hash element. Even "<$x >" (note the extra space) is treated as
"glob("$x ")", not "readline($x)".

//Makholm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top