_ check

Discussion in 'Perl Misc' started by George Mpouras, Mar 20, 2014.

  1. first I check if a file exists ( -f $file ) and then doing several tests
    using the _ for being fast.

    I have serious problem because the _ checks e.g -z _ sometimes is giving
    wrong results. the code is multi thread . What is going wrong ?
     
    George Mpouras, Mar 20, 2014
    #1
    1. Advertising

  2. Στις 20/3/2014 23:12, ο/η Jürgen Exner έγÏαψε:
    > George Mpouras <> wrote:
    >> first I check if a file exists ( -f $file ) and then doing several tests
    >> using the _ for being fast.
    >>
    >> I have serious problem because the _ checks e.g -z _ sometimes is giving
    >> wrong results. the code is multi thread . What is going wrong ?

    > ^^^^^^^^^^^^
    >
    > Are you sure you are not running into race conditions?
    >
    > jue
    >


    do not know, I am bunging my head with this. It is an apache mod perl.
    Maybe it have to do with apache cache.
    I do not know if _ is saying always the truth. I mean if between the -f
    and _ an other thread ask for -f what will happen
     
    George Mpouras, Mar 20, 2014
    #2
    1. Advertising

  3. George Mpouras <> writes:
    > Στις 20/3/2014 23:12, ο/η Jürgen Exner έγÏαψε:
    >> George Mpouras <> wrote:
    >>> first I check if a file exists ( -f $file ) and then doing several tests
    >>> using the _ for being fast.
    >>>
    >>> I have serious problem because the _ checks e.g -z _ sometimes is giving
    >>> wrong results. the code is multi thread . What is going wrong ?

    >> ^^^^^^^^^^^^
    >>
    >> Are you sure you are not running into race conditions?
    >>
    >> jue
    >>

    >
    > do not know, I am bunging my head with this. It is an apache mod
    > perl. Maybe it have to do with apache cache.
    > I do not know if _ is saying always the truth. I mean if between the
    > -f
    > and _ an other thread ask for -f what will happen


    As a somewhat simplified statement: Perl doesn't really support
    multi-threading, all it can do is have multiple perl processes :):=
    interpreters) run in a shared address space (thus, elegantly combining
    all disadvantages of either model with each other in order to provide
    none of the advantages). For your case, this means that each thread has
    its own 'stat buffer'.

    What do you mean by "-z _ sometimes is giving wrong results"? All uses
    of stat are prone to TOCTOU-races, meaning, -z file being true by the
    time the test was made doesn't even imply that it's still true by the
    time your code sees the result.
     
    Rainer Weikusat, Mar 20, 2014
    #3
  4. George Mpouras

    gamo Guest

    El 20/03/14 21:43, George Mpouras escribió:
    > first I check if a file exists ( -f $file ) and then doing several tests
    > using the _ for being fast.
    >
    > I have serious problem because the _ checks e.g -z _ sometimes is giving
    > wrong results. the code is multi thread . What is going wrong ?



    The special filehandle '_' is different from using a normal variable.
    So, if something is not working try a normal variable.

    If that don't works, try something like this

    $can_perhaps_read = -r "file"; # use the mode bits
    {
    use filetest 'access'; # intuit harder
    $can_really_read = -r "file";
    }
    $can_perhaps_read = -r "file"; # use the mode bits again

    Which is copyed from 'man filetest'

    --
    http://www.telecable.es/personales/gamo/
     
    gamo, Mar 21, 2014
    #4
  5. gamo <> writes:
    > El 20/03/14 21:43, George Mpouras escribió:
    >> first I check if a file exists ( -f $file ) and then doing several tests
    >> using the _ for being fast.
    >>
    >> I have serious problem because the _ checks e.g -z _ sometimes is giving
    >> wrong results. the code is multi thread . What is going wrong ?

    >
    >
    > The special filehandle '_' is different from using a normal variable.
    > So, if something is not working try a normal variable.


    This depends on your definition of 'normal variable': perl keeps the
    result of the last stat call in a 'normal variable' named PL_statbuf
    (actually, this is a macro expanding to the 'normal variable' but that's
    not really relevant here) and doing a filetest on _ examines the struct
    stat available as/ via PL_statbuf.

    But this doesn't really matter: The state of 'filesystem objects' can
    generally change at any time and this means the result of each stat call
    is potentially outdated as soon as it becomes available. And the same is
    true about checks done via access(2), as filetest does: No matter what
    the call returned, it is potentially not correct anymore by the time the
    result is available.
     
    Rainer Weikusat, Mar 21, 2014
    #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. Davisro
    Replies:
    1
    Views:
    701
    Michael D. Ober
    Jun 14, 2004
  2. mit
    Replies:
    1
    Views:
    836
    Ramu Pulipati
    Jan 25, 2006
  3. kris
    Replies:
    0
    Views:
    535
  4. kris
    Replies:
    0
    Views:
    496
  5. kris
    Replies:
    1
    Views:
    5,304
Loading...

Share This Page