Printing folder contents

G

Guest

I'.m trying to make a simple program which would print the contents of c:\
folder.
(files and first sub-folders). Am i at the right track? (this doesn't work)

#!/usr/bin/perl
use strict;
print "Content-Type: text/plain\n\n";
my @folder = <c:\*>;
foreach my $file (@folder)
{
print "$file\n";
}
# end
 
G

Guest

I'd also like to print out the
enviromental variables. I understand that, keys %ENV i somehow related but
how to use it?
 
B

Ben Morrow

I'.m trying to make a simple program which would print the contents of c:\
folder.
(files and first sub-folders). Am i at the right track? (this doesn't work)

#!/usr/bin/perl
use strict;

Good; you want to add
use warnings;
to that.
print "Content-Type: text/plain\n\n";

Why? Perl != CGI.
my @folder = <c:\*>;

When using Perl, always use / rather than \ for paths.
foreach my $file (@folder)
{
print "$file\n";
}
# end

The usual Perl way to indicate end-of-program is
__END__
because perl understands that as well.

Ben
 
G

Guest

And another problem solved too, thanks a lot!


You should start a new thread when starting a new topic.




The keys in %ENV are the names of the environment variables, while the
values of %ENV are the values of the environment variables corresponding
to those keys. Read more by doing


perldoc perlvar


So to print all environment variables and their values you could do
something like this:


print "$_ => $ENV{$_}\n" for keys %ENV;
 
N

Nataku

The %ENV hash contains all of the environmental variables that Perl
knows about. The key of this hash is the name of the variable ( like
HOME ) and the value is, naturally, the value contained within that
variable.

So, to get all the env variables, you can access this hash in any one
of the standard ways.

foreach( my $key ( keys %ENV ) ){
print ( "$key -> ".$ENV{$key}."\n" );
}

or

while( my ($key, $value) = each %ENV ){
print ( "$key -> $value\n" );
}

The second is preferred as the first will dump all of the keys into a
list. Normally the ENV hash isnt that big, but for large hashes this
could consume a significant portion of memory - while the second
simply goes through one at a time.

I highly reccomend reading through the perldoc, it contains answers to
many a question like this.
 
R

Roy Johnson

I'.m trying to make a simple program which would print the contents of c:\
folder.
(files and first sub-folders). Am i at the right track? (this doesn't work)

#!/usr/bin/perl
use strict;
print "Content-Type: text/plain\n\n";
my @folder = <c:\*>;
foreach my $file (@folder)
{
print "$file\n";
}
# end

How does it fail? What it should do is load up @folder with a listing
of the files in c:\, and then print the listing.
 
T

Tad McClellan

Roy Johnson said:
<mh> wrote in message news:<[email protected]>...
How does it fail? What it should do is load up @folder with a listing
of the files in c:\,


<glob> is "double quotish", so he has the equivalent of

my @folder = glob "c:\*";

which is the same as:

my @folder = glob 'c:*'; # _no_ slashes

when he wants either:

my @folder = glob "c:\\*";

or, better:

my @folder = glob "c:/*";

or, best:

my @folder = glob 'c:/*';
 
M

Master Web Surfer

et> said:
I'.m trying to make a simple program which would print the contents of c:\
folder.
(files and first sub-folders). Am i at the right track? (this doesn't work)

#!/usr/bin/perl
use strict;
print "Content-Type: text/plain\n\n";
my @folder = <c:\*>;
foreach my $file (@folder)
{
print "$file\n";
}
# end


Your syntax looks good (according to "The Camel"). Please be more
specific; what do you mean by "this doesn't work" ??

If you provide more details then perhaps someone can help you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top