Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

file listing within skript not working

by McSvenster (Novice)
on Jan 30, 2013 at 10:32 UTC ( #1016018=perlquestion: print w/replies, xml ) Need Help??
McSvenster has asked for the wisdom of the Perl Monks concerning the following question:

dearest wise monks,

I have a script where I want to put a list of files into an array. I tried it as easy as:

my @dateiliste = `ls $datenDir/*.xml`;

This leaves the script running forever doing nothing. To make sure it is this line of code I added :

print LOGFILE "listing des verz. $datenDir\n"; my @dateiliste = `ls $datenDir/*.xml`; my $s = @dateiliste; print LOGFILE "$s Dateien im verz. $datenDir\n";

In my logfile I find everything until "listing des verz. /tmp". The script is running and running and pretyy sure will be running even after I become a monk.

So I tried directly in terminal:

perl -e '$dd = "/tmp";@a = `ls $dd/*.xml`;$s = @a;print "$s Dateien";'

Sure enough this works fine and gives the exspected answer.

So why is the backtick-ls working with perl -e but not within a script?

Thanks in advance,

P.S.: I tried perl -w. It gives no output.

Replies are listed 'Best First'.
Re: file listing within skript not working
by moritz (Cardinal) on Jan 30, 2013 at 10:39 UTC
    There's no need to shell out to ls to get a list of files, you can simply say
    my @files = glob "$datenDir/*.xml";

    (assuming that $datenDir doesn't contain whitespaces or other nasty stuff; but that would a problem with your approach too).

    So why is the backtick-ls working with perl -e but not within a script?

    I'm very sure it's not script vs. -e, but that there is some other difference. Is the directory the same in both cases? Is the script run as the same user, with the same security context (if you use selinux) as your one-liner?

      Thanks a lot for Your help! I'll ask our server-admins about selinux. I am pretty sure you are right. When I changed the script to

      my @files = glob "$datenDir/*.xml";
      it does not even write anything into the logfile. So I think the listings within scripts might be forbidden.

      And thanks for directing me to "glob". Don't know why I didn't use it - I think I need something to drink :-)

        Well said moritz ++

        McSvenster Perl has many ways to do things,you can also use readdir() function this way,

        use strict; use warnings; use Data::Dumper; opendir (DIR, "/home/vinoth/"); my @files = grep /\.xml$/, readdir DIR; print Dumper \@files;

        I am not telling this is the best way, but its also one of the way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1016018]
Approved by moritz
[Veltro]: feasible in the sense of 'making money'?
[marto]: if your app just calls a remote or third party service to do the task, you app does not 'do the task', it's just a messenger
[marto]: in the sense that for various values of "a while ago" it would not have been possible (and arguable still isn't) to have a fingerprint of every piece of commercially released music to compare against.
[marto]: a co-worker refers to the late 1980s as "a while ago"
[Veltro]: Like I said before, I ment the solution as a whole
[Veltro]: People thought it was not possible.
[marto]: and until recently it wasn't and arguable (for some samples) probably isn't
[Veltro]: How did we get here? Oh yeah, advertisement. So let's get back there. What I ment to say is that I think it is possible to create an algorithm that filters out advertisement
[Veltro]: And yes, maybe your decoder needs the cloud to do so.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2018-05-24 12:12 GMT
Find Nodes?
    Voting Booth?