Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

opening a file to read and write

0 views
Skip to first unread message

ion...@yahoo.com

unread,
Feb 10, 2005, 9:39:56 AM2/10/05
to
open $file, '+>', $File::Find::name;

This did not do what I expected. It deleted all my file's contents!

Here's my whole program:

#!/c:/perl/bin/perl.exe
#change shebang line for my Windows computer for all scripts
use warnings;
use strict;

my $shebang = '#!/c:/perl/bin/perl.exe';

use File::Find;
find(\&wanted, $ARGV[0]);
sub wanted
{
if (/\.pl$/)
{
open my $file, '<', $File::Find::name;
my $contents = <$file>;
close $file;
return if not $contents;
chomp $contents;
return if $contents =~/^$shebang$/;
if ($contents =~/^#!/)
{
open $file, '<', $File::Find::name;
my @contents = <$file>;
close $file;
$contents[0] = $shebang."\n";
open $file, '>', $File::Find::name;
print $file @contents;
close $file;
print "Changed: $File::Find::name\n";
}
}
}

Did I really have to open and close so many times? Thanks!

wana

Paul Lalli

unread,
Feb 10, 2005, 10:11:07 AM2/10/05
to
<ion...@yahoo.com> wrote in message
news:1108046396.0...@o13g2000cwo.googlegroups.com...

> open $file, '+>', $File::Find::name;
>
> This did not do what I expected. It deleted all my file's contents!

Which is, of course, exactly what the documentation says it will do. It
is your expectations that were misguided.

> Here's my whole program:
>
> #!/c:/perl/bin/perl.exe
> #change shebang line for my Windows computer for all scripts

Why? Windows couldn't care less what path is specified in the shebang.

> use warnings;
> use strict;
>
> my $shebang = '#!/c:/perl/bin/perl.exe';
>
> use File::Find;
> find(\&wanted, $ARGV[0]);
> sub wanted
> {
> if (/\.pl$/)

Arrrrg. More horribly formatted code. Please use a real newsserver
until Google Groups gets their act together.

> {
> open my $file, '<', $File::Find::name;
> my $contents = <$file>;
> close $file;
> return if not $contents;
> chomp $contents;
> return if $contents =~/^$shebang$/;
> if ($contents =~/^#!/)
> {
> open $file, '<', $File::Find::name;
> my @contents = <$file>;

Why did you open, read one line, close, and open again? If you want to
'rewind' the input file, just use seek. Or you could even just read the
rest of the file in without seeking, storing everything in @contents:
my @contents = $contents, <$file>;

> close $file;
> $contents[0] = $shebang."\n";
> open $file, '>', $File::Find::name;
> print $file @contents;
> close $file;
> print "Changed: $File::Find::name\n";
> }
> }
> }
> Did I really have to open and close so many times? Thanks!

No. I would strongly suggest you read the documentation. In this case,
perldoc perlopentut
See the section "Mixing Reads and Writes", and read the part that tells
you this is almost certainly not the approach you should be using (3rd
paragraph of this section)

Paul Lalli

Chris Mattern

unread,
Feb 10, 2005, 10:19:07 AM2/10/05
to
ion...@yahoo.com wrote:

> open $file, '+>', $File::Find::name;
>
> This did not do what I expected. It deleted all my file's contents!
>

Why did you not read perldoc -f open, then? It would've told you
that's what would happen. It also tells you the right way to do
it, and given you warning about doing it that way, and suggested
yet another approach.

--
Christopher Mattern

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

Michael Korte

unread,
Feb 10, 2005, 10:23:55 AM2/10/05
to

<ion...@yahoo.com> schrieb im Newsbeitrag
news:1108046396.0...@o13g2000cwo.googlegroups.com...

> Did I really have to open and close so many times? Thanks!

why do you need a shebang at DOS ? there is no need in, because DOS will
know what interpreter should work with your string while you use extension
*.pl

I do not know any possibility to open a file for both reading and writing,
but i can offer you a smaller version of your function wanted that needs
only a 2times opening.

sub wanted{
if(/\.pl$/){


open my $file, '<', $File::Find::name;

my @cont = <$file>; # read the whole file to array
close($file);
if($cont[0]=~/^#!/){ # ahh there is a shebang ..
$cont[0] = $shebang . "\n"; # overwrite the first line


}
open my $file, '>', $File::Find::name;

print $file @cont;
close($file);


print "Changed: $File::Find::name\n";
}
}

if you would use an OS you also could do your work even faster and with a
less amount of lines ;-)

HTH
Michael

--
$J=$A=$P=@H;
$JAPH=\&JAPH;@H=split("","j".&$JAPH."t".&$JAPH."lcuahp".
&$JAPH."ksneehetorrar");$JA=1;$PP=@H-1;
$JAP=@H/6;while($JA){print$PH.$JAH.$H[$A];$P++;$A+=6;if($P%
$JAP==$HHH){$J++;$A=$PH+$J;}if($JJA+$P>$PP){undef
$JA;$PH;printeval(reverse";))01(rhc(nruter")}}sub
JAPH{eval(reverse";))23(rhc(nruter")}


David K. Wall

unread,
Feb 10, 2005, 10:35:48 AM2/10/05
to
<ion...@yahoo.com> wrote:

> open $file, '+>', $File::Find::name;
>
> This did not do what I expected. It deleted all my file's
> contents!

That's what it's supposed to do. See 'perldoc -f open', and pay
particular attention to the 6th paragraph.


--
David Wall

ion...@yahoo.com

unread,
Feb 10, 2005, 10:51:17 AM2/10/05
to

Michael Korte wrote:
> <ion...@yahoo.com> schrieb im Newsbeitrag
> news:1108046396.0...@o13g2000cwo.googlegroups.com...
>
> > Did I really have to open and close so many times? Thanks!
>
> why do you need a shebang at DOS ? there is no need in, because DOS
will
> know what interpreter should work with your string while you use
extension
> *.pl

Thanks to all for advice. I'll go do my reading.

The reason the shebang is needed in Windows is for Apache. Maybe my
Apache config is not set up right, but it only runs my scripts in
cgi-bin with the right shebang line.

The reason for opening and reading the first line only was to try and
prevent reading in the whole file if it didn't need to be changed. I
guess none of my scripts are that long that it will save much time.

wana

A. Sinan Unur

unread,
Feb 10, 2005, 10:43:19 AM2/10/05
to
ion...@yahoo.com wrote in news:1108046396.098348.238470
@o13g2000cwo.googlegroups.com:

> #!/c:/perl/bin/perl.exe
> #change shebang line for my Windows computer for all scripts

There is absolutely no reason for this.

Sinan

Gunnar Hjalmarsson

unread,
Feb 10, 2005, 11:20:17 AM2/10/05
to
ion...@yahoo.com wrote:
> The reason the shebang is needed in Windows is for Apache.

If you put a copy of perl.exe in C:/usr/bin, the 'standard' shebang line
works, and you don't need to struggle with changing it.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

ion...@yahoo.com

unread,
Feb 10, 2005, 11:42:21 AM2/10/05
to

Gunnar Hjalmarsson wrote:
> ion...@yahoo.com wrote:
> > The reason the shebang is needed in Windows is for Apache.
>
> If you put a copy of perl.exe in C:/usr/bin, the 'standard' shebang
line
> works, and you don't need to struggle with changing it.
>

Brilliant! Thank you!

wana (kicking self)

A. Sinan Unur

unread,
Feb 10, 2005, 12:12:29 PM2/10/05
to
ion...@yahoo.com wrote in
news:1108053741....@o13g2000cwo.googlegroups.com:

None of this hassle is necessary if you have:

ScriptInterpreterSource Registry-Strict

in httpd.conf (can be set on a location or directory basis as well) and:

[HKEY_CLASSES_ROOT\Perl\shell\ExecCGI\command]
@="\"C:\\opt\\Perl\\bin\\perl.exe\" \"%1\" %*"

in the windows registry

Sinan

Brian McCauley

unread,
Feb 10, 2005, 12:33:26 PM2/10/05
to
ion...@yahoo.com wrote:

> open $file, '+>', $File::Find::name;
>
> This did not do what I expected. It deleted all my file's contents!

Others have directed you to perlfunc and perlopentut.

But nobody as far as I can see has mentioned FAQ: "How come when I open
a file read-write it wipes it out?".

If you do nothing else before asking a question you should at the very
least ensure that your question is not _exactly_ the same as one in the FAQ.

Tad McClellan

unread,
Feb 10, 2005, 5:33:00 PM2/10/05
to
ion...@yahoo.com <ion...@yahoo.com> wrote:
> Michael Korte wrote:

>> why do you need a shebang at DOS ? there is no need in, because DOS
> will
>> know what interpreter should work with your string while you use
> extension
>> *.pl

> The reason the shebang is needed in Windows is for Apache.


What on earth does Apache have to do with Perl programs?

Could it be that you think that CGI and Perl are the same thing?

I hope not.

If your Perl program is running in a non-standard environment
then you should mention that when asking questions, as the
answer may then be different.

The "normal" place to run Perl programs is from the command line,
that is what most people will assume unless you tell them otherwise.


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

ion...@yahoo.com

unread,
Feb 10, 2005, 7:48:01 PM2/10/05
to
Tad McClellan wrote:

>
> The "normal" place to run Perl programs is from the command line,
> that is what most people will assume unless you tell them otherwise.
>

Which 'command line' is standard?

Most or all Unix shells will be the same with respect to Perl but not
the Windows command line, at least regarding quoting.

What about double-clicking on icon from GUI of various OS's?

You are right that I should specify how I am invoking my scripts.

wana

Jürgen Exner

unread,
Feb 10, 2005, 8:25:56 PM2/10/05
to
ion...@yahoo.com wrote:
> Tad McClellan wrote:
>> The "normal" place to run Perl programs is from the command line,
>> that is what most people will assume unless you tell them otherwise.
>>
>
> Which 'command line' is standard?

Doesn't really matter, pretty much any will do.

> Most or all Unix shells will be the same with respect to Perl but not
> the Windows command line, at least regarding quoting.

But that's not a Perl issue. Once you've figured out how to get the quoting
(or file pathes or whatever for that matter) right for your OS and command
line shell, then from there on it's all the same. And the Perl part starts
only at the "from there on".

> What about double-clicking on icon from GUI of various OS's?

Well, why would you want to?

jue


ion...@yahoo.com

unread,
Feb 10, 2005, 10:22:59 PM2/10/05
to

Tad McClellan wrote:

> The "normal" place to run Perl programs is from the command line,
> that is what most people will assume unless you tell them otherwise.
>
>

Forget any other example I have given. Here is a perfect one: I
cannot execute my Perl programs from the command line under Cygwin
unless they have the proper shebang line. That is definitely a
standard way to execute a perl program and it is different from
executing from the command line provided by Windows were the OS
determines the type of file by the extension.

wana

A. Sinan Unur

unread,
Feb 10, 2005, 10:51:21 PM2/10/05
to
ion...@yahoo.com wrote in news:1108092179.445838.283300
@z14g2000cwz.googlegroups.com:

>
> Tad McClellan wrote:
>
>> The "normal" place to run Perl programs is from the command line,
>> that is what most people will assume unless you tell them otherwise.
>
> Forget any other example I have given. Here is a perfect one: I
> cannot execute my Perl programs from the command line under Cygwin
> unless they have the proper shebang line.

You were originally changing the shebang lines to

#!/c:/perl/bin/perl.exe

which is a monstrosity.

So, I am assuming the original files had unixy shebang lines, maybe:

#! /usr/bin/perl

or similar.

If you are using the Cygwin environment, then you should use the Perl
distribution built for that environment, in which case, the second
shebang line above would work without modification.

Regardless of distribution, I cannot really see the point of changing
shebang lines to first version when you are on a Windows computer.
Either you have the ActiveState or a similar distribution in which case
the shebang line is not relevant to locating the perl executable, or you
have the Cygwin distribution which is almost exactly like being on a
unixy system.

Sinan

ion...@yahoo.com

unread,
Feb 10, 2005, 11:07:21 PM2/10/05
to

A. Sinan Unur wrote:
...

> Regardless of distribution, I cannot really see the point of changing

> shebang lines to first version when you are on a Windows computer.
> Either you have the ActiveState or a similar distribution in which
case
> the shebang line is not relevant to locating the perl executable, or
you
> have the Cygwin distribution which is almost exactly like being on a
> unixy system.
>
> Sinan

That's probably the problem. I am using ActiveState only, yet I
sometimes work with Cygwin where I do not have the Cygwin Perl
distribution installed, so I have to call on ActiveState Perl there
too.

The problem is that I prefer a Unix-like environment, yet Windows
supports the hardware on my laptop much better than any version of
Linux (Suse does the best though). My next computer will certainly be
an OS X laptop when this one fails and/or I can afford it.

I will definitely take your advice on the registry changes to fix the
problem with executing my cgi scripts.

wana

0 new messages