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

Totally new to Perl. Why won't here1 and here2 showup on the output? It prints the file names in the directory fine.
use strict; use warnings; print "here1\n"; @files = <*>; foreach $file (@files){ print $file . "\n"; } print "here2\n";

Replies are listed 'Best First'.
Re: help with Print
by ikegami (Patriarch) on Jun 14, 2011 at 21:38 UTC

    Why won't here1 and here2 showup on the output?

    Because your program doesn't compile, it cannot be run, and thus it cannot output anything.

    $ perl -e' use strict; use warnings; print "here1\n"; @files = <*>; foreach $file (@files){ print $file . "\n"; } print "here2\n"; ' Global symbol "@files" requires explicit package name at -e line 5. BEGIN not safe after errors--compilation aborted at -e line 5. $ perl -e' use strict; use warnings; print "here1\n"; my @files = <*>; # <-- my foreach my $file (@files){ print $file . "\n"; } # <-- my print "here2\n"; ' here1 Build.PL Changes.txt inc lib LICENSE.txt MANIFEST MANIFEST.SKIP README.txt t here2
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: help with Print
by 7stud (Deacon) on Jun 14, 2011 at 22:42 UTC
    @files = <*>;

    ...and that is considered ancient perl syntax. Do this instead:

    use strict; use warnings; use 5.010; for my $file (glob '*') { say $file; }

      There's nothing ancient about using the diamond operator for globing. As in this case the wildcard processing is actually not needed, it'd probably be better to use opendinr&readdir as that doesn't put the whole list of files into memory at once, but that doesn't really matter in most cases.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

Re: help with Print
by Khen1950fx (Canon) on Jun 15, 2011 at 00:01 UTC
    I think that you're looking for something like this:
    #!/usr/bin/perl use strict; use warnings; use diagnostics; open(LOG, '>', '/root/Desktop/log.out') or die "Can't redirect stdout: + $!"; open(CMD, 'ls |'); open(STDERR, '>&', STDOUT) or die "Can't redirect stderr: $!"; open(STDERR, '>', 'LOG_FILE') or die "Can't redirect stderr: $!"; print "\nhere1\n"; while (<CMD>) { print_stuff ($_); } sub print_stuff { my ($line) = @_; print LOG $line; print $line; } print "\nhere2\n"; close LOG; close CMD; close STDERR; exit;