[19331] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1526 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 15 06:05:52 2001

Date: Wed, 15 Aug 2001 03:05:19 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <997869919-v10-i1526@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 15 Aug 2001     Volume: 10 Number: 1526

Today's topics:
    Re: 2 dimensional array suntax help, please <ren@tivoli.com>
    Re: 2 dimensional array suntax help, please (Stan Brown)
        Can someone critique my code and point to better way?? <miscellaneousemail@yahoo.com>
    Re: Can someone critique my code and point to better wa <krahnj@acm.org>
    Re: Can someone critique my code and point to better wa <uri@sysarch.com>
    Re: Can someone critique my code and point to better wa <miscellaneousemail@yahoo.com>
    Re: Can someone critique my code and point to better wa (Tad McClellan)
    Re: Can someone critique my code and point to better wa <miscellaneousemail@yahoo.com>
    Re: Can someone critique my code and point to better wa <miscellaneousemail@yahoo.com>
    Re: Can someone critique my code and point to better wa <miscellaneousemail@yahoo.com>
    Re: Can someone critique my code and point to better wa <krahnj@acm.org>
    Re: Can someone critique my code and point to better wa <uri@sysarch.com>
    Re: confused - can't print to file (David Efflandt)
        Crypt::RSA, does that exist?  I just want to decode... (Torsten Mohr)
    Re: Emacs modules for Perl programming (Kev The Condenser)
        Evaluation order of object methods <djberg96@hotmail.com>
        FAQ: How do I get a file's timestamp in perl? <faq@denver.pm.org>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 14 Aug 2001 18:47:15 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: 2 dimensional array suntax help, please
Message-Id: <m3d75y5hmk.fsf@dhcp9-161.support.tivoli.com>

On 14 Aug 2001, stanb@panix.com wrote:

> In <3B79A1D7.A7AED971@earthlink.net> Benjamin Goldberg
> <goldbb2@earthlink.net> writes:
> 
>>By the way, are you *sure* entry takes a list, not a reference to a
>>list?
> 
> Mmm, it seems to be working thta way. Actually in the doc's it says
> it takes a scalar, but I already had it working with an array, which
> I was using as a prototype for this code.

FWIW, not only can a subroutine detect what context it is in and
behave differently, it can also test what type of arguments it is
passed and behave differently.  In other words, it is possible that
this routine can handle either a list or a list reference.

  sub dup_list {
    my @list = ref $_[0] ? @{$_[0]} : @_;
    @list, @list;
  }
  
  print dup_list(1, 2, 3), dup_list([4, 5, 6]), "\n";


(Bonus: what does dup_list() return in scalar context?)

-- 
Ren Maddox
ren@tivoli.com


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

Date: 14 Aug 2001 22:18:40 -0400
From: stanb@panix.com (Stan Brown)
Subject: Re: 2 dimensional array suntax help, please
Message-Id: <9lcm60$b0d$1@panix1.panix.com>

In <m3d75y5hmk.fsf@dhcp9-161.support.tivoli.com> Ren Maddox <ren@tivoli.com> writes:

>On 14 Aug 2001, stanb@panix.com wrote:

>> In <3B79A1D7.A7AED971@earthlink.net> Benjamin Goldberg
>> <goldbb2@earthlink.net> writes:
>> 
>>>By the way, are you *sure* entry takes a list, not a reference to a
>>>list?
>> 
>> Mmm, it seems to be working thta way. Actually in the doc's it says
>> it takes a scalar, but I already had it working with an array, which
>> I was using as a prototype for this code.

>FWIW, not only can a subroutine detect what context it is in and
>behave differently, it can also test what type of arguments it is
>passed and behave differently.  In other words, it is possible that
>this routine can handle either a list or a list reference.

Life in the perl world continues to get more interesting.

A bit of history here, about 10 years agao, I did a fair amount of perl
coding in perl4. 

