_ 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. Advertisements

  2. Στις 20/3/2014 23:12, ο/η Jürgen Exner έγÏαψε:
    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
    1. Advertisements

  3. 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
  4. George Mpouras

    gamo Guest

    El 20/03/14 21:43, George Mpouras escribió:

    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'
    gamo, Mar 21, 2014
  5. 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
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.