in reply to backtick iterpolation issue

here's the winner:
$playListRow = shell_quote($playListRow); $playListRow =~ s/\r//g; my $result = `ls $playListRow `; print "$result\n";
Filtered the /r out, and had to switch to shell_quote from quoteMeta as quoteMeta wasn't working quite right. I was using backticks as I needed to get the stderr and stdout. At a high level what I'm trying to do: parse a directory of iTunes playlists(m3u files) determine whether all the files in the playlist exist on my file server. If they do great, if they don't, output a playlist file, that lists which tracks are missing, what they are and where they go in the track order. Once i have that complied, I can import my playlists one by one and know whether I have the whole playlist, or if it's missing tracks. If it's missing tracks I can fix it. The issue i've been fighting with is if you import a playlist to itunes and are missing some of the files, itunes just dumps them. No error, nor any log that I've found. Thanks for all the help!

Replies are listed 'Best First'.
Re^2: backtick iterpolation issue
by Laurent_R (Canon) on Jul 01, 2015 at 17:30 UTC
    Fine if it works and provides what you want. But I would still reiterate the advice provided earlier by other monks: there is really no reason to shell out and run an ls shell command when Perl provides you with powerful builtins to do just that, such a glob (my favorite choice in this specific case) or opendir/readdir.

    Update: added links to the documentation of the suggested functions.

Re^2: backtick iterpolation issue
by 1nickt (Canon) on Jul 01, 2015 at 17:52 UTC

    I was using backticks as I needed to get the stderr and stdout.

    But using backticks as you did doesn't capture the stderr of your system executable!

    #!/usr/bin/env perl -w use strict; my $foo = qx{ ls }; # alternate syntax for backticks open my $fh, '>', 'bar.out' or die $!; print $fh $foo; close $fh; __END__
    $ cat bar.out foo.pl $
    #!/usr/bin/env perl -w use strict; my $foo = qx{ ls baz }; # doesn't exist open my $fh, '>', 'bar.out' or die $!; print $fh $foo; close $fh; __END__
    $ cat bar.out $

    You may have seen an error message spit out to your screen by your system executable, but it was not captured in your Perl program because you used backticks. You can combine the output streams, depending on your OS, with something like:

    #!/usr/bin/env perl -w use strict; my $foo = qx{ ls baz 2>&1 }; open my $fh, '>', 'bar.out' or die $!; print $fh $foo; close $fh; __END__
    $ cat bar.out ls: baz: No such file or directory $

    But then you'll have to parse what you get to see if matches an error string. This all begs the question of why you didn't follow advice from others as well as myself to use a module that is built for the task ... because, among other benefits they usually handle errors gracefully.

    Remember: Ne dederis in spiritu molere illegitimi!