[33005] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4281 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 11 18:09:13 2014

Date: Thu, 11 Sep 2014 15:09:08 -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           Thu, 11 Sep 2014     Volume: 11 Number: 4281

Today's topics:
        How to avoid message "A thread exited while 2 threads w <mikaelpetterson@hotmail.com>
    Re: How to avoid message "A thread exited while 2 threa <news@todbe.com>
    Re: How to avoid message "A thread exited while 2 threa <gravitalsun@hotmail.foo>
    Re: How to avoid message "A thread exited while 2 threa <rweikusat@mobileactivedefense.com>
        How to remove lines with seen prefix <sun_tong_001@users.sourceforge.net>
    Re: How to remove lines with seen prefix <gamo@telecable.es>
    Re: How to remove lines with seen prefix <gamo@telecable.es>
    Re: How to remove lines with seen prefix <ben.usenet@bsb.me.uk>
    Re: How to remove lines with seen prefix <sun_tong_001@users.sourceforge.net>
    Re: How to remove lines with seen prefix <news@lawshouse.org>
    Re: How to remove lines with seen prefix <whynot@pozharski.name>
    Re: How to remove lines with seen prefix <gamo@telecable.es>
    Re: How to remove lines with seen prefix <rweikusat@mobileactivedefense.com>
    Re: How to remove lines with seen prefix (Seymour J.)
    Re: How to remove lines with seen prefix <rweikusat@mobileactivedefense.com>
        ORM considered a driver of hardware sales (was: perl6 t <rweikusat@mobileactivedefense.com>
    Re: ORM considered a driver of hardware sales <bauhaus@futureapps.invalid>
    Re: perl6 too much pointless functionality <zeeb@zeeb.zeeb>
    Re: porting bash curl command into perl <news@lawshouse.org>
        utility perl module herbert.burnswell@gmail.com
    Re: utility perl module <gamo@telecable.es>
    Re: utility perl module <lesley.binks@gmail.com>
    Re: utility perl module <peter@makholm.net>
    Re: utility perl module <rweikusat@mobileactivedefense.com>
    Re: utility perl module <brian.helterline@hp.com>
    Re: utility perl module herbert.burnswell@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 2 Sep 2014 07:00:42 -0700 (PDT)
From: mike <mikaelpetterson@hotmail.com>
Subject: How to avoid message "A thread exited while 2 threads were running."
Message-Id: <6f59d0c0-61ed-450f-8daa-6e4177d5409e@googlegroups.com>

Hi,

I need to test if I can login to localhost using ssh. So I wrote the following program ( I am stuck to perl 5.8.4):


#!/usr/bin/perl 

use strict;
use warnings;
use threads;
no warnings;

my $verbose = 1;		#Debug on != 0
my $debug = "[DEBUG]:";









local $| = 1;
my $thr1 = threads->new(\&timer);
my $thr2 = threads->new (\&sshCheck);

sub timer{
	my $cnt = 0;
	while(1){
		if($cnt	> 59){
			print "terminate, not able to login!\n";
			$thr2->detach();
			last;
		}
		sleep(1);
		$cnt++;
	}
}



#ssh public key check.
sub sshCheck{

open COMMANDHANDLE, "ssh  127.0.0.1 uname 2>&1 | ";
	while ( my $res = <COMMANDHANDLE> ){
            chomp $res;
			local $| = 1;
			 $verbose && print "Ssh output: " . $res . "\n";
            if ( $res =~ /Are you sure you want to continue connecting/ )
            {
              print COMMANDHANDLE "yes\n";
            }
			elsif ( $res =~ /Enter Windows password/ )
            {
              die "Failed\n";
            }elsif ( $res =~ /Linux/ )
            {
              print "Public key authentication enabled!\n";
			  $thr1->detach();
			  last;
            }
			 

    }
close COMMANDHANDLE;
}


$thr2->join;
print "Done .... Goodbye.\n";

How can I avoid the message:

"A thread exited while 2 threads were running.

//mike



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

Date: Tue, 02 Sep 2014 14:29:04 -0700
From: "$Bill" <news@todbe.com>
Subject: Re: How to avoid message "A thread exited while 2 threads were running."
Message-Id: <540636A0.8040707@todbe.com>

On 9/2/2014 07:00, mike wrote:
> Hi,
>
> I need to test if I can login to localhost using ssh. So I wrote the following program ( I am stuck to perl 5.8.4):

 ...

> How can I avoid the message:
>
> "A thread exited while 2 threads were running.

I think that's an artifact of 5.8.

If it's going from the program to STDERR, you could try redirecting STDERR
to a file or memory vrbl (IO::Capture::Stderr) to get rid of it.



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

Date: Wed, 03 Sep 2014 19:13:19 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: How to avoid message "A thread exited while 2 threads were running."
Message-Id: <lu7en5$18kl$1@news.ntua.gr>

On 2/9/2014 5:00 μμ, mike wrote:
> Hi,
>
> I need to test if I can login to localhost using ssh. So I wrote the following program ( I am stuck to perl 5.8.4):
> How can I avoid the message:
>
> "A thread exited while 2 threads were running.
>
> //mike
>




Perl can not catch the ssh prompts you want, because ssh spawns a new 
terminal for them,  so your approach will never work.
Better to use a ssh module from cpan


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

Date: Wed, 03 Sep 2014 19:51:31 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How to avoid message "A thread exited while 2 threads were running."
Message-Id: <8738c835cc.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> On 2/9/2014 5:00 ¦Ì¦Ì, mike wrote:
>> Hi,
>>
>> I need to test if I can login to localhost using ssh. So I wrote the following program ( I am stuck to perl 5.8.4):
>> How can I avoid the message:
>>
>> "A thread exited while 2 threads were running.
>>
>> //mike
>
>
> Perl can not catch the ssh prompts you want, because ssh spawns a new
> terminal for them,  so your approach will never work.

It might be possible to parse the output of ssh -v instead.


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

Date: Sat, 06 Sep 2014 02:05:13 GMT
From: * Tong * <sun_tong_001@users.sourceforge.net>
Subject: How to remove lines with seen prefix
Message-Id: <tZtOv.100023$_k.77984@fx16.iad>

Hi, 

How to remove lines with seen prefix?

The "prefix" has the normal meaning, e.g., string 'abcde' has a prefix of 
'abc'. The pseudo code would be:

  $seen = last_line;
  if current_line begins exactly as $seen then next
  else { output current_line; $seen = current_line }

So for my following sample input

$ echo -e '/home/dave\n/home/dave/file1\n/home/dave/sub2/file2\n/home/phil
\n/home/phil/file2'
/home/dave
/home/dave/file1
/home/dave/sub2/file2
/home/phil
/home/phil/file2

The output would be 

/home/dave
/home/phil

Of course, the input would be sorted. 

Can you help please? Thanks



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

Date: Sat, 06 Sep 2014 09:01:09 +0200
From: gamo <gamo@telecable.es>
Subject: Re: How to remove lines with seen prefix
Message-Id: <luebfm$rnv$1@speranza.aioe.org>

El 06/09/14 a las 04:05, * Tong * escribió:
> Hi,
>
> How to remove lines with seen prefix?
>
> The "prefix" has the normal meaning, e.g., string 'abcde' has a prefix of
> 'abc'. The pseudo code would be:
>
>    $seen = last_line;
>    if current_line begins exactly as $seen then next
>    else { output current_line; $seen = current_line }
>
> So for my following sample input
>
> $ echo -e '/home/dave\n/home/dave/file1\n/home/dave/sub2/file2\n/home/phil
> \n/home/phil/file2'
> /home/dave
> /home/dave/file1
> /home/dave/sub2/file2
> /home/phil
> /home/phil/file2
>
> The output would be
>
> /home/dave
> /home/phil
>
> Of course, the input would be sorted.
>
> Can you help please? Thanks
>

(untested)

while (<>){
	chomp;
	$count++;
	$seen{$count} = $_;    # In doubt, a hash
}	

for $i (1..$count) {
	$ok=0;
	$temp = $seen{$i};
	for $j ($count+1..$count) {
		if ($seen{$j} =~ /^$temp/ && $ok==0) {
			$ok=1;
#			print "$seen{$i}\n";
#                       But could be dupes!
			$hash{$i}++;
			if ($hash{$i} ==1 ) { push @result, $i; }
			last; 		
		}	
	}	
}	
for $i (@result){
	print "$seen{$i}\n";
}

Well this is an overkill but as a first answer...
HTH

-- 
http://www.telecable.es/personales/gamo/


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

Date: Sat, 06 Sep 2014 11:30:18 +0200
From: gamo <gamo@telecable.es>
Subject: Re: How to remove lines with seen prefix
Message-Id: <luek7a$fj0$1@speranza.aioe.org>

El 06/09/14 a las 09:01, gamo escribió:
> (untested)
>
> while (<>){
>      chomp;
>      $count++;
>      $seen{$count} = $_;    # In doubt, a hash
> }
>
> for $i (1..$count) {
>      $ok=0;

This variable is superflous.

>      $temp = $seen{$i};
>      for $j ($count+1..$count) {

for $j ($i+1..$count) {

>          if ($seen{$j} =~ /^$temp/ && $ok==0) {
>              $ok=1;
> #            print "$seen{$i}\n";
> #                       But could be dupes!
>              $hash{$i}++;
>              if ($hash{$i} ==1 ) { push @result, $i; }
>              last;
>          }
>      }
> }
> for $i (@result){
>      print "$seen{$i}\n";
> }
>
> Well this is an overkill but as a first answer...
> HTH
>

You don't say what to do with prefixes not used after.

-- 
http://www.telecable.es/personales/gamo/


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

Date: Sat, 06 Sep 2014 12:20:19 +0100
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: How to remove lines with seen prefix
Message-Id: <87vbp1ov0s.fsf@bsb.me.uk>

* Tong * <sun_tong_001@users.sourceforge.net> writes:

> How to remove lines with seen prefix?
>
> The "prefix" has the normal meaning, e.g., string 'abcde' has a prefix of 
> 'abc'. The pseudo code would be:
>
>   $seen = last_line;
>   if current_line begins exactly as $seen then next
>   else { output current_line; $seen = current_line }
>
> So for my following sample input
>
> $ echo -e '/home/dave\n/home/dave/file1\n/home/dave/sub2/file2\n/home/phil
> \n/home/phil/file2'
> /home/dave
> /home/dave/file1
> /home/dave/sub2/file2
> /home/phil
> /home/phil/file2
>
> The output would be 
>
> /home/dave
> /home/phil
>
> Of course, the input would be sorted. 

Is

  my $l = '\n';
  while (<>) {
      if ($_ !~ /^\Q$l/) {
          print;
          chomp;
          $l = $_;
      }
  }

what you are looking for?

-- 
Ben.


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

Date: Sat, 06 Sep 2014 13:00:47 GMT
From: * Tong * <sun_tong_001@users.sourceforge.net>
Subject: Re: How to remove lines with seen prefix
Message-Id: <3ADOv.118518$Fo3.90901@fx09.iad>

On Sat, 06 Sep 2014 12:20:19 +0100, Ben Bacarisse wrote:

>> How to remove lines with seen prefix?
>>
>> The "prefix" has the normal meaning, e.g., string 'abcde' has a prefix
>> of 'abc'. The pseudo code would be:
>>
>>   $seen = last_line;
>>   if current_line begins exactly as $seen then next else { output
>>   current_line; $seen = current_line }
>>
>> So for my following sample input
>>
>> $ echo -e
>> '/home/dave\n/home/dave/file1\n/home/dave/sub2/file2\n/home/phil
>> \n/home/phil/file2'
>> /home/dave /home/dave/file1 /home/dave/sub2/file2 /home/phil
>> /home/phil/file2
>>
>> The output would be
>>
>> /home/dave /home/phil
>>
>> Of course, the input would be sorted.
> 
> Is
> 
>   my $l = '\n';
>   while (<>) {
>       if ($_ !~ /^\Q$l/) {
>           print; chomp; $l = $_;
>       }
>   }

BINGO! THANKS A LOT!!!

> what you are looking for?

I have two disk volumes containing similar content. I want to copy what's 
in v1 but missing from v2 into another disk volume, v3. Using find, sort, 
and comm, I am able to get a list of what to copy, but I need to further 
clean up that list. I.e., as long as I have /home/dave in the list, I 
don't need the other two.




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

Date: Sat, 06 Sep 2014 15:08:52 +0100
From: Henry Law <news@lawshouse.org>
Subject: Re: How to remove lines with seen prefix
Message-Id: <N5mdnQYOZOjriJbJnZ2dnUVZ8nSdnZ2d@giganews.com>

On 06/09/14 14:00, * Tong * wrote:
> I have two disk volumes containing similar content. I want to copy what's
> in v1 but missing from v2 into another disk volume, v3.

I'd feel more inclined to use File::Find on v1 and use the file 
existence test to see whether or not $File::Find::name is on v2; if not 
then use File::Copy to copy it to v3.  Something like this (hacked 
together on the fly, untested, probably contains logic errors ...)

use File::Find;
use File::Copy;
my $v1 = '/first/volume';
my $v2 = '/second/volume';
my $v3 = '/another/volume';
find( \%checkit, $v1 );

sub checkit {
    my $unique_part = substr($File::Find::name,length($v1));
    my $v2_name = $v2.$unique_part;
    unless ( -e $v2_name ) {
       copy( $File::Find::name, $v3.$unique_part )
          or die "Failed to copy $File::Find::name: $!";
       print "$File::Find::name -> $v3.$unique_part\n";
    }
}

If you want to compare more than just the names of the files (such as 
check sizes, dates, MD5 hashes and so on), then the "unless" statement 
could be easily modified.

-- 

Henry Law            Manchester, England


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

Date: Sat, 06 Sep 2014 14:26:01 +0300
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: How to remove lines with seen prefix
Message-Id: <slrnm0lrq9.ag5.whynot@orphan.zombinet>

with <tZtOv.100023$_k.77984@fx16.iad> * Tong * wrote:

> How to remove lines with seen prefix?

You realy should go IRC for shit like this.

% echo '/home/dave
/home/dave/file1                  
/home/dave/sub2/file2
/home/phil  
/home/phil/file2' | perl -nwle ' 
chomp;                                             
print "$seen" if defined $seen && !index $_, $seen;
$seen = $_'
/home/dave      
/home/phil

*SKIP*
> $ echo -e '/home/dave\n/home/dave/file1\n/home/dave/sub2/file2\n/home/phil
> \n/home/phil/file2'

Mai I ask you, your shell doesn't do multi-line quotes?

*CUT*

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Sun, 07 Sep 2014 03:06:01 +0200
From: gamo <gamo@telecable.es>
Subject: Re: How to remove lines with seen prefix
Message-Id: <lugb1s$tl4$1@speranza.aioe.org>

El 06/09/14 a las 11:30, gamo escribió:
> You don't say what to do with prefixes not used after.

Also, there could be an error in the previous sort which
could affect the result. The order is ], /, [, and if a
name includes ] at the end, count as no seen.

Another orientation is that the only prefix is /home/
and could be counted every distinct name up to "/"

-- 
http://www.telecable.es/personales/gamo/


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

Date: Sun, 07 Sep 2014 18:10:26 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How to remove lines with seen prefix
Message-Id: <87ppf7e4ql.fsf@sable.mobileactivedefense.com>

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> * Tong * <sun_tong_001@users.sourceforge.net> writes:
>
>> How to remove lines with seen prefix?
>>
>> The "prefix" has the normal meaning, e.g., string 'abcde' has a prefix of 
>> 'abc'. The pseudo code would be:
>>
>>   $seen = last_line;
>>   if current_line begins exactly as $seen then next
>>   else { output current_line; $seen = current_line }
>>
>> So for my following sample input
>>
>> $ echo -e '/home/dave\n/home/dave/file1\n/home/dave/sub2/file2\n/home/phil
>> \n/home/phil/file2'
>> /home/dave
>> /home/dave/file1
>> /home/dave/sub2/file2
>> /home/phil
>> /home/phil/file2
>>
>> The output would be 
>>
>> /home/dave
>> /home/phil
>>
>> Of course, the input would be sorted. 
>
> Is
>
>   my $l = '\n';
>   while (<>) {
>       if ($_ !~ /^\Q$l/) {
>           print;
>           chomp;
>           $l = $_;
>       }
>   }
>
> what you are looking for?

This won't work if the input contains blank lines (probably not a
problem here) because /^\Q$l/ will match anything when $l is the empty
string.

