[25887] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8115 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 25 14:05:24 2005

Date: Wed, 25 May 2005 11:05:06 -0700 (PDT)
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, 25 May 2005     Volume: 10 Number: 8115

Today's topics:
        Detecting a program is running. <root@localhost.com>
    Re: Detecting a program is running. <phaylon@dunkelheit.at>
        Ignorant question regarding includes <mddibern@NOSPAM.student.cs.uwaterloo.ca>
    Re: Ignorant question regarding includes (Greg Bacon)
    Re: Ignorant question regarding includes <mddibern@NOSPAM.student.cs.uwaterloo.ca>
    Re: Ignorant question regarding includes (Greg Bacon)
    Re: Need help with proper file locking of flatfile data xhoster@gmail.com
    Re: simplest method for renaming files / Windows ? <geoff.cox@notquitecorrectfreeuk.com>
        sql and perl <alexj@freesurf.ch>
    Re: sql and perl <1usa@llenroc.ude.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 26 May 2005 00:58:34 +0800
From: Dave <root@localhost.com>
Subject: Detecting a program is running.
Message-Id: <d72chi$mj9$1@news.hgc.com.hk>

Hi,

How can I use perl to detect a program is running?
This is typically used when program A running, A do not want to running 
multiple copies of itself.

Thanks
Sam


------------------------------

Date: Wed, 25 May 2005 19:18:11 +0200
From: phaylon <phaylon@dunkelheit.at>
Subject: Re: Detecting a program is running.
Message-Id: <pan.2005.05.25.17.18.10.643903@dunkelheit.at>

Dave wrote:

> How can I use perl to detect a program is running? This is typically used
> when program A running, A do not want to running multiple copies of
> itself.

How about a system-wide lock- and pid-file? The latter to find out if the
process is still running. Another -but not that save- approach might be to
work with $0 and the process table, but I think I would prefer the first.
I use the second method mostly only for short scripts on my own servers
so those can tell me «Another one's still running, I'm waiting.»

hth a bit,p

-- 
http://www.dunkelheit.at/
codito, ergo sum.



------------------------------

Date: Wed, 25 May 2005 11:55:26 -0400
From: Debo <mddibern@NOSPAM.student.cs.uwaterloo.ca>
Subject: Ignorant question regarding includes
Message-Id: <Pine.GSO.4.58.0505251150190.26743@rees.math.uwaterloo.ca>

Hello there,

I'd like to set up my application so that a whole bunch of global
'constants' are available merely by including the file somehow.

The gist of what I'm trying to do is something like this:


***In constants.pl:

my $constant1 = 'some value';
my $constant2 = 'some other value';

***In somefile.pl:

require constants.pl;

if ('hello' eq $constant1)
{
	....
}

This sort of thing is done is C/C++ all the time by including header files
or something hackish like that... that's along the lines of what I'm
trying to do.

I use strict everywhere, so scope will be an issue I imagine. Also, I
don't like to use the constants package because I tend to use these
'constants' in between double/interpolated quotes all the time.

Any suggestions? I'm open to anything that will allow me to store all of
my constant filepaths etc. in a single file. I'm sorry if the way to do
this is obvious in the documentation somewhere, I just haven't been able
to figure it out.

Thanks,

-Debo




------------------------------

Date: Wed, 25 May 2005 16:29:11 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Ignorant question regarding includes
Message-Id: <11999un3avgoaec@corp.supernews.com>

In article <Pine.GSO.4.58.0505251150190.26743@rees.math.uwaterloo.ca>,
    Debo  <mddibern@NOSPAM.student.cs.uwaterloo.ca> wrote:

: I'd like to set up my application so that a whole bunch of global
: 'constants' are available merely by including the file somehow.
:
: [...]
:
: I use strict everywhere, so scope will be an issue I imagine. Also, I
: don't like to use the constants package because I tend to use these
: 'constants' in between double/interpolated quotes all the time.
:
: Any suggestions? I'm open to anything that will allow me to store all of
: my constant filepaths etc. in a single file. I'm sorry if the way to do
: this is obvious in the documentation somewhere, I just haven't been able
: to figure it out.

