unlink command: <$files> argument fails

Discussion in 'Perl Misc' started by dn.perl@gmail.com, Oct 31, 2008.

  1. Guest

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


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

    Why? What is the difference?

    TIA.
     
    , Oct 31, 2008
    #1
    1. Advertising

  2. Peter Scott Guest

    On Fri, 31 Oct 2008 01:41:44 -0700, wrote:
    > $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.

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
     
    Peter Scott, Oct 31, 2008
    #2
    1. Advertising

  3. Grant Guest

    On Fri, 31 Oct 2008 01:41:44 -0700 (PDT), "" <> wrote:

    >
    >$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.
    --
    http://bugsplatter.id.au
     
    Grant, Oct 31, 2008
    #3
  4. Grant <> writes:

    >>a) unlink <$files> ; ===> fails
    >>b) unlink <$mydir/*.cc> ==> works.
    >>
    >>Why? What is the difference?


    > ~$ 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
     
    Peter Makholm, Oct 31, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Urbanus
    Replies:
    0
    Views:
    2,264
    Paul Urbanus
    Apr 7, 2006
  2. rbt
    Replies:
    4
    Views:
    2,220
  3. kenny roytman
    Replies:
    0
    Views:
    145
    kenny roytman
    Oct 30, 2007
  4. Ruben van Engelenburg

    unlink and big files

    Ruben van Engelenburg, Nov 8, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    155
    Ruben van Engelenburg
    Nov 9, 2003
  5. Prasanth

    Unlink command for opened files

    Prasanth, Apr 16, 2008, in forum: Perl Misc
    Replies:
    8
    Views:
    182
    Ben Morrow
    Apr 16, 2008
Loading...

Share This Page