CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



sri
New User

Mar 3, 2002, 1:50 PM

Post #1 of 6 (1746 views)
file handles Can't Post

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

Post #2 of 6 (1737 views)
Re: [sri] file handles [In reply to] Can't Post

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

Post #3 of 6 (1729 views)
Re: [Pro_4] file handles [In reply to] Can't Post

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

Hmm I think you mean:

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

Angelic


yapp
User

Mar 8, 2002, 5:02 AM

Post #4 of 6 (1723 views)
Re: [RedRum] file handles [In reply to] Can't Post

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

Post #5 of 6 (1721 views)
Re: [yapp] file handles [In reply to] Can't Post

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

Post #6 of 6 (1715 views)
Re: [RedRum] file handles [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives