For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2007 > Write the output to a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Write the output to a file
John Jack

2007-12-11, 4:01 am

Hi Group

I'm new to perl and haven't used it before. I'm still practising and trying
my best to know it. Anyway, I wanted to print the list of files in a
directory with their full pathnames. This script below worked fine but I
want to write it to a file. Can someone help me, please? I know it must be
very simple but as I said I very new to programming and perl is my new
programming language to learn.

------------------------------------------------
#!/bin/perl
use File::Find <File::Find> ;
my $ByteCount=0;

open(OUTFILE > "c:\\perl\\output.txt") or die "Can't open output.txt: $!";

find(\&pits, "W:\\Users\\PACLAWMAT\\PACIFIC TREATIES\\");
print "Files are using $ByteCount bytes\n";
# Subroutine that determines whether we matched the file extensions.
sub pits {
if ((/\.doc$/)){
print OUTFILE "$File::Find::name\n";

#my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
$therest) = stat($_) or die "Unable to stat $_\n";
$ByteCount += $size;
}

close(OUTFILE);

}


Thanks

John


Patmarbidon

2007-12-11, 4:01 am

Patrick Marion has written:
I think that your 'close (OUTPUT)' is inside the sub 'pits'

you might put it before 'sub pits {' to get it at the logiccal end of
your program.


John Jack has written :
> Hi Group
>
> I'm new to perl and haven't used it before. I'm still practising and trying
> my best to know it. Anyway, I wanted to print the list of files in a
> directory with their full pathnames. This script below worked fine but I
> want to write it to a file. Can someone help me, please? I know it must be
> very simple but as I said I very new to programming and perl is my new
> programming language to learn.
>
> ------------------------------------------------
> #!/bin/perl
> use File::Find <File::Find> ;
> my $ByteCount=0;
>
> open(OUTFILE > "c:\\perl\\output.txt") or die "Can't open output.txt: $!";
>
> find(\&pits, "W:\\Users\\PACLAWMAT\\PACIFIC TREATIES\\");
> print "Files are using $ByteCount bytes\n";
> # Subroutine that determines whether we matched the file extensions.
> sub pits {
> if ((/\.doc$/)){
> print OUTFILE "$File::Find::name\n";
>
> #my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
> $therest) = stat($_) or die "Unable to stat $_\n";
> $ByteCount += $size;
> }
>
> close(OUTFILE);
>
> }
>
>
> Thanks
>
> John
>
>


John W . Krahn

2007-12-11, 8:00 am

On Monday 10 December 2007 21:50, John Jack wrote:
>
> Hi Group


Hello,

> I'm new to perl and haven't used it before. I'm still practising and
> trying my best to know it. Anyway, I wanted to print the list of
> files in a directory with their full pathnames. This script below
> worked fine but I want to write it to a file. Can someone help me,
> please? I know it must be very simple but as I said I very new to
> programming and perl is my new programming language to learn.



#!/bin/perl
use warnings;
use strict;

use File::Find;

my $ByteCount = 0;

open OUTFILE, '>', 'c:/perl/output.txt'
or die "Can't open output.txt: $!";

find( \&pits, 'W:/Users/PACLAWMAT/PACIFIC TREATIES' );

print "Files are using $ByteCount bytes\n";

# Subroutine that determines whether we matched the file extensions.
sub pits {
if ( /\.doc$/ ) {
print OUTFILE "$File::Find::name\n";
$ByteCount += -s;
}
}

close OUTFILE;

__END__



John
--
use Perl;
program
fulfillment
Chas. Owens

2007-12-11, 7:01 pm

On Dec 11, 2007 2:10 AM, patmarbidon <patmarbidon@free.fr> wrote:
> Patrick Marion has written:
> I think that your 'close (OUTPUT)' is inside the sub 'pits'
>
> you might put it before 'sub pits {' to get it at the logiccal end of
> your program.

snip

Or remove the close completely. Perl closes all open file handles at
the end of the program, so you should only call close if a file handle
needs to be closed before then (you should close file handles once you
stop using them because they are a limited resource).
Dr.Ruud

2007-12-12, 8:00 am

"Chas. Owens" schreef:

> (you should close file handles once you
> stop using them because they are a limited resource).


I often start a new block at opening a file.

--
Affijn, Ruud

"Gewoon is een tijger."
Chas. Owens

2007-12-12, 8:00 am

On Dec 12, 2007 4:46 AM, Dr.Ruud <rvtol+news@isolution.nl> wrote:
> "Chas. Owens" schreef:
>
>
> I often start a new block at opening a file.

snip

If you are using the new scalar based file handles this is a good
practice since they automagically close when the last reference to
them goes out of scope.

{
open my $fh, "<", $file
or die "could not open $file: $!";
#do things with file
} #unless a reference was made to $fh it will close here

But if you are still using (or working on code that still uses) the
old bareword file handles, it will not close the file. This is but
one of the many advantages the new style file handles has over the old
style.
Dr.Ruud

2007-12-12, 8:00 am

"Chas. Owens" schreef:
> Dr.Ruud:
[color=darkred]
>
> If you are using the new scalar based file handles this is a good
> practice since they automagically close when the last reference to
> them goes out of scope.
>
> {
> open my $fh, "<", $file
> or die "could not open $file: $!";
> #do things with file
> } #unless a reference was made to $fh it will close here
>
> But if you are still using (or working on code that still uses) the
> old bareword file handles, it will not close the file. This is but
> one of the many advantages the new style file handles has over the old
> style.


Yes, thanks for the clarification. I detest code with bareword file
handles.

Your last comment line ("it will close here") I would put inside the
block, or I would say "is closed with the block".
(I am all for lexical comments.)

--
Affijn, Ruud

"Gewoon is een tijger."

Chas. Owens

2007-12-12, 8:00 am

On Dec 12, 2007 7:08 AM, Dr.Ruud <rvtol+news@isolution.nl> wrote:
> "Chas. Owens" schreef:

snip
snip[color=darkred]
> Your last comment line ("it will close here") I would put inside the
> block, or I would say "is closed with the block".
> (I am all for lexical comments.)

snip

I can see that. And in Perl 6 it would actually make a difference
since comments are included in the parse tree.
Uri Guttman

2007-12-12, 7:02 pm

>>>>> "R" == Ruud <rvtol> writes:

R> "Chas. Owens" schreef:[color=darkred]

R> Yes, thanks for the clarification. I detest code with bareword file
R> handles.

if you do have to support older perls (i do in file::slurp), use a ref
to a localized typeglob or Symbol::gensym. those will also close
automatically upon their being destroyed at the exit of their scope. the ability
to autoclose handles has been in perl forever. only the initializing of
lexical handles in the open call is newish.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Dermot

2007-12-13, 8:00 am

On 12/12/2007, Chas. Owens <chas.owens@gmail.com> wrote:
>
> On Dec 12, 2007 7:08 AM, Dr.Ruud <rvtol+news@isolution.nl> wrote:
>
> snip
>
> I can see that. And in Perl 6 it would actually make a difference
> since comments are included in the parse tree.



A small amount of clarification please.
Is it generally preferable to use scalars for filehandle; $fh is preferred
to FH?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com