mbeason has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks.. I'm trying to do a simple file test on each file in an array. However, I never get any results back. If I put a print statement before the file test, I get the expected results. Once the file test is in place, nothing happens. Here is my code..
foreach (@test_node_list) { if (-e "/usr/adm/best1_default/collect/$_/noInstance") { # It prob +ably is a node with data if (-e "/usr/adm/best1_default/manager/$_") { # It has +a vis files directory @node_list = (@node_list,$_); if (-e "/usr/adm/best1_default/manager/$_/$vis_year$vis_mo +nth$vis_zero$vis_day*.vis") { system("/appl/perform/best1home/perl/clean_graphics.pl +", "$_"); print "It exists.\n"; } } } }
The part that dies the piece just after the @node_list array. Your guidance is always appreciated!

Replies are listed 'Best First'.
Re: Simple File Test
by Zaxo (Archbishop) on Jul 11, 2006 at 23:03 UTC

    Try placing,

    print "/usr/adm/best1_default/manager/$_/$vis_year$vis_month$vis_zero$ +vis_day*.vis\n";
    before the test. Is that what you expect it to be? I suspect it appears to die because you get no match, and there is nothing following to indicate life. '*' is an uncommon character to find in a file name, since it conflicts with shell glob. Do you want a glob there, instead of -e?

    After Compline,
    Zaxo

Re: Simple File Test
by kwaping (Priest) on Jul 11, 2006 at 23:05 UTC
    I recommend inserting the following line right after the line containing @node_list:
    print "/usr/adm/best1_default/manager/$_/$vis_year$vis_month$vis_zero$ +vis_day*.vis";
    Also, @node_list = (@node_list,$_); is better written as push @node_list, $_;.

    ---
    It's all fine and dandy until someone has to look at the code.
      I've made the change to the code.. Here it is:
      foreach (@test_node_list) { if (-e "/usr/adm/best1_default/collect/$_/noInstance") { # It prob +ably is a node with data if (-e "/usr/adm/best1_default/manager/$_") { # It has +a vis files directory push @node_list, $_; if (-e "/usr/adm/best1_default/manager/$_/$vis_year$vis_mo +nth$vis_zero$vis_day*.vis") { print; } } }
      This doesn't generate anything. I realize the filename looks odd but when I check them from the cmdline, it works as expected. I'm trying to say, "If this node has a file called X, then do something. Right now, I just have a print statement there. Eventually, I really want to put a system() instead.
        I realize the filename looks odd but when I check them from the cmdline, it works as expected.

        You need to expand on what you mean by "check them from the cmdline". If you're saying when you run ls /usr/adm/best1_default/manager/foo/20060711*.vis it shows files, then you've missed what was being said about needing to use glob. Your shell will expand the wildcard *, the -e operator (among other things) will not. If you want wildcard expansion you need to use glob, or do something like using opendir and then grep'ing the results from a readdir.

        You've misunderstood. Here's the actual code with our suggested edits:
        foreach (@test_node_list) { if (-e "/usr/adm/best1_default/collect/$_/noInstance") { # It prob +ably is a node with data if (-e "/usr/adm/best1_default/manager/$_") { # It has +a vis files directory push @node_list, $_; print "/usr/adm/best1_default/manager/$_/$vis_year$vis_mon +th$vis_zero$vis_day*.vis"; if (-e "/usr/adm/best1_default/manager/$_/$vis_year$vis_mo +nth$vis_zero$vis_day*.vis") { system("/appl/perform/best1home/perl/clean_graphics.pl +", "$_"); print "It exists.\n"; } } } }

        ---
        It's all fine and dandy until someone has to look at the code.