A hopefully simple question

H

Harold Pritchett

I am modifying a perl script which I did not write.

I am at best a beginning perl programmer, but have been
writing code in other languages for over 30 years.

My question is the difference in the following two
statements:

print FILE "$_";
print FILE ">$_";

The file is the same, and is opened with the statement:

&lopen($FH, ">>", "$opt_f$suffix") ||
die("Can't append to $opt_f$suffix: $!");
$is_open = 1;

This is done in a subroutine and the file handle FILE is
passed into the subroutine as FH. The file name obviously
is passed in two pieces, $opt_f and $suffix.

I've tried to find this on the web, but have failed
miserably.

Thanks

Harold
 
P

phaylon

Harold said:
Subject: A hopefully simple question

Weird problem ..
I've tried to find this on the web, but have failed miserably.

... but you're lucky, I've got a simple question, what came out when you
tried? :D Also: Is that your real code? I was unable to find something in
perldoc about lopen. Is this a subroutine as & indicates?
 
T

thundergnat

Harold said:
My question is the difference in the following two
statements:

print FILE "$_";
print FILE ">$_";

What happened when you tested it?



my @words = qw/My question is the difference in the following two
statements/;
print "$_\n" for @words;
print ">$_\n" for @words;
 
H

Harold Pritchett

phaylon said:
Harold Pritchett wrote:




Weird problem ..




.. but you're lucky, I've got a simple question, what came out when you
tried? :D Also: Is that your real code? I was unable to find something in
perldoc about lopen. Is this a subroutine as & indicates?

This is a subroutine, defined in shlock.pl and majordomo.pl.

The code snippet from shlock.pl looks like this:

# open a file locked for exclusive access;
# we remember the name of the lock
# file, so that we can delete it when we close the file
#
sub main'lopen {
local($FH) = shift;
local($mode) = shift;
local($file) = shift;
# $fm is what will actually get passed to open()
local($fm) = "$mode$file";
local($status);

# create name for lock file
local($lockfile) = $file;
$lockfile =~ s,([^/]*)$,L.$1,;

# force unqualified filehandles into callers' package
local($package) = caller;
$FH =~ s/^[^':]+$/$package'$&/;

return undef unless &main'set_lock("$lockfile");

# Got the lock; now try to open the file
if ($status = open($FH, $fm)) {
# File successfully opened; remember the lock file for deletion
$lock_files[fileno($FH)] = "$lockfile";
} else {
# File wasn't successfully opened; delete the lock
&main'free_lock($lockfile);
}
# return the success or failure of the open
return $status;
}

Obviously, this code is a majordomo archive module, and I want to
understand how it works so I can modify it's behavior.

Harold
 
H

Harold Pritchett

Jim said:
The second statement will print a '>' character before it prints the
contents of the variable $_ (to FILE). The first statement will not.

Obvious now that I see it. Sometimes it's hard to tell data from code

Thank you very much.

Harold
 
B

Brian McCauley

Harold said:
This is a subroutine, defined in shlock.pl and majordomo.pl.

The code snippet from shlock.pl looks like...

.... shlock.pl is a Perl4 script.

There in general much better ways to do things than there were in Perl4.

Comparatively few poeple here can remeber Perl4 well if at all. I, for
example, have only been using Perl for a little over a decade so I
hardly touched Perl4.

Perl5 is almost but not quite 100% compatible with Perl4.

Once of the things that is different is what happens if you use a GLOB
(kinda poor-man's reference) in a string context. In Perl4 the
delimiter between the namespace and the symbol is a single quote, in
Perl5 it's a double colon.
# open a file locked for exclusive access;
# we remember the name of the lock
# file, so that we can delete it when we close the file
#
sub main'lopen {
local($FH) = shift;
local($mode) = shift;
local($file) = shift;
# $fm is what will actually get passed to open()
local($fm) = "$mode$file";
local($status);

# create name for lock file
local($lockfile) = $file;
$lockfile =~ s,([^/]*)$,L.$1,;

# force unqualified filehandles into callers' package
local($package) = caller;
$FH =~ s/^[^':]+$/$package'$&/;

return undef unless &main'set_lock("$lockfile");

# Got the lock; now try to open the file
if ($status = open($FH, $fm)) {
# File successfully opened; remember the lock file for deletion
$lock_files[fileno($FH)] = "$lockfile";
} else {
# File wasn't successfully opened; delete the lock
&main'free_lock($lockfile);
}
# return the success or failure of the open
return $status;
}

Obviously, this code is a majordomo archive module, and I want to
understand how it works so I can modify it's behavior.

So, can you point out the parts that you are finding difficulty finding
in the refernce manuals or the passages in the reference manuals you are
finding unclear.

Note: to make sense of this code you using contemporary documentation
you need to translate all the singe quetes in the middle of symbol
names into double colons.
 
C

Chris Mattern

Harold said:
I am modifying a perl script which I did not write.

I am at best a beginning perl programmer, but have been
writing code in other languages for over 30 years.

My question is the difference in the following two
statements:

print FILE "$_";

This prints the contents of $_ to the filehandle FILE.
print FILE ">$_";

This prints ">" and then the contents of $_ to the
filehandle FILE.
The file is the same, and is opened with the statement:

&lopen($FH, ">>", "$opt_f$suffix") ||
die("Can't append to $opt_f$suffix: $!");
$is_open = 1;

This is done in a subroutine and the file handle FILE is
passed into the subroutine as FH. The file name obviously
is passed in two pieces, $opt_f and $suffix.

I've tried to find this on the web, but have failed
miserably.

Thanks

Harold

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top