Alternate option:

perl -lne '$l and /^\Q$l/ or $l = $_, print'


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

Date: Mon, 08 Sep 2014 10:06:56 -0400
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: How to remove lines with seen prefix
Message-Id: <540db800$35$fuzhry+tra$mr2ice@news.patriot.net>

In <tZtOv.100023$_k.77984@fx16.iad>, on 09/06/2014
   at 02:05 AM, * Tong * <sun_tong_001@users.sourceforge.net> said:

>How to remove lines with seen prefix?

The question in your subject is extremely unclear.

>The "prefix" has the normal meaning, e.g., string 'abcde' has a
>prefix of  'abc'.

Still extremely unclear. From the example that you give, I assume that
what you really mean is 'The refix in question is the entirety of a
preceeding line.' It's still unclear whether you want to exclude all
seen prefixes or only the most recent; I would normally assume the
former.

>The pseudo code would be:
>  $seen = last_line;
>  if current_line begins exactly as $seen then next
>  else { output current_line; $seen = current_line }

You always need to consider edge cases; in this case, how to handle
the first line.

>Of course, the input would be sorted. 

There is no "of course"; that should have been part of the problem
statement.

>Can you help please?

The actual test could be something like

   if substr($current_line,0,length $prefix) = $prefix then next
   else {output; $prefix=$current_line}