I always said, after a day or two at this, I would get in perl mode. That
was, I could code _anything_ in a realy short time. Unfortunately this cde
was largely "write only", as I found out when I had to go back a year or so
later and update it :-(

Since then I have done primarily C and ksh. I found myself atracted back to
perl a few months agao, primarliy for the easy access to Oracle data. About
a month ago, I discovered Perl/TK. 

I must say perl5 has allowed me to write (what I hope is) much more
readable code. 

The only down side I see at the moment is that if I finally do decide some
of these things need to be recoded in C for the last bit of speed. There
will be a lot of "perlism's" to deal with in the translations. I don't just
mean syntax, I mean basic code design issues. One example is the extensive
use of hashes, but it's only 1 of many examples.

Also, thnaks to the wonderful helpful perl comunity for helping me to lear
"the perl way". Support like this just is not available for any price!

Thanks, again.


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

Date: Wed, 15 Aug 2001 04:00:42 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Can someone critique my code and point to better way??
Message-Id: <MPG.15e3a1ec5d50eb0989749@news.edmonton.telusplanet.net>

Hi everyone,

I was wondering if someone could critique my code by letting me know if 
there is a better, more efficient way to write or do what it does.

The code:

# %h is a hash tied to a database with DB_File.
# function called by:  print_contents(\*STDOUT, %h);

sub print_contents
{
  my ($FILE_HANDLE, %file_hash) = @_;
  my (@info, $key, $value);
  while (($key, $value) = each %file_hash)
  { 
    load_array(\@info, $key.",".$value);
    printf $FILE_HANDLE "%-20.20s %-10.10s %-15.15s %-13.13s %-12.12s %-
3.3s\n", @info;
  }	 
}

sub load_array
{
  my ($array, $delimited_string) = @_;
  (@$array[0], @$array[1], @$array[2], @$array[3], @$array[4], 
@$array[5]) = 
    split(/,/,$delimited_string);
}

Thanks.

---
Carlos 
www.internetsuccess.ca
*NOTE*: Internet Success is NOT yet fully operational so although you are 
welcomed to visit and take a look, trying to subscribe will only be a 
frustration for you as your data will not be saved at this time.


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

Date: Wed, 15 Aug 2001 04:39:44 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <3B79FD3B.82259A9E@acm.org>

"Carlos C. Gonzalez" wrote:
> 
> I was wondering if someone could critique my code by letting me know if
> there is a better, more efficient way to write or do what it does.
> 
> The code:
> 
> # %h is a hash tied to a database with DB_File.
> # function called by:  print_contents(\*STDOUT, %h);

Pass a reference to the hash instead of the hash itself.

print_contents( \*STDOUT, \%h );


> sub print_contents
> {
>   my ($FILE_HANDLE, %file_hash) = @_;
>   my (@info, $key, $value);
>   while (($key, $value) = each %file_hash)
>   {
>     load_array(\@info, $key.",".$value);
>     printf $FILE_HANDLE "%-20.20s %-10.10s %-15.15s %-13.13s %-12.12s %-
> 3.3s\n", @info;

printf $FILE_HANDLE "%-20.20s %-10.10s %-15.15s %-13.13s %-12.12s
%-3.3s\n", $key, (split/,/,$value)[0..4];


>   }
> }
> 
> sub load_array
> {
>   my ($array, $delimited_string) = @_;
>   (@$array[0], @$array[1], @$array[2], @$array[3], @$array[4],
> @$array[5]) =
>     split(/,/,$delimited_string);
> }



John
-- 
use Perl;
program
fulfillment


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

Date: Wed, 15 Aug 2001 04:57:56 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <x77kw5ly23.fsf@home.sysarch.com>

>>>>> "CCG" == Carlos C Gonzalez <miscellaneousemail@yahoo.com> writes:

  CCG> I was wondering if someone could critique my code by letting me know if 
  CCG> there is a better, more efficient way to write or do what it does.

it would help if you specified your goal too. i can discern it here but
with more complex code it will be useful.

  CCG> The code:

  CCG> # %h is a hash tied to a database with DB_File.
  CCG> # function called by:  print_contents(\*STDOUT, %h);

in general don't pass a hash by value. it will usually be better to
pass a ref. in this case you are going to do two loops over the hash
key/values which is a waste. first you do it when you pass the whole
tied hash and copy it and then inside the sub you loop over it again
with each.

  CCG> sub print_contents
  CCG> {
  CCG>   my ($FILE_HANDLE, %file_hash) = @_;

better to use $FH for that name. it is too long and it is obviously a
handle.

	 my ($FH, %hash_ref) = @_;


  CCG>   my (@info, $key, $value);
  CCG>   while (($key, $value) = each %file_hash)

now, loop over the hash ref instead.

	while ( ($key, $value) = each %{$hash_ref} )

  CCG>   { 
  CCG>     load_array(\@info, $key.",".$value);

most people don't use . for simple strings like that. string
interpolation is much clearer for that.

	load_array( \@info, "$key,$value" );


  CCG>     printf $FILE_HANDLE "%-20.20s %-10.10s %-15.15s %-13.13s %-12.12s %-
  CCG> 3.3s\n", @info;
  CCG>   }	 
  CCG> }

i rarely use printf. too often when i format strings i use sprintf and
then i can print it to a file, to stderr/stdout for debugging, to a log
file, etc. another trick i use is to make a sub sprintf to a string and
return that. the caller can then decide where to print it too. this is a
more flexible way than hardwiring a print to a file.

  CCG> sub load_array
  CCG> {
  CCG>   my ($array, $delimited_string) = @_;

that is separated, not delimited. the comma separates the two values.

  CCG>   (@$array[0], @$array[1], @$array[2], @$array[3], @$array[4], 
  CCG> @$array[5]) = 
  CCG>     split(/,/,$delimited_string);
  CCG> }

i don't get what that code is supposed to do. you assign what looks like
only 2 elements to the array. and you overwrite the same two elements in
each loop iteration. and why you joined the key/value just to split it
later makes little sense. maybe there are other commas in your keys and
data but that is not clear. so how would @info get filled with 6 values?

you could just do a push instead in the each loop and then print, no
need for the load_array sub:

	while ( ($key, $value) = each %{$hash_ref} )

		push( @info, ($key, $value) ) ;

but again, there is no clear logic since we don't know the data and how
it is meant to be used.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs  --------------------------  http://jobs.perl.org


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

Date: Wed, 15 Aug 2001 06:12:55 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <MPG.15e3c0cd7a42523098974c@news.edmonton.telusplanet.net>

In article <x77kw5ly23.fsf@home.sysarch.com>, Uri Guttman at 
uri@sysarch.com says...

> but again, there is no clear logic since we don't know the data and how
> it is meant to be used.
> 

Wow!  Lots of stuff for me to chew on.  I should have described more of 
the logic involved.  Sorry about that.

My data consists of the following as an example:

email_address 
first_name,country,city,terms_of_subscription_agreement_number,new_subscr
iber

The above names describe the fields in my file.  email_address does not 
have a comma after it.  There is no newline on the above record (as far 
as I know) although it may show up that way in your newsreader.

Since I am storing the values in a file through the module DB_File I must 
stringify the value to associate with my key so that I can save and 
associate more than one value variable with the key.

I append a comma to the key in my code so that when I split it all up 
everything gets split correctly.  Since I am using a hash to store the 
contents of the file the key in the hash only contains the email_address 
(with no comma) while the value contains everything else (with commas). 

My print_contents sub is meant to print out the contents to whatever 
$FILE_HANDLE I specify.  Whether to the screen for debugging or to a text 
file on my Windows 98 system that I can open with a spreadsheet. 

The reason I created a seperate load_array sub is because I split the 
values in my file into their respective fields in various places in my 
code and rather than have that code scattered about I wanted to put it in 
one place. 

I hope that helps.

Thanks again for everyone's input.  I am learning a lot. 

---
Carlos 
www.internetsuccess.ca
*NOTE*: Internet Success is NOT yet fully operational so although you are 
welcomed to visit and take a look, trying to subscribe will only be a 
frustration for you as your data will not be saved at this time.


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

Date: Wed, 15 Aug 2001 00:39:20 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <slrn9njv7o.c6u.tadmc@tadmc26.august.net>

Carlos C. Gonzalez <miscellaneousemail@yahoo.com> wrote:
>
>I was wondering if someone could critique my code by letting me know if 
>there is a better, more efficient way to write or do what it does.


>sub print_contents
>{
>  my ($FILE_HANDLE, %file_hash) = @_;
>  my (@info, $key, $value);


You should restrict variables to the smallest possible scope.

The variables are only needed in the while loop, so they should be
scoped to the while loop.


>  while (($key, $value) = each %file_hash)
>  { 

   while ( my($key, $value) = each %file_hash)
   {
     my @info;


>    load_array(\@info, $key.",".$value);


That's an obfuscated way of getting a comma between two values:

   load_array(\@info, "$key,$value");


>    printf $FILE_HANDLE "%-20.20s %-10.10s %-15.15s %-13.13s %-12.12s %-
>3.3s\n", @info;
>  }	 
>}


You can use a "list slice" as suggested in another followup,
and avoid the @info temporary variable.


>sub load_array
>{
>  my ($array, $delimited_string) = @_;
>  (@$array[0], @$array[1], @$array[2], @$array[3], @$array[4], 
>@$array[5]) = 
>    split(/,/,$delimited_string);


   @$array = split(/,/,$delimited_string); # no need for all that indexing

>}


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


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

Date: Wed, 15 Aug 2001 06:30:47 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <MPG.15e3c466bcf15c7498974d@news.edmonton.telusplanet.net>

In article <x77kw5ly23.fsf@home.sysarch.com>, Uri Guttman at 
uri@sysarch.com says...

> in general don't pass a hash by value. it will usually be better to
> pass a ref. in this case you are going to do two loops over the hash
> key/values which is a waste. first you do it when you pass the whole
> tied hash and copy it and then inside the sub you loop over it again
> with each.

I see what you mean Uri.  I guess I was thinking more from the standpoint 
that passing by reference might accidentally change the value of the hash 
whereas in this instance I did not need to have the value changed inside 
the print_contents sub.  I did not think about the overhead of passing a 
copy to the sub.  Interesting....

Is there a way to protect the value of the hash inside a sub when that 
hash is passed by reference if there will never be any cause to change 
it's value inside the sub?  

In other words is there a way to get the best of both worlds.  The 
protection of the calling argument value passing by value and the 
processing efficiency of calling by reference?

> better to use $FH for that name. it is too long and it is obviously a
> handle.
> 
> 	 my ($FH, %hash_ref) = @_;

Good point Uri.  I may at some point sell my code to my subscribers so 
they can easily build their own newsletters or something similar.  
Unfortunately most will not be interested in becoming Perl programmers.  
As such having long names like I do will make things clearer for them and 
less time consuming for me in terms of answering questions about my code. 

I am keeping my intended audience and the way they will likely view 
things in the back of my head as I write my code.  

>   CCG>     load_array(\@info, $key.",".$value);
> 
> most people don't use . for simple strings like that. string
> interpolation is much clearer for that.
> 
> 	load_array( \@info, "$key,$value" );

I didn't know you could do that.  I will have to implement that Uri. 

> 	while ( ($key, $value) = each %{$hash_ref} )
> 
> 		push( @info, ($key, $value) ) ;
> 

Interesting.  Definitely would clean up my code.  I will have to look at 
the push function.  I tend to see it used in sample code quite a bit. 

Thanks again Uri.

---
Carlos 
www.internetsuccess.ca
*NOTE*: Internet Success is NOT yet fully operational so although you are 
welcomed to visit and take a look, trying to subscribe will only be a 
frustration for you as your data will not be saved at this time.


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

Date: Wed, 15 Aug 2001 06:37:37 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <MPG.15e3c6691baacbc798974e@news.edmonton.telusplanet.net>

In article <slrn9njv7o.c6u.tadmc@tadmc26.august.net>, Tad McClellan at 
tadmc@augustmail.com says...

> You should restrict variables to the smallest possible scope.
> The variables are only needed in the while loop, so they should be
> scoped to the while loop.
> >  while (($key, $value) = each %file_hash)
> >  { 
>    while ( my($key, $value) = each %file_hash)
>    {
>      my @info;

Makes sense Tad but I seem to recall reading somewhere that having a my 
variable inside a looping structure recreated it for each instance of the 
loop. Is this true?  Is there truly some penalty associated with having a 
my declaration inside of a loop?

Thanks.

---
Carlos 
www.internetsuccess.ca
*NOTE*: Internet Success is NOT yet fully operational so although you are 
welcomed to visit and take a look, trying to subscribe will only be a 
frustration for you as your data will not be saved at this time.


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

Date: Wed, 15 Aug 2001 07:22:39 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <MPG.15e3d135dcd7b3a7989750@news.edmonton.telusplanet.net>

In article <3B79FD3B.82259A9E@acm.org>, John W. Krahn at krahnj@acm.org 
says...

> printf $FILE_HANDLE "%-20.20s %-10.10s %-15.15s %-13.13s %-12.12s
> %-3.3s\n", $key, (split/,/,$value)[0..4];

Using the above line (- the [0..4]) has totally eliminated the need I 
felt for the load_array sub.  I like it.  Much cleaner than my code.

What's the [0..4] in your example for John?  I am guessing it's some kind 
of modifier to tell printf to split the value into 4 pieces and apply 
each piece to each of the last four format specifiers??

Thanks.

---
Carlos 
www.internetsuccess.ca
*NOTE*: Internet Success is NOT yet fully operational so although you are 
welcomed to visit and take a look, trying to subscribe will only be a 
frustration for you as your data will not be saved at this time.


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

Date: Wed, 15 Aug 2001 07:43:43 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <3B7A28D1.CDA95ABD@acm.org>

"Carlos C. Gonzalez" wrote:
> 
> In article <3B79FD3B.82259A9E@acm.org>, John W. Krahn at krahnj@acm.org
> says...
> 
> > printf $FILE_HANDLE "%-20.20s %-10.10s %-15.15s %-13.13s %-12.12s
> > %-3.3s\n", $key, (split/,/,$value)[0..4];
> 
> Using the above line (- the [0..4]) has totally eliminated the need I
> felt for the load_array sub.  I like it.  Much cleaner than my code.
> 
> What's the [0..4] in your example for John?  I am guessing it's some kind
> of modifier to tell printf to split the value into 4 pieces and apply
> each piece to each of the last four format specifiers??

(split/,/,$value) creates a list by splitting $value on commas. [0..4]
creates a slice (a sub-list) of the original list.
(split/,/,$value)[0..4] is the same as (split/,/,$value)[0,1,2,3,4]. You
can also slice a list in an arbitrary order (split/,/,$value)[3,1,4,2,0]



John
-- 
use Perl;
program
fulfillment


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

Date: Wed, 15 Aug 2001 07:56:24 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Can someone critique my code and point to better way??
Message-Id: <x7zo91kb87.fsf@home.sysarch.com>


>>>>> "CCG" == Carlos C Gonzalez <miscellaneousemail@yahoo.com> writes:

  CCG> Is there a way to protect the value of the hash inside a sub when
  CCG> that hash is passed by reference if there will never be any cause
  CCG> to change it's value inside the sub?

  CCG> In other words is there a way to get the best of both worlds.
  CCG> The protection of the calling argument value passing by value and
  CCG> the processing efficiency of calling by reference?

not directly or simply. you could do something with tied to handle
writes specially and not allow certain ways to write to it. but you
should trust your own code. if you specify the call uses the hash
reference in a read only mode, that is all you should need. anyone who
breaks that rule deserves their troubles. this is called designing an
API and it is one of the most critical parts to your design, how do i
make calls and pass in/out data to them? here you claim that the sub
takes a hash ref and doesn't modify the hash.

  >> better to use $FH for that name. it is too long and it is obviously a
  >> handle.
  >> 
  >> my ($FH, %hash_ref) = @_;

  CCG> Good point Uri.  I may at some point sell my code to my
  CCG> subscribers so they can easily build their own newsletters or
  CCG> something similar.  Unfortunately most will not be interested in
  CCG> becoming Perl programmers.  As such having long names like I do
  CCG> will make things clearer for them and less time consuming for me
  CCG> in terms of answering questions about my code.

long names are fine where they help, but FH isso common and well
understood that it is ok too. FILE works too. i have never seen
FILE_HANDLE except by newbies or long named phreaks. and *I* like
descriptive names but use FH or its kin.

  >> most people don't use . for simple strings like that. string
  >> interpolation is much clearer for that.

  >> load_array( \@info, "$key,$value" );

  CCG> I didn't know you could do that.  I will have to implement that Uri. 

that is the PURPOSE of double quoted strings. they interpolate while
single quoted strings don't. the . operator by itself is rarely used. in
perl6 it will be even rarer as the $() and @() constructs will
interpolate and allow any expression in a double quoted string.

but the .= operator is used quite often. can you figure out why?

  >> while ( ($key, $value) = each %{$hash_ref} )
  >> 
  >> push( @info, ($key, $value) ) ;
  >> 

  CCG> Interesting.  Definitely would clean up my code.  I will have to
  CCG> look at the push function.  I tend to see it used in sample code
  CCG> quite a bit.

you are expressing amazement at some very fundamental and simple perl
features. i think you should be reading a decent intro book (learning
perl, teach yourself perl in 24 hours, elements of programming with
perl) and read as much of the basic perldocs as you can. in particular
perlfunc and the perlfaq should be read at least once cover to
cover. skim over stuff you don't understand now and go back to the docs
whenever you need to (and it should be often).  then you will learn
about most of the features you will see and use.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs  --------------------------  http://jobs.perl.org


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

Date: Wed, 15 Aug 2001 02:09:19 +0000 (UTC)
From: see-sig@from.invalid (David Efflandt)
Subject: Re: confused - can't print to file
Message-Id: <slrn9njmee.58f.see-sig@typhoon.xnet.com>

On Tue, 14 Aug 2001 07:01:49 GMT, John W. Krahn <krahnj@acm.org> wrote:
> David Efflandt wrote:
>> 
>> On Sun, 12 Aug 2001 21:50:57 +0100, Terry <dcsnospam@ntlworld.com> wrote:
>> > Hello again,
>> >
>> > I'll answer my own question as it may be of help to someone else :)
>> > I assumed that truncating the file would set the filepointer at the start of
>> > the truncated file. Apparently it doesn't because if I add
>> >
>> > seek (PROG, 0, 0);
>> >
>> > straight after the truncate statement then everything works fine :o)
>> 
>> It makes no sense to truncate at the end of file because the data before
>> that in the file remains.  Seek the beginning _before_ you truncate.
> 
> Huh?
> 
>> The only reason your method worked was because the new data was longer
>> than the old data, and the truncate was redundant.  But if data is unknown
>> length (possibly shorter) you have to seek the beginning of the file, then
>> truncate, or you may have old data lingering after the new shorter data.
> 
> What?
> 
> $ ls -l text.txt
> -rw-r--r--    1 john     users       13870 Aug  3 13:21 text.txt
> $ perl -e'open I,"+<text.txt";truncate I,0;seek I,0,0;print I "shorter
> data\n"'
> $ ls -l text.txt
> -rw-r--r--    1 john     users          13 Aug 13 23:56 text.txt
> $ cat text.txt
> shorter data
> 
> Maybe you have a funny OS that doesn't allow this?

I guess I misunderstood truncate.  I thought it truncated based on current
location in the file, but apparently it truncates based on the beginning
of the file regardless of where you are (even at eof), which can leave you
way past the new eof.  I tested as above except inserting @I=<I> before
the truncate so I was at eof, and it still works as above.  Thanks for
straightening me out.

-- 
David Efflandt  (Reply-To is valid)  http://www.de-srv.com/
http://www.autox.chicago.il.us/  http://www.berniesfloral.net/
http://cgi-help.virtualave.net/  http://hammer.prohosting.com/~cgi-wiz/


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

Date: 15 Aug 2001 10:30:27 GMT
From: tmohr@schleim.qwe.de (Torsten Mohr)
Subject: Crypt::RSA, does that exist?  I just want to decode...
Message-Id: <9ldj03$1hg$1@schleim.qwe.de>

Hi everybody,

i'd like to decrypt an RSA-encoded "number" or "chiffre".
I took the algorithm from "Applied Kryptography".
I got the private key (consists of d and n) and the
block to decode, everything as a Math::BigInt.

Now i have to do this calculation:

m = c ** d mod n

Sadly, c and d (also n) are 1024 bit BigInt numbers.
In Java the class "BigInteger" has a method "modPow",
that exactly does what i need.  That method is kind of
complicated and based on the "chinese remainder theorem".

Does something like that exist for Perl?


Any hints are appreciated,
Torsten.


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

Date: 15 Aug 2001 09:57:51 GMT
From: mkc@aon.at (Kev The Condenser)
Subject: Re: Emacs modules for Perl programming
Message-Id: <3b7a479f$0$26754$6e365a64@newsreader02.highway.telekom.at>

In article <perl-faq/emacs-lisp-modules_997530006@rtfm.mit.edu>,
jari.aalto@poboxes.com wrote:
> Archive-name: perl-faq/emacs-lisp-modules
> Posting-Frequency: 2 times a month
> URL: http://tiny-tools.sourceforge.net/
> Maintainer: Jari Aalto <jari.aalto@poboxes.com>
> 
> Announcement: "What Emacs lisp modules can help with programming Perl"
> 
>     Preface
> 
>         Emacs is your friend if you have to do anything comcerning software
>         development: It offers plug-in modules, written in Emacs lisp
>         (elisp) language, that makes all your programmings wishes come
>         true. Please introduce yourself to Emacs and your programming era
>         will get a new light.
> 
i know, i know :)

> 	    http://tiny-tools.sourceforge.net/emacs-elisp.html
thank you very much for that url!
-- 
pid was 11705


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

Date: Wed, 15 Aug 2001 08:09:46 GMT
From: "Daniel Berger" <djberg96@hotmail.com>
Subject: Evaluation order of object methods
Message-Id: <e7qe7.35135$X6.1287052@typhoon.mn.mediaone.net>

Hi all,

It's late.  Couldn't sleep.  Hopefully this makes sense.

I've got question on object methods.  Setting up methods is easy, as is
calling individual methods.  For this particular object that I have created,
I would like to return $self if called in void context or an array or array
ref if called in list or scalar context, respectively.  No problem.

However, where I'm having trouble is when I want to 'stack' methods that
ultimately are called in list or scalar context.

First, my very simple object.

##############################################
package Some::Object;
use strict;

my @array;  # This is our global array that we manipulate

sub new{
   my $class = shift;
   @array = @_;
   my $self = [];
   bless($self, $class);
}

sub length{
    return scalar @array;
}

sub unique{
   my $self = shift;
   my %item;
   foreach(@array){ $item{$_}++ }

   unless(defined wantarray){
      @array = keys %item;
      return $self;
   }

   my @keys = keys %item;
   return @keys if wantarray;
   return \@keys;
}
###########################################

Now, what I would like to be able to do is:

my $so = Some::Object->new(1,2,3,4,5,2,3,4);
my $length = $array->unique()->length();           # Want to return '5'

This would ideally modify the object itself first (with a call to 'unique'),
then call 'length()', and assign it to $length.  However, since the
'unique()' method returns a value in list or scalar context, it's going to
get evaluated first and assigned to $length *before* the length() method is
called and will die, since I'm not operating on the object any longer at
that point.

Is there a non-cheesy way for me to accomplish what I want?  Am I even
making sense?

Thanks in advance (and good night).

Regards,

Dan




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

Date: Wed, 15 Aug 2001 06:17:08 GMT
From: PerlFAQ Server <faq@denver.pm.org>
Subject: FAQ: How do I get a file's timestamp in perl?
Message-Id: <Etoe7.113$V3.191996928@news.frii.net>

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with every Standard Distribution of
Perl.

+
  How do I get a file's timestamp in perl?

    If you want to retrieve the time at which the file was last read,
    written, or had its meta-data (owner, etc) changed, you use the -M, -A,
    or -C filetest operations as documented in the perlfunc manpage. These
    retrieve the age of the file (measured against the start-time of your
    program) in days as a floating point number. To retrieve the "raw" time
    in seconds since the epoch, you would call the stat function, then use
    localtime(), gmtime(), or POSIX::strftime() to convert this into
    human-readable form.

    Here's an example:

        $write_secs = (stat($file))[9];
        printf "file %s updated at %s\n", $file,
            scalar localtime($write_secs);

    If you prefer something more legible, use the File::stat module (part of
    the standard distribution in version 5.004 and later):

        # error checking left as an exercise for reader.
        use File::stat;
        use Time::localtime;
        $date_string = ctime(stat($file)->mtime);
        print "file $file updated at $date_string\n";

    The POSIX::strftime() approach has the benefit of being, in theory,
    independent of the current locale. See the perllocale manpage for
    details.

- 

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Answers to questions about LOTS of stuff, mostly not related to
Perl, can be found by pointing your news client to

    news:news.answers

or to the many thousands of other useful Usenet news groups.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.

  AUTHOR AND COPYRIGHT

    Copyright (c) 1997-1999 Tom Christiansen and Nathan
    Torkington.  All rights reserved.

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

                                                           05.21
-- 
    This space intentionally left blank


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

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.  

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


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