Click to See Complete Forum and Search --> : need help writing a perl script to open a file


RedOctober
11-08-2000, 03:13 PM
I am trying to write a simple script that will open a .txt file. Here is my prob. the file I am trying to open is located here C:\Perl\bin\testfile.txt. I placed it in the perl path hoping pearl will find it easier. Here are some examples script I have written that did nothing:

$MYFILE = "testpage.txt";
open (MYFILE) | | die;
for this I get a perl died at line 1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
open (MYFILE, TESTPAGE);
this just jumped back to the c: prompt
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if (open(MYFILE, "testpage.txt")){
#run if this works
} else {
print "cannot open file \n";
exit 1;
}
got some syntax error with this one
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

can anyone tell what I am doing wrong. Remember I am still learnng Perl here.

Thank you again



------------------
big up to all a de linux man dem ina de place

YaRness
11-08-2000, 03:18 PM
this is how i open a file that is on the command line (ie, do "perl script.pl somefile.txt")

i'm gonna check on your code in a second here.

if (! open(INFILE,$ARGV[0]))
{
print STDERR "programname: input file did not open\n";
exit;
}

YaRness
11-08-2000, 03:25 PM
test.pl:

#!c:\perl\bin\perl.exe -w


if (open(MYFILE, "test.txt"))
{
print "file opened successfully\n";
}
else
{
print "something ain't right\n";
}

while ($line = <MYFILE> )
{print "$line\n";}


test.txt:

yar
yar2
yar3


output of "perl test.pl":

C:\pscripts>perl test.pl
file opened successfully
yar

yar2

yar3


C:\pscripts>

TheLinuxDuck
11-08-2000, 03:27 PM
Originally posted by RedOctober:

$MYFILE = "testpage.txt";
open (MYFILE) | | die;


The first argument for the open command is a file handle, which is used for reading from/writing to the file. The second argument is the name of the file you wish to open. The code will need to do something like:

my($filename)="testfile.txt";
open(FILEHANDLE,$filename) or die "Cannot open file $filename: $!\n";


http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

RedOctober
11-08-2000, 03:27 PM
if (! open(INFILE,$ARGV[0])){
print STDERR "programname: input file did not open\n";
exit;}

sorry to be so slow buy I dont get the $argv[0] nor the !open. Please, please explain.

RedOctober
11-08-2000, 03:30 PM
OK ...now I am getting it......let me try again thanks guys

YaRness
11-08-2000, 03:30 PM
something else that worked:


#!c:\perl\bin\perl.exe -w


$inputfile = "test.txt";
open (MYFILE,$inputfile) | | die "**** happens";

while ($line = <MYFILE> )
{print "$line\n";}



note this also worked with $MYFILE instead of $inputfile, but doing so could be confusing.

RedOctober
11-08-2000, 03:33 PM
Yarness, I copied and paste your prior code and I get the "something ain't right" response. I know the file exits.

YaRness
11-08-2000, 03:39 PM
# "!" is the not operator. basically
# this says "if the open command returns
# false, do the stuff in the {}." because if
# the open command returns false, then
# "not false" is true
#
# and what THAT means is that if the file
# doesn't open properly, the text message
# is displayed
#
# (basic boolean algebra:
# "not true" is equivalent to false, and
# "not false" is equivalent to true).
#
# @ARGV is the arrray that holds
# command line parameters.
# (arrays as a whole in perl are referenced
# as @something. each member of an array
# is referenced as $something[0],
# $something[1], etc)
#
# so if i execute
# "perl test.pl param1 param2 param3"
# then $ARGV[0] is "param0",
# $ARGV[1] is "param1",
# $ARGV[2] is "param2",
# (arrays are always counted from 0, so
# an array with 12 members has slots
# 0 through 11 filled)
#
#
if (! open(INFILE,$ARGV[0]))
{
print STDERR "programname: input file did not open\n";
exit;
}


[This message has been edited by YaRness (edited 08 November 2000).]

[This message has been edited by YaRness (edited 08 November 2000).]

[This message has been edited by YaRness (edited 08 November 2000).]

YaRness
11-08-2000, 03:46 PM
Originally posted by RedOctober:
Yarness, I copied and paste your prior code and I get the "something ain't right" response. I know the file exits.

make sure the name of the text file in your directory, and the name of the file in the perl script are the same. i used "test.txt" above. and you might wanna quote which code you are using just to make double sure i'm on the same track with you.