or the equivalent with a pattern match.

-- 
Shmuel (Seymour J.) Metz, SysProg and JOAT  <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action.  I reserve the
right to publicly post or ridicule any abusive E-mail.  Reply to
domain Patriot dot net user shmuel+news to contact me.  Do not
reply to spamtrap@library.lspace.org



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

Date: Mon, 08 Sep 2014 23:05:35 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How to remove lines with seen prefix
Message-Id: <87oaup6a4w.fsf@sable.mobileactivedefense.com>

Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> * Tong * <sun_tong_001@users.sourceforge.net> writes:
>>> How to remove lines with seen prefix?

[...]


>>> $ echo -e '/home/dave\n/home/dave/file1\n/home/dave/sub2/file2\n/home/phil
>>> \n/home/phil/file2'
>>> /home/dave
>>> /home/dave/file1
>>> /home/dave/sub2/file2
>>> /home/phil
>>> /home/phil/file2
>>>
>>> The output would be 
>>>
>>> /home/dave
>>> /home/phil

[...]

>>   my $l = '\n';
>>   while (<>) {
>>       if ($_ !~ /^\Q$l/) {
>>           print;
>>           chomp;
>>           $l = $_;
>>       }
>>   }

[...]

> This won't work if the input contains blank lines (probably not a
> problem here) because /^\Q$l/ will match anything when $l is the empty
> string.
>
> Alternate option:
>
> perl -lne '$l and /^\Q$l/ or $l = $_, print'

