If you get it from STDIN, you probably need to chomp it to get rid of the
trailing newline stuff. as in
print "prompt>";
chomp ( $dirname = <STDIN> );
And, per another message, you cannot append to the front of a file, there is
already data at the front of the file, and if you were able to write there (some
modes permit that, but not append mode), you would wipe out the data that was
already there.
So what you want to do in your loop is: open the file for reading, read the file
into a buffer, close the file. Open the file for writing, write the copyright
yada yada, write the buffer, close the file.
Tanya Graham wrote:
> ok, for some reason if i hardcode it, it is fine. it won't work if i try and
> get it from STDIN. anyway, here is my program:
> #!/usr/bin/perl
>
> $dirname = 'C:/PerlExp';
> opendir (DIR, $dirname) or die "can't open directory
> $dirname: $!";
>
> while ( defined ($file = readdir (DIR))){
> open (SOURCE, ">>$file") or die "can't open file $file for
> appending";
> seek (SOURCE, 0, 0) or die "can't point to beginning";
> print SOURCE "copyright info yada yada yada" ;
> close (SOURCE);
> }
> closedir(DIR);
>
> i get the error "can't open file . for appending at C:|append.pl line 7"
> is it trying to open something called "."? the files in that folder are all
> like "tester.txt" "tester2.txt", etc....
> any ideas?
> tanya
>
> -----Original Message-----
> From: Ron [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, May 29, 2001 4:12 PM
> To: perl win32 users
> Subject: Re: directory
>
> >From perlport document:
>
> =====================================================
> "System calls accept either / or \ as the path separator.
>
> However, many command?line utilities of DOS vintage treat / as the option
> prefix,
> so they may get confused by filenames containing /.
> Aside from calling any external programs, / will work just fine, and
> probably better,
> as it is more consistent with popular usage,
> and avoids the problem of remembering what to backwhack and what not to."
> =====================================================
>
> "System calls" includes opendir().
>
> By the way, I tested with both / and \ as file separators in the program I
> posted.
>
> ----- Original Message -----
> From: "Tanya Graham" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>;
> <[EMAIL PROTECTED]>
> Sent: Tuesday, May 29, 2001 6:31 PM
> Subject: RE: directory
>
> > it still doesn't work...it says there is no such file or directory...why
> > does it use back slashes, but when i specify the path i'm supposed to use
> > forward slashes?
> > tanya
> >
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> > Sent: Tuesday, May 29, 2001 3:35 PM
> > To: [EMAIL PROTECTED]
> > Subject: Re: directory
> >
> >
> >
> > >Hi,
> > >I am trying to open a directory, but I don't know how to specify it
> > exactly.
> > >I just need to open something in the C drive, under a folder named
> > PerlExp.
> > >I know this is a dumb question, but does anyone want to help me?
> > >thanks
> > >tanya graham
> >
> > Tanya,
> >
> > You can open directories in a mannor very similar to opening and reading a
> > file.
> >
> > #----------------------------------
> >
> > my $perlexp_dir = "C:/PerlExp";
> > opendir( DIRHNDLE, $perlexp_dir ) or die "Failed to open $perlexp_dir!";
> >
> > # if opendir works you now have a handle into the directory
> > # stored in DIRHNDLE. There are many ways to "work" with it
> >
> > # one way
> > my @file_list = readdir DIRHNDLE;
> > # now operate on the file_list array
> >
> > # another way
> > while ( defined( my $file = readdir DIRHNDLE ) ) {
> > # operate on $file
> > }
> >
> > # another way
> > for my $file ( readdir DIRHNDLE ) {
> > # operate on $file
> > }
> >
> > closedir( DIRHNDLE ) or die "Failed to close $perlexp_dir!";
> >
> > #----------------------------------
> >
> > Remember pick ONE of the methods above as once you've "used up" the handle
> > you need to close and re-open it to reset the cursor position back to the
> > top! The first method is best if you're going to need to make more than
> > one pass (for the reason just stated) through the data. Open the handle,
> > read it into an array, and then you can close it immediately. Now you can
> > rip through the array as many times as needed without having to pay any IO
> > penalties. The while and for examples are handy for when you know you only
> > need to make a single pass through the file listing.
> >
> > HTH,
> >
> > Chuck
> >
> >
> > _______________________________________________
> > Perl-Win32-Users mailing list
> > [EMAIL PROTECTED]
> > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
> > _______________________________________________
> > Perl-Win32-Users mailing list
> > [EMAIL PROTECTED]
> > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
> >
>
> _______________________________________________
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
> _______________________________________________
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
--
Glenn
=====
Due to the current economic situation, the light at the
end of the tunnel will be turned off until further notice.
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users