YaRness
11-08-2000, 03:49 PM
TLD, since your attention is here, i had a question: does "use strict" completely forbid one to have global variables, or is there a way to set them so all the subroutines in the file can use them (it's just a pain to pass parameters in perl. maybe i should work on OOP-ing it.....)

RedOctober
11-08-2000, 03:59 PM
I finally got it to work.....take a look..I placed the complete path in the script

$inputfile = "c:/perl/bin/testpage.txt";
open (MYFILE,$inputfile) | | die "**** happens";while ($line = <MYFILE> ){
print "$line\n";
}

whew...thanks guys

YaRness
11-08-2000, 04:05 PM
more on "!" and using an if statement:

if it's not clear yet, here's the difference between using "!" and not using it above:


if (open(INFILE,$ARGV[0]))
{
print "this will print if the file DOES open\n";
}
else
{
print "this will print if the file DOES NOT open\n";
}



if (! open(INFILE,$ARGV[0]))
{
print "this will print if the file DOES NOT open\n";
}
else
{
print "this will print if the file DOES open\n";
}


btw, the open command, and i think any perl command that doesn't return a specific value, will return a value of true (which i think is represented by a non-zero value, ie, 1) if the command completes successfully, and a value of false (represented by the number zero) if the command fails. the line of code:


if (! open(MYFILE, "test.txt))
{print "something\n";}


is broken down by perl like so: first it runs the open command, which returns either true or false. let's say it returns false. this value of false is operated on by the "!", so now we have a value of true. that value of true is what the if command gets, and it then does whatever follows in the {} block.

(i think yo can actually do if statements like this: "if (! open(MYFILE, "test.txt")) print "something\n";", which is called a oneliner, but i find it more eyeball-pleasing, and less hassle to decode, if i always put the command in a {} block. if you do more than one command, you HAVE to use a {} block).

YaRness
11-08-2000, 04:10 PM
Originally posted by RedOctober:
I finally got it to work.....take a look..I placed the complete path in the script

$inputfile = "c:/perl/bin/testpage.txt";
open (MYFILE,$inputfile) | | die "**** happens";while ($line = <MYFILE> ){
print "$line\n";
}

whew...thanks guys


rawkin. what is the directory you are running the script from? if you are currently in the c:\perl\bin\ dir and you run the script, it shouldn't need the full path.

ie

c:\>cd perl\bin
c:\Perl\bin>perl test.pl


or if test.txt is in, say, c:\scripts\text.txt, then


c:\>cd scripts
c:\scripts>perl test.pl


of course, if c:\perl\bin isn't in your path, then substitute c:\perl\bin\perl.exe for perl , or set up yer dos window so c:\perl\bin is in your path.

RedOctober
11-08-2000, 04:26 PM
Well perl is in the path. Usually at the C: I just enter the script name eg

C:\>testfile.pl

this usually excutes the program.

c:\>perl testfile.pl

I dont need to do this.

I associates all .pl file with the Perl Interpretor.

TheLinuxDuck
11-08-2000, 04:32 PM
Originally posted by YaRness:
TLD, since your attention is here, i had a question: does "use strict" completely forbid one to have global variables, or is there a way to set them so all the subroutines in the file can use them (it's just a pain to pass parameters in perl. maybe i should work on OOP-ing it.....)

You can define psuedo-globals using the my keyword. Like:

#!/usr/bin/perl -w
use strict;
#
# External to all functions and blocks
my(@globalarray);
if(open(MYFILE,"test.txt")) {
while(<MYFILE> ) {
dosomethingwithfiledata($_);
}
close(MYFILE);
print "@globalarray\n";
}
#
sub dosomethingwithfiledata
{
my($buffer)=$_[0];
$buffer=~ tr/a-z/A-Z/; # lower to uppercase
push @globalarray, $buffer;
}


It's really pretty easy to pass references into a subroutine to allow you to edit the contents, without globals. Here's the previous program without globals.

#!/usr/bin/perl -w
use strict;
#
if(open(MYFILE,"test.txt")) {
my(@array); # define inside main block
while(<MYFILE> ) {
dosomethingwithfiledata($_,\@array);
} # \ passes the reference to the array
close(MYFILE);
print "@array\n";
}
#
sub dosomethingwithfiledata
{
# now, we're dunping the two passed items
# into the $buffer and in a scalar reference
my($buffer,$arrayref)=@_;
$buffer=~ tr/a-z/A-Z/;
# We just use the scalar reference as though
# it were the actual name of the array
push @$arrayref, $buffer;
}


It can get tricky to pass around lots of different variables, especially when many of the subs use some of the same variables. I come from the train of thought that globals aren't good to get used to using. I don't know how much it really matters with perl, but I try to not use globals unless I'm stuck, or testing something out.

------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

RedOctober
11-08-2000, 04:39 PM
Doesd anyone have a code than can ping a server and email me if there is no response.....or guide to a book that can help

YaRness
11-08-2000, 04:45 PM
i thought i remembered trying to declare global stuff with my. i dunnno, it's been a week or two since i looked at it, i'll eyeball it again sometime.

YaRness
11-08-2000, 04:49 PM
someproc($_,\@array);
...
my($buffer,$arrayref)=@_;


yeah, i can do that, it's so fugly though. and it makes trying to debug and follow stuff hard.

and i'm lazy.

and i haven't started really modulating anything (yet), so global variables are OTAY.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------

[This message has been edited by YaRness (edited 08 November 2000).]

YaRness
11-08-2000, 04:50 PM
Originally posted by RedOctober:
Doesd anyone have a code than can ping a server and email me if there is no response.....or guide to a book that can help

in linux or winderz? would prolly be pretty easy to scratch up a shell script to do that. i'm in winderz today so i can't help you.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------

TheLinuxDuck
11-08-2000, 05:04 PM
Originally posted by RedOctober:
Doesd anyone have a code than can ping a server and email me if there is no response.....or guide to a book that can help

This script will email you the results, regardless of the outcome:

#!/bin/bash
ping 123.123.123.123 -c 10 > /tmp/pingo
mail red@october.com -s "ping for 123.123.123.123" < /tmp/pingo




------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

RedOctober
11-08-2000, 05:04 PM
This would be for Windows2000

YaRness
11-08-2000, 05:10 PM
Originally posted by RedOctober:
This would be for Windows2000

if you had some good mail proggy, you could prolly do it with .bat files, like


@echo off
ping 127.0.0.1 > temp.log
:then use some mail client to send temp.log to red@october.com
del temp.log

but i don't know much about such software for windows.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------