This also won't work when the input contains blank lines since it will
print a blank line for each of them.


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

Date: Mon, 01 Sep 2014 17:33:06 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: ORM considered a driver of hardware sales (was: perl6 too much pointless functionality)
Message-Id: <87oauzpagt.fsf_-_@sable.mobileactivedefense.com>

Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
>>> On 20.08.14 18:34, Rainer Weikusat wrote:
>>>>>> >>'useful packages' are a secondary concern: If the language in itself is
>>>>>> >>useful, it can be used within the limits of the available library
>>>>>> >>support.

[...]

> Other example: Using Perl as an embedded language, embedded in
> PostgreSQL in my case. While CPAN modules could be used in this
> situation, they're rarely useful because Perl is really just a more
> powerful language for writing stored procedures (accessing  the
> facilities provided by the DB environment).
>
> The code I'm presently working on, while it will probably only end up as
> a few hundred lines, is supposed to enable import of some 70,000 AD
> users into a certain user database without blowing up, something
> the existing Java code for AD import cannot do.

JFTR: Performing this task with preciously little (ca 324 LOC) Perl code
and direct SQL statements instead Seam/Hibernate/JBoss/Java has
increased the speed by about a factor of 17 and some task which used to
take about 1h 20minutes can now be completed in 169s (an additional
index is responsible for the remaining speedup, something people who
view a database as 'persistency layer' aka 'dumb engine for storing
structured, binary data' are prone to overlook [politely put]).

I assume it would be possible to get the Perl solution down to more
'competitive' behaviour by adding enough CPAN modules to it.




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

Date: Tue, 02 Sep 2014 15:16:51 +0200
From: "G.B." <bauhaus@futureapps.invalid>
Subject: Re: ORM considered a driver of hardware sales
Message-Id: <lu4g00$q6q$2@dont-email.me>

On 01.09.14 18:33, Rainer Weikusat wrote:
> I assume it would be possible to get the Perl solution down to more
> 'competitive' behaviour by adding enough CPAN modules to it.

Or use JDBC, which is also somewhat beside the point.



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

Date: Wed, 10 Sep 2014 21:50:05 -0500
From: zeeb <zeeb@zeeb.zeeb>
Subject: Re: perl6 too much pointless functionality
Message-Id: <2014091021500583389-zeeb@zeebzeeb>

On 2014-08-12 13:58:40 +0000, George Mpouras said:

> On 12/8/2014 15:09, Peter Makholm wrote:
>> George Mpouras <gravitalsun@hotmail.foo> writes:
>> 
>>> For example look here   http://doc.perl6.org/language/variables
>> 
>> This isn't really different from perl5 except that 'my $*dynamic1' is
>> spelled 'local $dynamic1' in perl5.
>> 
>> //Makholm
>> 
> 
> 
> 
> they should implement the virtual machine (which is something 
> necessary) without touching the syntax of the language.
> Only simple changes as needed.
> They change almost everything, there is no point to that.
> I mean if everything is changed, why someone keep following ? They do 
> not learn the lesson of slow adoption of python 3 where the developers 
> pissed off.

Perl 5 is going nowhere. Perl 5 and 6 are too different. They're 
basically different languages.



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

Date: Mon, 01 Sep 2014 19:15:25 +0100
From: Henry Law <news@lawshouse.org>
Subject: Re: porting bash curl command into perl
Message-Id: <C7adnSeyDKIjKpnJnZ2dnUVZ8hidnZ2d@giganews.com>

On 23/08/14 20:42, chaos.lord@gmx.net wrote:
> i'm a little stucked about using curl with perl.

So you posted, over a week ago.  Since then there have been at least 
half a dozen attempts to help you, including some code that you could 
have modified to do what you want.

So where are you, you rude and ungrateful wretch?  Too busy lording it 
over chaos, I expect.  Next time you want a bit of help, forget it.

-- 

Henry Law            Manchester, England


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

Date: Tue, 9 Sep 2014 12:20:02 -0700 (PDT)
From: herbert.burnswell@gmail.com
Subject: utility perl module
Message-Id: <c1e8aeb3-189f-4925-8450-36d5fb67c244@googlegroups.com>

All,

I have created a PM that includes different utilities that I can call via scripts.  It's working pretty well except for one logging part.  I created the following in the PM:

#!/usr/bin/perl

package myutils;

use strict;
use warnings;
use base qw(Exporter);

our @ISA        = qw(Exporter);
our @EXPORT     = qw(begin_log write_log_header write_log);

our $logfile    = "/path/to/file.log";

sub begin_log
{

        open(LOG, ">$logfile") || die "Couldn't open $logfile: $!";
        write_log_header("Begin Log File");
        close LOG;

}

sub write_log_header
{

        open(LOG, ">>$logfile") || die "Couldn't open $logfile: $!";
        print LOG ("\n====================================\n");
        print LOG ( "   @_  --- ".scalar(localtime time));
        print LOG ("\n====================================\n");
        close LOG;

}

sub write_log
{

        open(LOG, ">>$logfile") || die "Couldn't open $logfile: $!";
        print LOG @_;
        close LOG;

}

1;

I know there are many logging options but I like how this works for my needs.

It works fine when called from other scripts however, as you can see, I have to define $logfile in the PM to get it to work.  I'd like to define the $logfile in the scripts that call these routines.

What do I need to do to be able to set the $logfile from the calling script?

Any guidance is greatly appreciated.

TIA,

Herb 


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

Date: Wed, 10 Sep 2014 09:42:33 +0200
From: gamo <gamo@telecable.es>
Subject: Re: utility perl module
Message-Id: <luovdg$5vu$1@speranza.aioe.org>

El 09/09/14 a las 21:20, herbert.burnswell@gmail.com escribió:
>          print LOG @_;

You can put something more in that line, as localtime, $0 and the \n

>          close LOG;
>
> }
>
> 1;
>
> I know there are many logging options but I like how this works for my needs.

Well there is a dedicated daemon syslog which could be used trough 
Sys::Syslog in linux. But if you want a log for every script and works 
for you, it's fine. Thanks for sharing.


-- 
http://www.telecable.es/personales/gamo/


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

Date: Wed, 10 Sep 2014 05:19:41 -0700 (PDT)
From: Lesley Binks <lesley.binks@gmail.com>
Subject: Re: utility perl module
Message-Id: <32c86276-8b06-435b-9cba-126e23d57a61@googlegroups.com>

On Tuesday, 9 September 2014 20:20:10 UTC+1, herbert....@gmail.com  wrote:
> All,
> 
> 
> 
> I have created a PM that includes different utilities that I can call via scripts.  It's working pretty well except for one logging part.  I created the following in the PM:
> 
> 
> 
> #!/usr/bin/perl
> 
> 
> 
> package myutils;
> 
> 
> 
> use strict;
> 
> use warnings;
> 
> use base qw(Exporter);
> 
> 
> 
> our @ISA        = qw(Exporter);
> 
> our @EXPORT     = qw(begin_log write_log_header write_log);
> 
> 
> 
> our $logfile    = "/path/to/file.log";
our $logfile;
> 
> 
> 
> sub begin_log
> 
> {
> 
$logfile = shift; ## assumes one parameter.
> 
> 
>         open(LOG, ">$logfile") || die "Couldn't open $logfile: $!";
> 
>         write_log_header("Begin Log File");
> 
>         close LOG;
> 
> 
> 
> }
> 
> 
> 
> sub write_log_header
> 
> {
> 
> 
> 
>         open(LOG, ">>$logfile") || die "Couldn't open $logfile: $!";
> 
>         print LOG ("\n====================================\n");
> 
>         print LOG ( "   @_  --- ".scalar(localtime time));
> 
>         print LOG ("\n====================================\n");
> 
>         close LOG;
> 
> 
> 
> }
> 
> 
> 
> sub write_log
> 
> {
> 
> 
> 
>         open(LOG, ">>$logfile") || die "Couldn't open $logfile: $!";
> 
>         print LOG @_;
> 
>         close LOG;
> 
> 
> 
> }
> 
> 
> 
> 1;
> 
> 
> 
<snip>
> 
> 
> What do I need to do to be able to set the $logfile from the calling script?
> 

