[31662] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2925 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Apr 28 14:09:27 2010

Date: Wed, 28 Apr 2010 11:09:09 -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, 28 Apr 2010     Volume: 11 Number: 2925

Today's topics:
    Re: alternative for ctime <kst-u@mib.org>
    Re: alternative for ctime (Alan Curry)
    Re: checksum calculation for file offsets <smallpond@juno.com>
    Re: checksum calculation for file offsets <ben@morrow.me.uk>
    Re: FAQ 7.26 How can I find out my current or calling p <brian.d.foy@gmail.com>
        Overriding a require'd module's subroutine <rodent@gmail.com>
        use constant behavior <cartercc@gmail.com>
    Re: use constant behavior <uri@StemSystems.com>
    Re: use constant behavior <cartercc@gmail.com>
    Re: use constant behavior <tadmc@seesig.invalid>
    Re: use constant behavior <uri@StemSystems.com>
        XML Replace <trevor.dodds@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 27 Apr 2010 14:58:03 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: alternative for ctime
Message-Id: <lnmxwoblsk.fsf@nuthaus.mib.org>

GRP <rengaprasath@gmail.com> writes:
[...]
> Script will check for pattern in few log files (lets say its looking
> for pattern "Out-of-Memory" in /u1/app/log/WL.log & /u2/app/log/WL.log
> etc.. ).
>
> These WL.log files from different directories will delete & overwrite
> once it reach 10mb. It may happen in a day or in few days.
>
> Since i need a maintain a flat file or Database like below
> <directory-and-file-name>:<err-line-number>:<some-other-constant-value-
> example.filecreation-time>
>
> the reason being maintaining a flat/database file because if the same
> pattern "Out-of-Memory" found in the same line, it should check in the
> flat file & if the entry found it wont alert. Whereas the same pattern
> found in a different line , it will not be available in the flat file
> and alert will trigger.
>
> Since the log files may rotate often i need to tag someother value
> along with filename and linenumber in the flat file which is key
> value. I can't simply store <directory-and-file-name>:<err-line-
> number> in flat file , becos once the file recreated and if the error
> comes in the same line, it wont get alerted , which is wrong. In order
> to overcome i taught of storing file creation time, which is not
> possible.

Do you have any control over what's written to the files?  If so, it
would be easy enough to write a timestamp and/or other information on
the first line.

If not, checking the inode number (stat("filename"))[1] should detect
when a new copy of the file is created.  The combination of device and
inode number (elements 0 and 1 of the result of stat()) should uniquely
identify the file, but just inode number is probably enough for your
purposes.

Complex file system setups can mess this up, with the same file
appearing on different devices, but that's probably not going to be an
issue for you.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


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

Date: Tue, 27 Apr 2010 22:39:00 +0000 (UTC)
From: pacman@kosh.dhis.org (Alan Curry)
Subject: Re: alternative for ctime
Message-Id: <hr7p24$bld$1@speranza.aioe.org>

