[30018] in Perl-Users-Digest
Perl-Users Digest, Issue: 1261 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 6 21:09:43 2008
Date: Wed, 6 Feb 2008 18:09:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 6 Feb 2008 Volume: 11 Number: 1261
Today's topics:
Re: add match condition <bik.mido@tiscalinet.it>
Re: add match condition <ben@morrow.me.uk>
Re: Connecting to a Server on a Server <ben@morrow.me.uk>
Re: CPerl mode problem: syntax highlighting stumbles ov <omouse@gmail.com>
Re: eval EXPR with maximum execution time? alx__21@hotmail.com
Re: eval EXPR with maximum execution time? <ben@morrow.me.uk>
Re: eval EXPR with maximum execution time? xhoster@gmail.com
IPC Message Queue Help jsahiwal@gmail.com
Re: moving binary data from one RDBMS to Other <m@rtij.nl.invlalid>
Re: PDF API for password encryption? <bik.mido@tiscalinet.it>
To modify a file from the command line clearguy02@yahoo.com
Re: To modify a file from the command line clearguy02@yahoo.com
Re: To modify a file from the command line <joost@zeekat.nl>
Re: To modify a file from the command line clearguy02@yahoo.com
Re: To modify a file from the command line <joost@zeekat.nl>
Re: To modify a file from the command line <ben@morrow.me.uk>
Re: To modify a file from the command line clearguy02@yahoo.com
Re: To modify a file from the command line <uri@stemsystems.com>
Re: To modify a file from the command line clearguy02@yahoo.com
Re: To modify a file from the command line <ben@morrow.me.uk>
Re: To modify a file from the command line <uri@stemsystems.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 06 Feb 2008 23:32:15 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: add match condition
Message-Id: <29dkq3t056ump301q226hi3r26k2pfiuk3@4ax.com>
On Tue, 05 Feb 2008 17:21:35 GMT, Jürgen Exner <jurgenex@hotmail.com>
wrote:
>>not matching the string 'disable' in $_
>
> if (m/$featureId/ and index($_, 'disable') == -1)
>
>This is assuming that $featureId actually does contain a regex. Otherwise it
>would be better to replace the match with an index() call, too.
I beg to differ, since a match is more intuitively readable (to a Perl
programmer) and AFAIK when a constant string is involved, then it is
optimized to a simple index() anyway.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Thu, 7 Feb 2008 00:18:31 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: add match condition
Message-Id: <nbvp75-ipf1.ln1@osiris.mauzo.dyndns.org>
Quoth Michele Dondi <bik.mido@tiscalinet.it>:
> On Tue, 05 Feb 2008 17:21:35 GMT, Jürgen Exner <jurgenex@hotmail.com>
> wrote:
>
> >>not matching the string 'disable' in $_
> >
> > if (m/$featureId/ and index($_, 'disable') == -1)
> >
> >This is assuming that $featureId actually does contain a regex. Otherwise it
> >would be better to replace the match with an index() call, too.
>
> I beg to differ, since a match is more intuitively readable (to a Perl
> programmer) and AFAIK when a constant string is involved, then it is
> optimized to a simple index() anyway.
It may actually be optimized further than that: if the match has a
fixed portion, and the matched string is shorter than that, the match is
not even attempted.
Ben
------------------------------
Date: Wed, 6 Feb 2008 21:42:20 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Connecting to a Server on a Server
Message-Id: <s6mp75-m2f1.ln1@osiris.mauzo.dyndns.org>
Quoth deadpickle <deadpickle@gmail.com>:
> On Feb 6, 1:18 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> >
> > If what you mean is that updraft is firewalled, and the only way in is
> > via a ssh tunnel, then you may be able to use Net::SSH2 to create a
> > tunneled connection. See the ->tcpip method of Net::SSH2. Note that this
> > requires that updraft supports ssh2, and also requires libssh2 installed
> > on the client.
>
> So then if this works do I still need to create a socket using
> IO::Socket::INET on the client as well as the server or can I proceed
> in the script without creating the IO::Socket:INET?
Read the docs. ->tcpip returns (something equivalent to) a filehandle,
that you use as you would use a socket: that is, you don't need to
create a separate socket. This is one advantage over using an external
ssh client: if you were, you *would* need to create a separate socket
connection to the local end of the tunnel.
Ben
------------------------------
Date: Wed, 6 Feb 2008 13:28:49 -0800 (PST)
From: OMouse <omouse@gmail.com>
Subject: Re: CPerl mode problem: syntax highlighting stumbles over my $old_z=3; my $old_y=4;
Message-Id: <ff447d8e-9b8f-4f90-b170-acb1fca1f12e@e6g2000prf.googlegroups.com>
On Feb 5, 7:27 pm, shul <meshu...@gmail.com> wrote:
> Hi I am using
> emacs GNU Emacs 21.4.1, with CPerl mode enabled.
> on debian sid.
>
Try upgrading to GNU Emacs 22.x or at least try to grab the latest
version of CPerl mode.
Could you please paste some more lines of code as an example? I tried
the ones shown and I had no problem with it.
-Rudolf
------------------------------
Date: Wed, 6 Feb 2008 13:54:13 -0800 (PST)
From: alx__21@hotmail.com
Subject: Re: eval EXPR with maximum execution time?
Message-Id: <6469cffc-18ca-42fd-b8a2-51515b71e1a7@u10g2000prn.googlegroups.com>
Thank you!
As I'm not doing any IO inside the eval (just a simple return at the
end), I think that alarm will work well.
------------------------------
Date: Wed, 6 Feb 2008 21:48:12 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: eval EXPR with maximum execution time?
Message-Id: <shmp75-m2f1.ln1@osiris.mauzo.dyndns.org>
Quoth Joost Diepenmaat <joost@zeekat.nl>:
> Joost Diepenmaat <joost@zeekat.nl> writes:
> > See perldoc -f alarm, but note that your code may still block on IO, as
> > far as I know, if your perl was build with "safe signals" - the default
> > for a few years now.
Using POSIX::SigAction it is possible to install an unsafe handler for
SIGALARM, which will allow you to break out of IO. Of course, all the
traditional caveats about unsafe signals then apply: it would be best to
do nothing more in the sighandler than die to break out of the eval.
> For a possibly more robust (or at least, more certain way of stopping
> processing) you could fork() the offending process and kill() it from
> the parent if it doesn't stop after a sleep().
>
> If you want to get any useful data back from the fork()ed child process,
> you need some kind of IPC. See the perlopen and perlipc man pages.
If you make the child the watchdog instead, there's no need for this. It
also makes it easier to clean up after a successful completion. Of
course, it makes it slightly *harder* to clean up after an unsuccessful
completion: you're pretty much back to signalling the parent, catching
that signal unsafely, and breaking out of the eval. See above :). If
you're not using alarm for anything else (on many systems, sleep uses
alarm internally) it is probably the best solution.
Ben
------------------------------
Date: 06 Feb 2008 22:59:00 GMT
From: xhoster@gmail.com
Subject: Re: eval EXPR with maximum execution time?
Message-Id: <20080206175902.783$fQ@newsreader.com>
Joost Diepenmaat <joost@zeekat.nl> wrote:
> alx__21@hotmail.com writes:
>
> > Hi,
> >
> > I'm looking for something that is like eval in that it can compile and
> > run code from strings, but will quit after a maximum given time.
> >
> > The benchmark module tool "timethis" is very close, but it can only
> > ever specify a minimum time to iterate the given code. What I want is
> > code that iterates ONCE, breaking at a maximum time that I can specify
> > (if it runs that long).
> >
> > If anybody knows of a function to do this I would appreciate it. I
> > would rather not have to start digging around in the source for
> > timethis to create my own.
> >
> > Thanks.
>
> See perldoc -f alarm, but note that your code may still block on IO, as
> far as I know, if your perl was build with "safe signals" - the default
> for a few years now.
I don't think that that would be a problem for IO. OS-level IO calls
will get interrupted, and perl will then either not resume them (having the
Perl-level call set $! and return failure) or perl will execute the sig
handler and then try to resume the call. In either case, a sig handler
that dies should break out of the eval promptly. It is things
that spent a long time at the perl level (as opposed to the system level,
or the Perl level), like sorts and large regex, that have the problem with
safe signals.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Wed, 6 Feb 2008 14:16:50 -0800 (PST)
From: jsahiwal@gmail.com
Subject: IPC Message Queue Help
Message-Id: <5964ccd3-4ef2-42a5-af94-b1329306fc6b@v46g2000hsv.googlegroups.com>
Perl Guru's
I am not able to find the right options for my message queue. I would
like for the queue to receive messages as soon as they are sent, but
am receiving them after the queue is removed. Seems like a blocking
issue but I am not sure. Posting the two small script I am stuck with.
Would appreciate constructive response.
Sending the message as the incoming argument.
use IPC::Msg;
use IPC::SysV;
$outgoing=$ARGV[0];
$key = 999;
$queue = msgget($key,0) or die $!;
my $type = 1234;
#$queue = new IPC::Msg($key,0);
$string = "This";
#$message = pack(A4,"This");
$message = pack("A*",$outgoing);
print $message;
msgsnd($queue,$message,0);
Receiving Part
#!/usr/bin/perl
use IPC::SysV qw(IPC_PRIVATE S_IRWXU IPC_CREAT);
use IPC::Msg;
my $key = 999;
my $queue = msgget($key,&IPC_CREAT | 0777) or die $!;
my ($buffer,$type);
#for ($i=0; $i < 2; $i++) {
if (defined $queue) {
msgrcv($queue,$buffer,60,0,0);
($type,$string)=unpack("a*",$buffer);
print $buffer ;
}
msgctl($queue,IPC_RMID,0);
I know the queue is made by using ipcs -q .
Thanks
Jack
------------------------------
Date: Wed, 6 Feb 2008 22:14:21 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: moving binary data from one RDBMS to Other
Message-Id: <pan.2008.02.06.21.14.21@rtij.nl.invlalid>
On Wed, 06 Feb 2008 11:44:16 -0800, Perl Lover wrote:
> In article <pan.2008.02.06.19.39.39@rtij.nl.invlalid>, Martijn Lievaart
> says...
>
>>I bet you are using windows and are seeing LF -> CRLF conversion. Try
>>opening your files as binary files. See perldoc -f open and perldoc -f
>>binmode.
>
> No. The files are created on Solaris and are used locally.
In that case, try to post a small but complete example that exhibits the
problem.
M4
------------------------------
Date: Wed, 06 Feb 2008 23:24:02 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: PDF API for password encryption?
Message-Id: <4nckq31i0vc0abm4rsr3bgpakec57dun53@4ax.com>
On Tue, 5 Feb 2008 09:20:51 -0800 (PST), Finster <finster@gmail.com>
wrote:
>There is CAM::PDF, unfortunately this module has the mind-numbing
>security hole that the owner password and user password must be
>identical in order to correctly output an encrypted PDF. So, someone
>with the user password could completely remove the encryption just by
>loading up the PDF in Adobe Acrobat.
>
>If there is a secure implementation that allows the the passwords to
>be different, I'd sure like to know about it.
You may try the pdfcrypt utility,
<http://www.sanface.com/pdfcrypt.html>, which claims to be written in
"PERL" (this is not a good sign) and although it is not free software,
I suppose it is not prohibited to look at its source. It simply says:
"You can't modify directly it."
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Wed, 6 Feb 2008 14:59:33 -0800 (PST)
From: clearguy02@yahoo.com
Subject: To modify a file from the command line
Message-Id: <ba11df1a-ebd1-4a88-b274-c8ad94f7a486@v4g2000hsf.googlegroups.com>
Hi all,
I have this file, test.txt and I want to replace a string "bsmith"
with "brsmith" in that file from a command line itself (windows xp).
how can i do it?
Thanks
J
------------------------------
Date: Wed, 6 Feb 2008 15:22:00 -0800 (PST)
From: clearguy02@yahoo.com
Subject: Re: To modify a file from the command line
Message-Id: <c91ad795-199a-4191-9b30-cae30bd06e77@b2g2000hsg.googlegroups.com>
On Feb 6, 2:59=A0pm, cleargu...@yahoo.com wrote:
> Hi all,
>
> I have this file, test.txt and I want to replace a string "bsmith"
> with "brsmith" in that file from a command line itself (windows xp).
> how can i do it?
>
> Thanks
> J
Sorry.. I forgot to add the line.
c:\perl -pe "s/bsmith/brsmith/g" <test.txt
I meant to ask if there is any other powerful command line one liner?
Thanks,
J
------------------------------
Date: Thu, 07 Feb 2008 00:24:50 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: To modify a file from the command line
Message-Id: <87hcglis71.fsf@zeekat.nl>
clearguy02@yahoo.com writes:
> On Feb 6, 2:59Â pm, cleargu...@yahoo.com wrote:
>> Hi all,
>>
>> I have this file, test.txt and I want to replace a string "bsmith"
>> with "brsmith" in that file from a command line itself (windows xp).
>> how can i do it?
>>
>> Thanks
>> J
>
> Sorry.. I forgot to add the line.
>
> c:\perl -pe "s/bsmith/brsmith/g" <test.txt
>
> I meant to ask if there is any other powerful command line one liner?
Drop the < for one character less.
that's about as good as it gets, without using sed
Joost.
------------------------------
Date: Wed, 6 Feb 2008 16:11:02 -0800 (PST)
From: clearguy02@yahoo.com
Subject: Re: To modify a file from the command line
Message-Id: <c5a5ee36-12d6-49fc-aab6-146b1f4f0690@i12g2000prf.googlegroups.com>
On Feb 6, 3:24=A0pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> cleargu...@yahoo.com writes:
> > On Feb 6, 2:59=A0pm, cleargu...@yahoo.com wrote:
> >> Hi all,
>
> >> I have this file, test.txt and I want to replace a string "bsmith"
> >> with "brsmith" in that file from a command line itself (windows xp).
> >> how can i do it?
>
> >> Thanks
> >> J
>
> > Sorry.. I forgot to add the line.
>
> > c:\perl -pe "s/bsmith/brsmith/g" <test.txt
>
> > I meant to ask if there is any other powerful command line one liner?
>
> Drop the < for one character less.
>
> that's about as good as it gets, without using sed
>
> Joost.- Hide quoted text -
>
> - Show quoted text -
No it is not working. I just want to replace the word in the same
file, test1.txt
c:\perl -pe "s/bsmith/brsmith/g" test.txt
doesn't work.
Even, c:\perl -pe "s/bsmith/brsmith/g" <test.txt >test.txt
not working :(
Thanks
J
------------------------------
Date: Thu, 07 Feb 2008 01:13:56 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: To modify a file from the command line
Message-Id: <87hcglr5bv.fsf@zeekat.nl>
clearguy02@yahoo.com writes:
> On Feb 6, 3:24Â pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> No it is not working. I just want to replace the word in the same
> file, test1.txt
Oh right, I missed that part.
> c:\perl -pe "s/bsmith/brsmith/g" test.txt
Try
perl -i -pe "s/bsmith/brsmith/g" test.txt
See perlrun for the -i switch.
Joost.
------------------------------
Date: Thu, 7 Feb 2008 00:20:29 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: To modify a file from the command line
Message-Id: <dfvp75-ipf1.ln1@osiris.mauzo.dyndns.org>
Quoth Joost Diepenmaat <joost@zeekat.nl>:
> clearguy02@yahoo.com writes:
>
> > On Feb 6, 3:24Â pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> > No it is not working. I just want to replace the word in the same
> > file, test1.txt
>
> Oh right, I missed that part.
>
> > c:\perl -pe "s/bsmith/brsmith/g" test.txt
>
> Try
>
> perl -i -pe "s/bsmith/brsmith/g" test.txt
Or rather,
perl -i~ -pe "s/bsmith/brsmith/g" test.txt
since Win32 doesn't have inodes, so can't do inplace edit without a
backup.
Ben
------------------------------
Date: Wed, 6 Feb 2008 16:48:14 -0800 (PST)
From: clearguy02@yahoo.com
Subject: Re: To modify a file from the command line
Message-Id: <366a3397-5d31-46cf-a273-228f0a6a365b@i72g2000hsd.googlegroups.com>
On Feb 6, 4:20=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Joost Diepenmaat <jo...@zeekat.nl>:
>
> > cleargu...@yahoo.com writes:
>
> > > On Feb 6, 3:24=A0pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> > > No it is not working. I just want to replace the word in the same
> > > file, test1.txt
>
> > Oh right, I missed that part.
>
> > > c:\perl -pe "s/bsmith/brsmith/g" test.txt
>
> > Try
>
> > perl -i -pe "s/bsmith/brsmith/g" test.txt
>
> Or rather,
>
> =A0 =A0 perl -i~ -pe "s/bsmith/brsmith/g" test.txt
>
> since Win32 doesn't have inodes, so can't do inplace edit without a
> backup.
>
> Ben
Thanks Ben..
But I don't want to create another file, text1.txt~...
perl -i~ -pe "s/bsmith/brsmith/g" test.txt
Is there any way to do in-place editing with no backup file creations?
Thanks,
J
------------------------------
Date: Thu, 07 Feb 2008 00:56:30 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: To modify a file from the command line
Message-Id: <x7wsph37pe.fsf@mail.sysarch.com>
>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
BM> Quoth Joost Diepenmaat <joost@zeekat.nl>:
>> clearguy02@yahoo.com writes:
>>
>> > On Feb 6, 3:24Â pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
>> > No it is not working. I just want to replace the word in the same
>> > file, test1.txt
>>
>> Oh right, I missed that part.
>>
>> > c:\perl -pe "s/bsmith/brsmith/g" test.txt
>>
>> Try
>>
>> perl -i -pe "s/bsmith/brsmith/g" test.txt
BM> Or rather,
BM> perl -i~ -pe "s/bsmith/brsmith/g" test.txt
BM> since Win32 doesn't have inodes, so can't do inplace edit without a
BM> backup.
what do inodes have to do with it? perl doesn't do true inplace edits as
it could screw it up with a large file. if you output more than you
input, you could overwrite later input data in the same file. -i always
writes to another file and renames it when the program is done. you can
check this out by looking at the inode numbers. it wouldn't make sense
to not use a new file.
i use the same idea in File::Slurp in write_file with the atomic option
on. it writes the whole file to a temp name and when done calls rename
(which is atomic) so you always have a legit file around.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 6 Feb 2008 17:00:16 -0800 (PST)
From: clearguy02@yahoo.com
Subject: Re: To modify a file from the command line
Message-Id: <bf4bdfc4-6a81-484a-8a69-557e653176cd@f47g2000hsd.googlegroups.com>
On Feb 6, 4:56=A0pm, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "BM" =3D=3D Ben Morrow <b...@morrow.me.uk> writes:
>
> =A0 BM> Quoth Joost Diepenmaat <jo...@zeekat.nl>:
> =A0 >> cleargu...@yahoo.com writes:
> =A0 >>
> =A0 >> > On Feb 6, 3:24=A0pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> =A0 >> > No it is not working. I just want to replace the word in the same=
> =A0 >> > file, test1.txt
> =A0 >>
> =A0 >> Oh right, I missed that part.
> =A0 >>
> =A0 >> > c:\perl -pe "s/bsmith/brsmith/g" test.txt
> =A0 >>
> =A0 >> Try
> =A0 >>
> =A0 >> perl -i -pe "s/bsmith/brsmith/g" test.txt
>
> =A0 BM> Or rather,
>
> =A0 BM> =A0 =A0 perl -i~ -pe "s/bsmith/brsmith/g" test.txt
>
> =A0 BM> since Win32 doesn't have inodes, so can't do inplace edit without =
a
> =A0 BM> backup.
>
> what do inodes have to do with it? perl doesn't do true inplace edits as
> it could screw it up with a large file. if you output more than you
> input, you could overwrite later input data in the same file. -i always
> writes to another file and renames it when the program is done. you can
> check this out by looking at the inode numbers. it wouldn't make sense
> to not use a new file.
>
> i use the same idea in File::Slurp in write_file with the atomic option
> on. it writes the whole file to a temp name and when done calls rename
> (which is atomic) so you always have a legit file around.
>
> uri
>
> --
> Uri Guttman =A0------ =A0u...@stemsystems.com =A0-------- =A0http://www.sy=
sarch.com--
> ----- =A0Perl Architecture, Development, Training, Support, Code Review =
=A0------
> ----------- =A0Search or Offer Perl Jobs =A0-----http://jobs.perl.org=A0--=
-------
> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.com-=
--------
Thanks Uri,
Can you pl. give an example? :)
J
------------------------------
Date: Thu, 7 Feb 2008 01:47:20 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: To modify a file from the command line
Message-Id: <8i4q75-rgg1.ln1@osiris.mauzo.dyndns.org>
Quoth Uri Guttman <uri@stemsystems.com>:
> >>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
>
> BM> perl -i~ -pe "s/bsmith/brsmith/g" test.txt
>
> BM> since Win32 doesn't have inodes, so can't do inplace edit without a
> BM> backup.
>
> what do inodes have to do with it? perl doesn't do true inplace edits as
> it could screw it up with a large file. if you output more than you
> input, you could overwrite later input data in the same file. -i always
> writes to another file and renames it when the program is done. you can
> check this out by looking at the inode numbers. it wouldn't make sense
> to not use a new file.
Under Unix, -i with no backup unlinks the old file (holding it open) and
then creates the new file in its place, so there's no need for another
filename. Under systems that don't have close-behind semantics (because
they don't have an inode layer), this is impossible: under Win32, you
cannot delete a file while it is open. Therefore, under such systems you
have to provide a backup filename so that both input and output files
get to have a name. (In principle perl could work around this by making
up a temporary filename in the same directory to use instead, but at
least under Win32 it doesn't.)
> i use the same idea in File::Slurp in write_file with the atomic option
> on. it writes the whole file to a temp name and when done calls rename
> (which is atomic) so you always have a legit file around.
This is actually somewhat safer than inplace edit, of course, as the
file is replaced atomically. Even with a backup name, -i does things in
the wrong order: first it renames the old file to the backup, then it
creates a new file by the original name for the output, so if the
process is interrupted the file is left corrupted. I guess the perl core
doesn't want to get involved with the issue of safely creating a
temporary filename, and just avoids the issue.
I have often thought Unix could do with flink(2) and mkstemp(2)
syscalls, that create a new link to an open fd and create a new file
with no name respectively. It would save a whole lot of mucking about
with temporary names.
Ben
------------------------------
Date: Thu, 07 Feb 2008 02:08:39 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: To modify a file from the command line
Message-Id: <x7ejbp34d4.fsf@mail.sysarch.com>
>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
BM> Under Unix, -i with no backup unlinks the old file (holding it open) and
BM> then creates the new file in its place, so there's no need for another
BM> filename. Under systems that don't have close-behind semantics (because
BM> they don't have an inode layer), this is impossible: under Win32, you
BM> cannot delete a file while it is open. Therefore, under such systems you
BM> have to provide a backup filename so that both input and output files
BM> get to have a name. (In principle perl could work around this by making
BM> up a temporary filename in the same directory to use instead, but at
BM> least under Win32 it doesn't.)
interesting. i didn't think about that way of doing things.
>> i use the same idea in File::Slurp in write_file with the atomic option
>> on. it writes the whole file to a temp name and when done calls rename
>> (which is atomic) so you always have a legit file around.
BM> This is actually somewhat safer than inplace edit, of course, as the
BM> file is replaced atomically. Even with a backup name, -i does things in
BM> the wrong order: first it renames the old file to the backup, then it
BM> creates a new file by the original name for the output, so if the
BM> process is interrupted the file is left corrupted. I guess the perl core
BM> doesn't want to get involved with the issue of safely creating a
BM> temporary filename, and just avoids the issue.
well, that is why i call that option atomic. the idea is to always have
a proper file around. i have plans for inplace edit functions in
file::slurp (line by line or whole file) and those will call write_file
with the atomic option. then you can do inplace editing from a program
with just a single sub call (passing it a code ref to do the editing).
BM> I have often thought Unix could do with flink(2) and mkstemp(2)
BM> syscalls, that create a new link to an open fd and create a new file
BM> with no name respectively. It would save a whole lot of mucking about
BM> with temporary names.
open can create temp files now (i think it uses file::temp
underneath). in slurp i take the old file name and append the pid to it
for the temp file. maybe i need to make that part more robust but no one
has mentioned it. one reason i don't use the open temp feature is that
slurp is backwards compatible to 5.005.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1261
***************************************