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: Perl Programming Help: Beginner:
Newcomer needs help ! How to write to file

 



banjiyaqub
Deleted

May 16, 2001, 9:35 AM

Post #1 of 8 (1726 views)
Newcomer needs help ! How to write to file Can't Post

What was to be a first perl script failed. Can'tr figure out why ? Help sort it out.

I intend to write an application output to a file, simply capturing event as recorded by the application into a file. The application understands string such as $2, so I write this script below, and I suppose a perl script can have arguments, so I can use $2 etc as arguments (e.g logp.pl $2) on the application command text box. .

The result is howver an empty.

my @ARGS = @ARGV;
my $logfile = "/tmp/yaqub/downlist.count";
if ( ! -e($logfile) ) {
open(LOGFILE, ">>$logfile") or die "Unable to create file $filename";
print "@ARGS\n";
close(LOGFILE);
}




randor
User

May 16, 2001, 9:41 AM

Post #2 of 8 (1725 views)
Re: Newcomer needs help ! How to write to file [In reply to] Can't Post

here is your problem:

print "@ARGS\n";

you need the filehandle to tell perl what to print to:

print LOGFILE "@ARGS\n";

hope this helps.





rjoseph
Novice

May 16, 2001, 9:47 AM

Post #3 of 8 (1724 views)
Re: Newcomer needs help ! How to write to file [In reply to] Can't Post

And the fact that >> does not create files, only appends to them. Try > instead.

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


randor
User

May 16, 2001, 10:33 AM

Post #4 of 8 (1719 views)
Re: Newcomer needs help ! How to write to file [In reply to] Can't Post

actually rjoseph,
i do not want to step on toes, but >> will also write files.

just thought id throw that in.

hope that helps.




randor
User

May 16, 2001, 10:34 AM

Post #5 of 8 (1719 views)
Re: Newcomer needs help ! How to write to file [In reply to] Can't Post

err, write NEW files



Cure
User

May 16, 2001, 7:44 PM

Post #6 of 8 (1704 views)
Re: Newcomer needs help ! How to write to file [In reply to] Can't Post

If FILEHANDLE is omitted, prints by default to standard output (or to the last selected output channel--see /select). If LIST is also omitted, prints $_ to the currently selected output channel. To set the default output channel to something other than STDOUT use the select operation


print "@ARGS\n" is equivalent to print STDOUT "@ARGS\n"

Instead of writing to the txt file you are print the output to the filehandle STDOUT.

you need to do this print LOGFILE "@ARGS\n";

print $_ is equivalent to print STDOUT $_ which is equivalent to print {STDOUT} $_



Cure






banjiyaqub
Deleted

May 17, 2001, 5:45 AM

Post #7 of 8 (1693 views)
Re: Newcomer needs help ! How to write to file [In reply to] Can't Post

Thank you all,

Your suggestions took me a step futher than my former situatiom. Now the file is created with text input. The script now is as below:

my @ARGS = @ARGV;
my $logfile = "/tmp/yaqub/downlist.count";
if ( ! -e($logfile) ) {
open(LOGFILE, ">>$logfile") or die "Unable to create file $logfile";
print LOGFILE "@ARGS\n";
close(LOGFILE);
}

But it never appends. If the file is deleted a new one is created with just a line of expected text when the script is run but cannot append to the file if the file already exist.

Any idea why this is so ? I suppose it should append everytme the script run as command.





Mortimer
journeyman

May 18, 2001, 5:18 AM

Post #8 of 8 (1680 views)
Re: Newcomer needs help ! How to write to file [In reply to] Can't Post

It won't append because after you create and write to the file, your if condition then returns false.

if ( ! -e($logfile) ) { ...if the file does not exist...

You actually say the answer in your last post...

> but cannot append to the
> file if the file already exist

Dave.


 
 


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

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