[the question boiled down to "how do I know if a log file has been rotated
since the last time I looked at it?"]

In article <lnmxwoblsk.fsf@nuthaus.mib.org>,
Keith Thompson  <kst-u@mib.org> wrote:
|
|Do you have any control over what's written to the files?  If so, it
|would be easy enough to write a timestamp and/or other information on
|the first line.

That's the best answer (other than "switch to FreeBSD so you can use
st_birthtime")

|
|If not, checking the inode number (stat("filename"))[1] should detect
|when a new copy of the file is created.  The combination of device and
|inode number (elements 0 and 1 of the result of stat()) should uniquely
|identify the file, but just inode number is probably enough for your
|purposes.

device.inode is unique at a single point in time, but the same combination
can reappear later, after the first file is gone.

Rotated log files can get deleted eventually, and before that they can get
gzipped, which will result in a .gz file with a new inode number and the
original inode number becoming available for reuse.

It wouldn't be surprising to see /var/log/mail get the same inode number it
had 2 weeks ago, when it had the contents now present in /var/log/mail.2.gz

-- 
Alan Curry


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

Date: Tue, 27 Apr 2010 14:10:16 -0400
From: Steve C <smallpond@juno.com>
Subject: Re: checksum calculation for file offsets
Message-Id: <hr79am$eoo$1@news.eternal-september.org>

Ironhide wrote:
> Hi,
> 
> This is what I am trying to achieve.
> 
> Create a copy of a file.
> User inputs starting offset and end offset for the file.
> For this range the perl program should calculate the md5sum
> for both the original file and the copied file and compare them
> both.
> 
> Any idea how to go about dealing with the offsets?


Digest::Perl::MD5 is a pure perl implementation.  You could hack
the addfile method to add optional start and end offset arguments.
It would just add a seek to start, and only read end-start bytes
instead of to end of file.  (Off-by-one error left to the astute).

If you need better performance use the addfile method for
Digest::MD5.  Rather than hack C source and rebuild, it might be
easier to read from STDIN.  In bash this would be:

dd if=myfile ibs=1 skip=$START count=$(( $END - $START )) | mymd5sum.pl


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

Date: Tue, 27 Apr 2010 19:26:40 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: checksum calculation for file offsets
Message-Id: <0smja7-h0t1.ln1@osiris.mauzo.dyndns.org>


Quoth Steve C <smallpond@juno.com>:
> Ironhide wrote:
> > 
> > Create a copy of a file.
> > User inputs starting offset and end offset for the file.
> > For this range the perl program should calculate the md5sum
> > for both the original file and the copied file and compare them
> > both.
> > 
> > Any idea how to go about dealing with the offsets?
> 
> Digest::Perl::MD5 is a pure perl implementation.  You could hack
> the addfile method to add optional start and end offset arguments.
> It would just add a seek to start, and only read end-start bytes
> instead of to end of file.  (Off-by-one error left to the astute).
> 
> If you need better performance use the addfile method for
> Digest::MD5.  Rather than hack C source and rebuild, it might be
> easier to read from STDIN.  In bash this would be:
> 
> dd if=myfile ibs=1 skip=$START count=$(( $END - $START )) | mymd5sum.pl

That seems excessively complicated. If you *really* can't afford to read
the appropriate section of the file into memory, try PerlIO::subfile.

Ben



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

Date: Tue, 27 Apr 2010 16:07:52 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 7.26 How can I find out my current or calling package?
Message-Id: <270420101607526829%brian.d.foy@gmail.com>

In article <braha7-clj1.ln1@osiris.mauzo.dyndns.org>, Ben Morrow
<ben@morrow.me.uk> wrote:


> I'm not being clear, I think. What I want to know is
> 
>     Under what circumstances is it *correct* for $obj->DOES("Foo") to
>     return true but *incorrect* for $obj->isa("Foo") to return true?

It's incorrect for isa() to return true if the module does not inherit
from the thing you give it, although DOES() can return true when it
does the role without inheriting from it. 

I understand your question, but I don't think you're seeing the
difference between inheritance and roles.


> If ->isa is restricted to only checking @ISA for inheritance, then why
> is it overridable? 

Because Perl is a dynamic language and that's only a normal method. If
something wants to claim to inherit from something for whatever reason,
well, Perl lets you do that. I'm not saying it's a good idea, just that
it is what it is.


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

Date: Tue, 27 Apr 2010 13:47:36 -0700 (PDT)
From: Ratty <rodent@gmail.com>
Subject: Overriding a require'd module's subroutine
Message-Id: <0764e288-b8b7-46e9-97ce-fd5dc726f036@11g2000prw.googlegroups.com>

I'm using the MARC::Batch module. It refuses to process records with
character encoding issues. It dies with a warning about line 166 in
Encode.pm. I can use eval to make it skip bad records instead but I
don't want that either. I want it to do the best it can. What I need
to do is modify Encode::decode so it does not die when it can't decode
a string. This works if I add an eval to Encode.pm sitting in my perl/
lib directory. But I don't want to do it this way. I want everything
restricted to my one calling program. I seem to remember doing
something similar years ago by simply copying the subroutine, with
package name, into my program and my program would use that instead.
But it doesn't work for me this time. No errors, it simply ignores it.
Perhaps because I'm not calling the module directly this time, but
rather it is being called somewhere in the bowels of MARC::Record,
which I'm also not calling directly.

use MARC::Batch;

## Programming here

## Attempt to override
sub Encode::decode($$;$)
{
    my ($name,$octets,$check) = @_;
    return undef unless defined $octets;
    $octets .= '' if ref $octets;
    $check ||=0;
    my $enc = find_encoding($name);
    unless(defined $enc){
	require Carp;
	Carp::croak("Unknown encoding '$name'");
    }
    ## Add eval heres
    my $string;
    eval { $string = $enc->decode($octets,$check); };
    $_[1] = $octets if $check and !($check & LEAVE_SRC());
    return $string;
}

What's the most elegant way to redefine somebody else's subroutine?
BTW, I also tried:

{
local *Encode::decode = \&myDecode;
}

Doesn't work either


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

Date: Tue, 27 Apr 2010 14:28:33 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: use constant behavior
Message-Id: <a45f2c8f-3634-4161-8759-1609cc8d2139@v18g2000vbc.googlegroups.com>

use constant DIR => 'log_files';
mkdir DIR unless -e DIR;

This places a subdirectory in my current working directory named
log_files, creating it if it doesn't exist.

print_log();
sub print_log
{
  open LOG, '>', "DIR/log.csv" or die "Cannot open LOG, $!";
  ...
}

This throws a DOES NOT EXIST error. However,

print_log(DIR);
sub print_log
{
  my $dir = shift;
  open LOG, '>', "$dir/log.csv" or die "Cannot open LOG, $!";
  ...
}

works just fine. What am I missing?

Thanks, CC.


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

Date: Tue, 27 Apr 2010 17:35:02 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: use constant behavior
Message-Id: <87mxwoo9yx.fsf@quad.sysarch.com>

>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:

  c> use constant DIR => 'log_files';
  c> mkdir DIR unless -e DIR;

  c> This places a subdirectory in my current working directory named
  c> log_files, creating it if it doesn't exist.

  c> print_log();
  c> sub print_log
  c> {
  c>   open LOG, '>', "DIR/log.csv" or die "Cannot open LOG, $!";
  c>   ...
  c> }

  c> This throws a DOES NOT EXIST error. However,

perl constants are really subs with a prototype of no arguments. they
are converted at compile to their value (and constant folded if
possible).

"DIR" won't work because subs don't directly interpolate. you can work
around that with DIR . '/log.csv' or the @{[DIR]} interpolation trick.

another way is to use the ReadOnly module which declares regular
variables which can't be modified. then you can use $DIR in either situation.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Wed, 28 Apr 2010 04:40:05 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: use constant behavior
Message-Id: <becf91e9-c12e-4b13-9a2a-5723dab7cbfa@s9g2000yqa.googlegroups.com>

On Apr 27, 5:35=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> perl constants are really subs with a prototype of no arguments. they
> are converted at compile to their value (and constant folded if
> possible).

Uri, thanks for your explanation.

I would then assume that
 - use constant DIR =3D> 'log_files';
would be more or less equivalent to
 - sub DIR { return 'log_files'; }

Is this assumption correct, more or less?

Thanks, CC.


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

Date: Wed, 28 Apr 2010 07:16:04 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: use constant behavior
Message-Id: <slrnhtg9g0.kpg.tadmc@tadbox.sbcglobal.net>

ccc31807 <cartercc@gmail.com> wrote:

> use constant DIR => 'log_files';

>   open LOG, '>', "DIR/log.csv" or die "Cannot open LOG, $!";


    perldoc constant

        Constants defined using this module cannot be interpolated 
        into strings like variables.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Wed, 28 Apr 2010 13:09:56 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: use constant behavior
Message-Id: <87k4rrjyfv.fsf@quad.sysarch.com>

>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:

  c> On Apr 27, 5:35 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
  >> perl constants are really subs with a prototype of no arguments. they
  >> are converted at compile to their value (and constant folded if
  >> possible).

  c> Uri, thanks for your explanation.

  c> I would then assume that
  c>  - use constant DIR => 'log_files';
  c> would be more or less equivalent to
  c>  - sub DIR { return 'log_files'; }

sub DIR() { 'log_files' }

the important difference is the prototype of () so it can be compile
time converted to a constant. the return isn't needed (i dunno if it
affects it becoming a proper constant but i doubt it).

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Wed, 28 Apr 2010 10:01:37 -0700 (PDT)
From: Trev <trevor.dodds@gmail.com>
Subject: XML Replace
Message-Id: <7294eca6-e350-445f-abd4-aa71132cf31d@r6g2000vbh.googlegroups.com>

I'm trying to use Perl to replace a line in a few XML files I have.

Example XML below, I'm wanting to change the Id= part from  Id="/Local/
App/App1" to Id=/App1". I know there's an easy way to do this with
perl alone however I'm trying to use XML::Simple or any XML plugin for
perl.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<Profile xmlns="xxxxxxxxx" name="" version="1.1" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance">


	<Application Name="App1" Id="/Local/App/App1" Services="1" policy=""
StartApp="" Bal="5" sessInt="500" WaterMark="1.0"/>


<AppProfileGuid>586e3456dt</AppProfileGuid>

</Profile>


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

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 2925
***************************************


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