Just pass the logfile information as a parameter thusly

my $logfile="some/place/to/log/stuff";
begin_log($logfile);

Kind regards

L.


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

Date: Wed, 10 Sep 2014 14:51:23 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: utility perl module
Message-Id: <87ppf31vw4.fsf@vps1.hacking.dk>

herbert.burnswell@gmail.com writes:

> sub begin_log
> {
>
>         open(LOG, ">$logfile") || die "Couldn't open $logfile: $!";
>         write_log_header("Begin Log File");
>         close LOG;
>
> }

I would really recommend that you use lexical filehandles. That is,
rewrite this to look like:

    open(my $logfh, ">", $logfile) or die "...";
    ...; # Do something with $logfh
    close $logfh;

This will make the code less susceptible to have bade side effects when
combined with other code. I also used the three-args open insted of your
two-args open. This is just what I consider good style.

And notice that write_log_header() opens the logfile itself, so in
reallity begin_log is a no-op except for the work write_log_header does
by itself.

> It works fine when called from other scripts however, as you can see,
> I have to define $logfile in the PM to get it to work.  I'd like to
> define the $logfile in the scripts that call these routines.

Without making other changes in your utility module, you can set the
$logfile variable from outside the module. Just use something like this
in your scripts:

    $myutils::logfile = "/path/to/scriptname.log";

This is because you used an "our" declaration to make a package
variable, instead of declaring $logfile with "my".

//Makholm


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

Date: Wed, 10 Sep 2014 15:10:55 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: utility perl module
Message-Id: <87d2b34lcg.fsf@sable.mobileactivedefense.com>

herbert.burnswell@gmail.com writes:

[...]

> #!/usr/bin/perl
>
> package myutils;
>
> use strict;
> use warnings;
> use base qw(Exporter);
>
> our @ISA        = qw(Exporter);
> our @EXPORT     = qw(begin_log write_log_header write_log);
>
> our $logfile    = "/path/to/file.log";
>
> sub begin_log
> {
>
>         open(LOG, ">$logfile") || die "Couldn't open $logfile: $!";
>         write_log_header("Begin Log File");
>         close LOG;
>
> }

[...]

> It works fine when called from other scripts however, as you can see,
> I have to define $logfile in the PM to get it to work.  I'd like to
> define the $logfile in the scripts that call these routines.

You can access a $logfile variable in the package of the caller by using
the (scalar-context) return value of caller to construct a symbolic
reference to it:

-------------
package A;

use strict;

sub logfile
{
    no strict 'refs';
    my ($lf, $pckg);

    $pckg = caller();
    
    $lf = ${$pckg.'::logfile'};
    $lf // die("$pckg doesn't defined \$logfile");

    return $lf;
}

package main;

our $logfile = 'lf';

print(A::logfile(), "\n");
-------------

The return value of this logfile subroutine can be used in place of the
$logfile variable in your code.


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

Date: Wed, 10 Sep 2014 16:17:48 -0700
From: Brian Helterline <brian.helterline@hp.com>
Subject: Re: utility perl module
Message-Id: <luqm6s$6qp$1@usenet01.boi.hp.com>

On 9/10/2014 5:51 AM, Peter Makholm wrote:> herbert.burnswell@gmail.com 
writes:
 >
 >> sub begin_log
 >> {
 >>
 >>          open(LOG, ">$logfile") || die "Couldn't open $logfile: $!";
 >>          write_log_header("Begin Log File");
 >>          close LOG;
 >>
 >> }
 >
 > I would really recommend that you use lexical filehandles. That is,
 > rewrite this to look like:
 >
 >      open(my $logfh, ">", $logfile) or die "...";
 >      ...; # Do something with $logfh
 >      close $logfh;
 >
 > This will make the code less susceptible to have bade side effects when
 > combined with other code. I also used the three-args open insted of your
 > two-args open. This is just what I consider good style.
 >
 > And notice that write_log_header() opens the logfile itself, so in
 > reallity begin_log is a no-op except for the work write_log_header does
 > by itself.

no. begin_log() creates an empty logfile.  Without it, begin_log() would
append to whatever was there from before.





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

Date: Wed, 10 Sep 2014 17:19:24 -0700 (PDT)
From: herbert.burnswell@gmail.com
Subject: Re: utility perl module
Message-Id: <8eba73ee-bcea-4be7-99d2-3903f1192925@googlegroups.com>

Thank you all for your great suggestions.  I will dig in a bit and try a few of these out.

I really appreciate the help,

Herb


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

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 4281
***************************************


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