Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
file handles



sri
New User

Mar 3, 2002, 1:50 PM


Views: 3326
file handles

could anybody tell me how to prompt a user for a file name is it $x=<>; ? and then

1.prints to screen total number of records in the file

2. prints the first and last records

prompts the user for an output filename and prints all the records one per line to the output file


Pro_4
User

Mar 5, 2002, 6:34 PM


Views: 3317
Re: [sri] file handles

Cgi or windows command thingie...?

anyways this is how you do it with both:

[perl]

CGI:

use CGI::Standard;

$filename = param('filename'); #filename is the name of the input box

open(FH, "<$filename") || die $1;

@stuff = <FH>

close(FH);



Windows thingie:

$filename = <STDIN> #pauses until the user types in something and presses enter

open(FH, "<$filename") || die $1;

@stuff = <FH>

close(FH);

[perl]



Hope that helps :) If you need something explained just ask. Also i dont know how your database is setup so that determines how many records are in it but just basically loop through the array and print out the stuff and then while you are looping through to print it out, just print it to another file too by prompting them after you prompted them the first time.
(sorry about any confusion in that, English is not my best subject)



P.S. Ahh how do you get this perl tag thingie to work!?!?!?!


(This post was edited by Pro_4 on Mar 5, 2002, 6:37 PM)


Paul
Enthusiast

Mar 7, 2002, 11:49 AM


Views: 3309
Re: [Pro_4] file handles

>>
open(FH, "<$filename") || die $1;
<<

Hmm I think you mean:

open(FH, "<$filename") || die $!;

Angelic


yapp
User

Mar 8, 2002, 5:02 AM


Views: 3303
Re: [RedRum] file handles

just-to-let-you-know:

if you use open ... or die, the ( .. ) can be removed.
btw. using "<$filename" just tells perl it should read the file. Omitting it won't make a difference.

[perl]open FH, $filename or die "Can't open $filename: $!";[/perl]

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


Paul
Enthusiast

Mar 8, 2002, 5:18 AM


Views: 3301
Re: [yapp] file handles

I was just pointing out the $1 should have been $! I guess I should have changed the rest whilst I was on a roll.

BTW has anyone ever had any luck using:

"+<bla.."

I can never get that sucker to work :(


(This post was edited by RedRum on Mar 8, 2002, 5:20 AM)


yapp
User

Mar 9, 2002, 1:53 AM


Views: 3295
Re: [RedRum] file handles

Sorry, I didn't see the $1 (please make it bold or something)

The +< sucker (read+write I guess?) requires that the file already exists.
Use this instead:

[perl]
use Fcntl qw(:DEFAULT :flock);

sysopen(FH, $file, O_CREAT|O_RDWR) or die "Can't rdwr $file: $!";
flock(FH, LOCK_EX);

my @data = <FH>;
# modifiy @data;

seek(FH, 0, 0) or die "Can't seek to begin: $!";
truncate(FH, 0) or die "Can't erase contents: $!";

print FH, @data;
close(FH);
[/perl]

If you don't want to slurp anything, use a secondfile wherein you store the new contents. (read in $file, write to "$file~"). At the end, seek both files to the begin, start reading from "$file~", and write that back into $file. Don't forget the truncate the $file first!

Maybe truncating $file to the size of the "$file~" is a very nice option, since you keep the diskspace reserved. (so it doesn't need re-allocation)

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool