What is the difference between if( -f $f ) and unless( -d $f )?

Discussion in 'Perl Misc' started by hsk, Oct 26, 2004.

  1. hsk

    hsk Guest

    #!/usr/bin/perl -w

    use File::Find;

    my ( @dirs, $sum, $cnt, $dirname, $base_dir );

    $base_dir = "D:\\temp";

    find(\&wanted, $base_dir);

    sub wanted {

    $name = $File::Find::name;


    if( -d $name )
    {
    push( @dirs, $name );
    }
    }


    foreach my $d ( @dirs )
    {

    opendir( DIR, $d );

    # set back to 0 size
    $sum = 0;


    foreach my $f ( readdir( DIR ) )
    {
    # set back to 0 size
    $size = 0;

    # if( -f $f )
    unless( -d $f )
    {
    $size = (stat($f))[7];
    $sum = $sum + $size;
    }
    }
    close( DIR );

    }

    There is no error in case of 'if( -f $f ), but gets some errors for
    'unless( -d $f )'. The error comes up in the line of '$sum = $sum + $size'
    , saying 'Use of Uninitialized value.'
    Is there any of who might give an answer for this?

    OS: window 2000, ActivePerl 5.8 version
     
    hsk, Oct 26, 2004
    #1
    1. Advertising

  2. Re: What is the difference between if( -f $f ) and unless( -d $f)?

    hsk wrote:

    [ Subject:
    What is the difference between if( -f $f ) and unless( -d $f )? ]

    You should get some hints if you check out "perldoc -f -X".

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 26, 2004
    #2
    1. Advertising

  3. hsk

    Tony Curtis Guest

    >> On 25 Oct 2004 19:07:52 -0700,
    >> (hsk) said:


    > # if( -f $f ) unless( -d $f ) { $size = (stat($f))[7]; $sum
    > = $sum + $size;


    > There is no error in case of 'if( -f $f ), but gets some
    > errors for 'unless( -d $f )'. The error comes up in the line
    > of '$sum = $sum + $size' , saying 'Use of Uninitialized
    > value.' Is there any of who might give an answer for this?


    Well, firstly -f and -d don't cover the entire file type
    space, so the 2 tests you have don't trap all possible cases.

    Secondly, you don't check whether the stat() succeeded, which
    is more likely to be the root cause of your woes.

    You can also write

    $sum += $size;

    to be more succinct.

    hth
    t
     
    Tony Curtis, Oct 26, 2004
    #3
  4. (hsk) wrote in
    news::

    > What is the difference between if( -f $f ) and unless( -d $f )?


    Please do not only put your question in the subject line.

    see perldoc -f -x

    Sinan
     
    A. Sinan Unur, Oct 26, 2004
    #4
  5. hsk wrote:

    Subject: What is the difference between if( -f $f ) and unless( -d $f )?

    Uhh, I would say sockets, symbolic links, named pipes, devices, just to name
    a few more common ones.

    jue
     
    Jürgen Exner, Oct 26, 2004
    #5
    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. jakk
    Replies:
    4
    Views:
    12,511
  2. Santa
    Replies:
    1
    Views:
    1,140
    Mark A. Odell
    Jul 17, 2003
  3. Gábor SEBESTYÉN

    Unless unless

    Gábor SEBESTYÉN, Jun 17, 2005, in forum: Ruby
    Replies:
    3
    Views:
    171
    Gábor SEBESTYÉN
    Jun 17, 2005
  4. PerlFAQ Server
    Replies:
    0
    Views:
    384
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    293
    PerlFAQ Server
    Apr 15, 2011
Loading...

Share This Page