How about putting your constants in a module?

    $ cat Constants.pm
    package Constants;

    use strict;

    *Constants::constant1 = \'some value';
    *Constants::constant2 = \'some other value';

    1;

    $ cat try
    #! /usr/local/bin/perl

    use warnings;
    use strict;

    use Constants;

    print "\$Constants::constant1 = $Constants::constant1\n",
          "\$Constants::constant2 = $Constants::constant2\n";

    $Constants::constant1 = 42;

    $ ./try
    $Constants::constant1 = some value
    $Constants::constant2 = some other value
    Modification of a read-only value attempted at ./try line 11.

Note that the trick of creating a constant by assigning a reference to
a typeglob (e.g., C<*Constants::constant1 = \'some value';>) is
documented in the perlmod manpage.  Search for PI.

Take this advice with a grain of salt: it may or may not be the best
approach, but that's hard to say because your post focused on the
mechanism rather than the problem you're trying to solve.

Using Perl as though it were a merely a nicer C++ isn't dangerous,
but it's likely to cause you to do a lot more work than necessary and
to miss opportunities to learn new and interesting techniques.

Hope this helps,
Greg
-- 
In Tyler, Texas, there used to be the most accurately named used car
dealership in American history: Caveat Emptor Motors. "Let the buyer
beware." It was a clever name, but the company went out of business.
I guess the buyers bewore too much.    -- Gary North


------------------------------

Date: Wed, 25 May 2005 12:59:14 -0400
From: Debo <mddibern@NOSPAM.student.cs.uwaterloo.ca>
Subject: Re: Ignorant question regarding includes
Message-Id: <Pine.GSO.4.58.0505251255280.26743@rees.math.uwaterloo.ca>

GB> Take this advice with a grain of salt: it may or may not be the best
GB> approach, but that's hard to say because your post focused on the
GB> mechanism rather than the problem you're trying to solve.
GB>
GB> Using Perl as though it were a merely a nicer C++ isn't dangerous,
GB> but it's likely to cause you to do a lot more work than necessary and
GB> to miss opportunities to learn new and interesting techniques.

Well, the nature of my problem is fairly simple. I'm writing a series of
cgi scripts and they all need to know about certain filepaths on the
server. I just wanted a place to dump a bunch of constants describing
these filepaths so that I wouldn't have to change a dozen scripts if one
of the paths changed.

You're right that my post concentrated on the mechanism, but that's mostly
because said mechanism is the way I'm accustomed to hacking things :)
(Your method works very well by the way; thanks for that!) However, if
there is a better/cleaner/zanier way to maintain global constants across
multiple files, I'm certainly open to hearing them. I didn't think that
there would be that many ways to do such a thing...

Thanks again!

-Debo


------------------------------

Date: Wed, 25 May 2005 17:47:43 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Ignorant question regarding includes
Message-Id: <1199ehvefubngf0@corp.supernews.com>

In article <Pine.GSO.4.58.0505251255280.26743@rees.math.uwaterloo.ca>,
    Debo  <mddibern@NOSPAM.student.cs.uwaterloo.ca> wrote:

: Well, the nature of my problem is fairly simple. I'm writing a series
: of cgi scripts and they all need to know about certain filepaths on
: the server. I just wanted a place to dump a bunch of constants
: describing these filepaths so that I wouldn't have to change a dozen
: scripts if one of the paths changed.
:
: You're right that my post concentrated on the mechanism, but that's
: mostly because said mechanism is the way I'm accustomed to hacking
: things :) (Your method works very well by the way; thanks for that!)
: However, if there is a better/cleaner/zanier way to maintain global
: constants across multiple files, I'm certainly open to hearing them. I
: didn't think that there would be that many ways to do such a thing...

I'd say what you're doing is reasonable.  It sounds like you're making
sound application of the "Keep It Simple, Stupid" rule. :-)

