Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi fellow perlmonks,
I noticed a strange behaviour when using glob to expand filenames. I try to identify directories which contain nothing besides one special sort of file. I noticed that the return condition of my script failed to execute, even if glob returned something.

#!/usr/bin/perl -w use strict; use warnings; use File::Basename; sub remove_files_in_empty_directories($) { my ($directory) = @_; # do nothing if the directory is not empty # This is the problematic part return if glob("$directory/*"); # directory is empty, remove the .Ecurep-Lenovo* files foreach my $file ( glob("$directory/.EcuRep-Lenovo_*") ) { print $file,"\n"; } } my $list_file = shift @ARGV; die "list_file is missing - Usage: $0 list_file\n" unless $list_file; open(F,"<",$list_file) || die "can not open $list_file: $!\n"; while (my $line = <F> ) { chomp $line; my $dir = dirname $line; remove_files_in_empty_directories($dir); }
I pass this script a file containing a list of directories (filename is test):
/ecurep/hw/7/7042/21/EC/21EC3EC/.EcuRep-Lenovo_outofscope /ecurep/hw/7/7042/21/EC/21EC41C/.EcuRep-Lenovo_outofscope /ecurep/hw/7/7042/21/EC/21EC4DC/.EcuRep-Lenovo_outofscope

I call the script like this: perl test_perl test


This is kinda unexpected, as all three directories contain other directories. When I step through with the debugger, I can evaluate the glob with x, and it returns a list of directories every single time. If I evaluate x scalar glob("$directory/*") it also evaluates to the first entry from the list every time. But in case if the last pathname it steps further into the function anyway instead of executing my return statement. I cannot get my head wrapped around this - what is happening here?

Further testing of the problem:
If I delete the first line of the testfile, thereby removing the first directory from the list of directories to be one shorter, it works.

/ecurep/hw/7/7042/21/EC/21EC3EC/.EcuRep-Lenovo_outofscope /ecurep/hw/7/7042/21/EC/21EC41C/.EcuRep-Lenovo_outofscope
perl test_perl test2 No Output (as expected)

File Permissions to the directories:
drwxr-s--- 4 root swsupt 512 Apr 08 04:53 21EC3EC drwxr-s--- 4 root swsupt 512 Apr 08 05:24 21EC41C drwxr-s--- 5 root swsupt 512 Apr 11 12:12 21EC4DC [1:root@itcaix23:]/ecurep/tmp/perl_test # ls -l /ecurep/hw/7/7042/21/E +C/21EC41C total 0 -rw-r--r-- 1 root swsupt 25 Jun 24 2016 .EcuRep-Leno +vo_outofscope drwxr-s--- 2 root swsupt 512 Mar 31 09:06 2017-03-31 drwxr-s--- 2 root swsupt 512 Apr 07 09:05 2017-04-07 [1:root@itcaix23:]/ecurep/tmp/perl_test # ls -l /ecurep/hw/7/7042/21/E +C/21EC4DC total 0 -rw------- 1 root swsupt 29 Apr 11 12:12 .EcuRep-Leno +vo_outofscope drwxr-s--- 2 root swsupt 512 Mar 28 09:48 2017-03-28 drwxr-s--- 2 root swsupt 512 Apr 04 09:49 2017-04-04 drwxr-s--- 2 root swsupt 512 Apr 11 09:48 2017-04-11 [1:root@itcaix23:]/ecurep/tmp/perl_test # ls -l /ecurep/hw/7/7042/21/E +C/21EC3EC total 0 -rw-r--r-- 1 root swsupt 25 Nov 04 05:26 .EcuRep-Leno +vo_outofscope drwxr-s--- 2 root swsupt 512 Mar 31 06:26 2017-03-31 drwxr-s--- 2 root swsupt 512 Apr 07 06:26 2017-04-07
I am working with perl on AIX here. I have tested with perl 5.20 on AIX7.2 and perl 5.8.8 on AIX6.1. The behaviour is the same.
I tried the following variations on the code (just substituting the problematic part):
# makes no difference return if glob "'${directory}/*'"; # with this construct it works! my @files = glob("$directory/*"); return if scalar @files; # doesn't work either - prints "hi hi <directory-name> if ( glob("$directory/*") ) { print "hi\n"; return; }
I am not shure if I am misusing glob here of if there is a problem with my perl-version. Can someone help me understanding what is going on?

Kind regards, Yulivee

In reply to Strange behaviour when using glob in if condition by yulivee07

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2023-12-02 03:10 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (13 votes). Check out past polls.