Greg
-- 
Where liberty dwells, there is my country.
    -- Ben Franklin


------------------------------

Date: 25 May 2005 17:57:37 GMT
From: xhoster@gmail.com
Subject: Re: Need help with proper file locking of flatfile data
Message-Id: <20050525135737.599$lo@newsreader.com>

"Randy" <rbutcher.nospam@hotmail.com> wrote:
> Hello,
>
> I seem to be having some difficulties locking a simple counter flatfile
> database. To start off, here is the code I am presently using:
>
> #!/usr/bin/perl
> use Fcntl qw(:DEFAULT :flock);
>
> open (LOCKERFILE, "<$activeuser/locker.txt") or die "Can't open: $!";
> flock (LOCKERFILE, LOCK_EX) or die "Can't lock: $!";
>
> open (GETCOUNT, "<$activeuser/counter.txt") or die "Can't open file: $!";
>   flock (GETCOUNT, LOCK_SH) or die "Can't lock: $!";

useless

>   $currentcount=<GETCOUNT>;
>   ($date,$count,$users)=split(/\|/,$currentcount);
> close(GETCOUNT);
>
> $count = $count + 1;
>
> open (WRITECOUNT, ">$activeuser/counter.txt") or die "Can't open file:
> $!";
>   flock (WRITECOUNT, LOCK_SH) or die "Can't lock: $!";

useless

>   print WRITECOUNT "$date|$count|$users";
> close(WRITECOUNT);

or die $!

>
> close(LOCKERFILE);

or die $!

> exit;

There is no point in flocking the counter.txt file, because access to it
is already controlled by the locker file.  And if it weren't for that, it
would still be useless because you get shared rather than exlcusive locks.
And even if it werent' for that it would still be useless because there
is a race condition.

>
> I use the locker.txt file to start a primary lock before the reading and
> writing begins, suggested to me by a user from this newsgroup long ago.
> The script seems to work most of the time, but every now and then I check
> the data and its completely gone ... only leaving the data separators | |
> | So I guess it doesnt really work well after all.
>
> I would be interested in knowing if this script is infact flawed in my
> approach or if the data loss is being caused by something else.

At a first glance, I don't see anything in the code that would plausibly
cause this problem without dying or triggering a warning.  (If your script
was dying or triggering a warning sometimes, would you know?)

Maybe there is some rogue program that is accessing counter.txt without
going through the locking process.  (Change the names of both files, and
see if the problem goes away).  Or maybe your OS/FS has silently broken
locks.

> I have
> really done lots of reading on locking flatfile databases and there seems
> to be alot of opinions, but few hard facts on how to do it correctly.
>
> This script performs a very simple function but is very important to me
> to make it work right without losing my data. I would very much value
> feedback on this. Many thanks in advance.


Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: Wed, 25 May 2005 15:39:54 GMT
From: Geoff Cox <geoff.cox@notquitecorrectfreeuk.com>
Subject: Re: simplest method for renaming files / Windows ?
Message-Id: <mo69911rhqtmagcool96tcjrsksrnqi9vq@4ax.com>

On Wed, 25 May 2005 12:17:56 GMT, "Jürgen Exner"
<jurgenex@hotmail.com> wrote:

>Geoff Cox wrote:
>> At the moment I
>> 1. use Find::File
>> 2. open each file and print OUT each line to  files with a new name
>> 3. delete the original files
>> 4. rename the new files to the original ones with another Perl script
>
>If you don't mind asking me, but what is the purpose of this excercise, i.e. 
>what are you trying to achive with steps 1-4 above?

!! I did not make it very clear - I have a set of files which are them
acted on by a program and changed so I have been giving them a
different name so as not to overwrite the originals ( I have been
opening the files, making changes and then writing them back with an
OUT command. For a separate reason I need the files to have the
original name fro the next step so have been changing them back to the
original name !!

anyway all is better now as I have realised that

rename ("d://perl/progs/test/file1.txt","file2.txt") ;

does work with Windows so am now using this approach.

Thanks

Cheers

Geoff







>For all practical purposes you got an identical set of files, just some file 
>attributes (created, last modified, maybe owner, group, access right) would 
>be different and there are better ways to change those than to copy each and 
>every file and rename it back to the original name.
>
>> I know there are better ways - but am confused as to which to use.
>> Which is the simplest?
>
>Depends on what you actually want to achive. Your steps 1-4 are almost a 
>NOP.
>
>jue
>



------------------------------

Date: Wed, 25 May 2005 19:48:46 +0200
From: Alexandre Jaquet <alexj@freesurf.ch>
Subject: sql and perl
Message-Id: <4294ba7f$0$1163$5402220f@news.sunrise.ch>

I'm having trouble with a sql query on mysql with perl,

I would like to show on a web page all items who have a different name 
(nom in french) but mysql doesn't work correctly (as I know postgresql 
and other database)

SELECT DISTINCT nom, id_vinyl,auteur,label,prix, pochette, date_stock 
FROM view_nouveaute WHERE id_vinyl = id_vinyl

return me

nom1 	1 	auteur1 	label1 	12 	pochette/0.jpg 	0000-00-00
nom1 	2 	auteur1 	label1 	12 	pochette/1.jpg 	0000-00-00
nom1 	15 	auteur1 	label1 	12 	pochette/14.jpg 	0000-00-00
nom1 	16 	auteur1 	label1 	12 	pochette/15.jpg 	0000-00-00
nom1 	17 	auteur1 	label1 	12 	pochette/16.jpg 	0000-00-00
nom1 	18 	auteur1 	label1 	12 	pochette/17.jpg 	0000-00-00
nom1 	19 	auteur1 	label1 	12 	pochette/18.jpg 	0000-00-00
nom1 	20 	auteur1 	label1 	12 	pochette/19.jpg 	0000-00-00
nom2 	21 	auteur2 	label2 	12 	pochette/20.jpg 	0000-00-00
nom2 	22 	auteur2 	label2 	12 	pochette/21.jpg 	0000-00-00
nom2 	23 	auteur2 	label2 	12 	pochette/22.jpg 	0000-00-00
nom2 	24 	auteur2 	label2 	12 	pochette/23.jpg 	nom2 	29 	auteur2 
label2 	12 	pochette/28.jpg 	0000-00-00
nom2 	30 	auteur2 	label2 	12 	pochette/29.jpg 	0000-00-00

It doesn't take result who got ONLY different name.

Any idea to solve it ?

Thanks in advance.

Alexandre Jaquet


------------------------------

Date: Wed, 25 May 2005 17:58:37 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: sql and perl
Message-Id: <Xns96618DFC5EF38asu1cornelledu@127.0.0.1>

Alexandre Jaquet <alexj@freesurf.ch> wrote in
news:4294ba7f$0$1163$5402220f@news.sunrise.ch: 

> I'm having trouble with a sql query on mysql with perl,

You posted no Perl code.

> I would like to show on a web page all items who have a different name
> (nom in french) but mysql doesn't work correctly (as I know postgresql
> and other database)

How is that a Perl issue?
 
> SELECT DISTINCT nom, id_vinyl,auteur,label,prix, pochette, date_stock 
> FROM view_nouveaute WHERE id_vinyl = id_vinyl

That is not Perl.

> return me

This could be Perl.

 
> nom1      1      auteur1      label1      12      pochette/0.jpg     
> 0000-00-00 nom1      2      auteur1      label1      12     
> pochette/1.jpg      0000-00-00 nom1      15      auteur1      label1  

This is definitely not Perl.

> Any idea to solve it ?

No.

> Thanks in advance.

"Thanks in advance" is one of the rudest, bossiest statements of all 
time. As such, it is no wonder that it gets used frequently by people 
who do not appreciate the value of others' time.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


------------------------------

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 V10 Issue 8115
***************************************


home help back first fref pref prev next nref lref last post