[19284] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1479 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 9 18:10:40 2001

Date: Thu, 9 Aug 2001 15:10:21 -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: <997395020-v10-i1479@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Thu, 9 Aug 2001     Volume: 10 Number: 1479

Today's topics:
    Re: q: converting strings <krahnj@acm.org>
    Re: q: converting strings <krahnj@acm.org>
    Re: Sub that defaults to use $_ in callers context (Yves Orton)
    Re: Sub that defaults to use $_ in callers context (Yves Orton)
    Re: Sub that defaults to use $_ in callers context <bart.lateur@skynet.be>
        traversing sub-directories <chuckmg@acm.org>
    Re: traversing sub-directories <eric@mizuhocap.com>
    Re: traversing sub-directories <cberry@cinenet.net>
        Update: (Perl) programming contest loosely based on the <tsee@gmx.net>
    Re: Using TRUE constant in IF expression?? <mjcarman@home.com>
    Re: Where can I learn to do this? <rsherman@ce.gatech.edu>
        Why is $i so popular? <lmoran@wtsg.com>
    Re: Why is $i so popular? <uri@sysarch.com>
    Re: Win32::eventlog --  Help!?!?! <kalinabears@hdc.com.au>
    Re: Write to middle of file <sh@planetquake.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 09 Aug 2001 19:54:05 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: q: converting strings
Message-Id: <3B72EA5D.C25BD194@acm.org>

Tad McClellan wrote:
> 
> Jasper McCrea <jasper@guideguide.com> wrote:
> >Tad McClellan wrote:
> >> Jasper McCrea <jasper@guideguide.com> wrote:
> >> >Malcolm Dew-Jones wrote:
> >> >> Oliver (ow22@nospam-cornell.edu) wrote:
> >> >> : how do i convert a string to an int? thanks
> >> >>
> >> >> In perl, if the string looks like a number then it is a number.
> >> >
> >> >This is a little misleading.
> >>
> >> Not nearly as misleading, IMO, as your comments that follow...
> >>
> >> >$one = "twelve";
> >> >$two = "12";
> >> >
> >> >both are strings, and look like numbers to me.
> >>                                          ^^^^^
> >>
> >> I suggest that perhaps you are in the minority then.
> >
> >Malcolm wrote 'if the string looks like a number then it is a number'.
> >
> >I suggest that 'twelve' looks like a number,
> 
> All the world does not all speak English.
> 
> For a significant portion of the world's population, that does not
> look like a number.
> 
> "12" looks like a number in (nearly?) all the world.

Except in Egypt (and other arabic countries?) where the numerals are
completely different.



John
-- 
use Perl;
program
fulfillment


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

Date: Thu, 09 Aug 2001 20:02:53 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: q: converting strings
Message-Id: <3B72EC6D.BEC06336@acm.org>

Jasper McCrea wrote:
> 
> 12
> That looks like two arabic symbols set side by side.

I've lived in Egypt for more than a year and I've never seen those
symbols used in arabic writing. (But then I'm not an expert in the
arabic language.)



John
-- 
use Perl;
program
fulfillment


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

Date: 9 Aug 2001 11:13:46 -0700
From: demerphq@hotmail.com (Yves Orton)
Subject: Re: Sub that defaults to use $_ in callers context
Message-Id: <74f348f7.0108091013.35ab561c@posting.google.com>

Bjoern Hoehrmann <bjoern@hoehrmann.de> wrote in message news:<3c02906c.71455277@news.bjoern.hoehrmann.de>...
> * Yves Orton wrote in comp.lang.perl.misc:
> >Just curious if anyone knows how to get access to the callers $_
> 
> As
> 
>   % perl -wmstrict -MData::Dumper -e "
>       sub x { print Dumper [ \@_, [caller], [\%::]] } sub t { x } t 10"
> 
> kindly demonstrates, you don't have access to the callers $_ unless you
> pass it to the called sub routine or store it somewhere where the sub
> routine has access to it.
> 
> But maybe I misunderstood your question...

Im not sure if you did or did not understand the question, as Im not
sure if  understand your answer.  :-)

Am I correct in thinking that %:: is the symbol table for main?

And what does this part of the output mean:
              '_' => *::_,
Is that not the $_ or is that a different $_

Frankly i'm now more confused than before.  

Thanks though, and I really would like to understand this a little
better.

yves


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

Date: 9 Aug 2001 11:30:00 -0700
From: demerphq@hotmail.com (Yves Orton)
Subject: Re: Sub that defaults to use $_ in callers context
Message-Id: <74f348f7.0108091030.2154ddd1@posting.google.com>

Joe Schaefer <joe+usenet@sunstarsys.com> wrote in message news:<m3lmktcp7s.fsf@mumonkan.sunstarsys.com>...
> Ilya Martynov <ilya@martynov.org> writes:
> 
> > Yves Orton wrote:
> > YO> Hi All,
> > YO> Just curious if anyone knows how to get access to the callers $_ so
> > YO> that it can be used as a default like so many keywords do?
> > 
> > YO> I tried to do it directly by
> > 
> > YO> sub func {
> > YO>     my $param=shift || $_;
> > YO> }
> > 
> > YO> but obviously this doesnt work as $_ is automatically localized to the
> > YO> sub (at least im pretty sure I read that somwhere.) Either way though
> > YO> it didnt work.

This is not my day.  I originally tried to do this a while back had
some kind of problem with it, decided it couldnt be done and gave up. 
Today I tried to get it to work and made a very embarrassing mistake
while doing so. :-(
Once I saw the replies I realized my shockingly obvious blunder.  All
I can say is that I am sooooo glad i didnt post my actual code but
rather something that resembeled it. (Now only if i had tested _that_
code.....)

My apologies for wasting all of your time.

> 
> $_ isn't auto-localized for subs; but @_ is (for obvious reasons).
> Assuming you're not trying to modify your sub's args,  AFAICT the 
> only problem with the above is that 
> 
>   $_ = 1;
>   func(0);
> 
> will set $param=1 in the sub. You probably want 0 here, so a 
> conditional like
> 
>   sub func {
>     my $param = @_ ? $_[0] : $_;
>     ...
>   }
> 
> or 
> 
>   sub func {
>     my ($param) = (@_, $_);
>     ...
>   }
> 
> might work better.  If there's still a problem, perhaps a more
> explicit example is appropriate.

That last one is cool.  Took me a minute to suss it out but very cool
indeed.

> 
> > At least in Perl 5.6.1 $_ is not automatically localized:
> 
> Right. TMTOWTDI, but for subs that *modify* their arguments uniformly, 
> and default to act on $_, I often use the following idiom:
> 
>   sub modify {
>     for (@_ ? @_ : $_) {
>         # code to modify $_ goes here
>     }
>   }
> 
> e.g.
> 
>   sub increment {
>      ++$_ for @_ ? @_ : $_;     
>   }
> 
> HTH

Interesting ideas.  I am going home now. Very embarrassed.

Once again apologies for wasting your time.

Yves


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

Date: Thu, 09 Aug 2001 21:11:59 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Sub that defaults to use $_ in callers context
Message-Id: <15v5ntsb0upmts9iu4ln73lmk1tlk24ugo@4ax.com>

Yves Orton wrote:

>Just curious if anyone knows how to get access to the callers $_ so
>that it can be used as a default like so many keywords do?
>
>I tried to do it directly by
>
>sub func {
>    my $param=shift || $_;
>}

That works. BTW, you're asking for the much despised dynamic scoping. I
think people are overreacting.

-- 
	Bart.


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

Date: Thu, 09 Aug 2001 20:03:32 GMT
From: Chuck Goldstein <chuckmg@acm.org>
Subject: traversing sub-directories
Message-Id: <3B72ECD8.44993047@acm.org>

Am new to Perl.  An old (more ways than one) awk-user.  I
would like to traverse subdirectories to any depth looking
fof files matching 1) a name pattern (e.g. html) and 2) a
pattern in text ... and then operate on said files.  Have a
book on Perl but can't locate a paridigm for such.  The
first part of my need is to replicate 'find' within Perl.
Of course, I could use 'find' with a -exec  ... but in back
of my memory someone once told me that Perl traversed
directory structures very well.  Is this true ... or is my
memory bad?

Chuck





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

Date: Thu, 09 Aug 2001 16:13:19 -0400
From: Eric <eric@mizuhocap.com>
Subject: Re: traversing sub-directories
Message-Id: <3B72EEDE.D1CB3901@mizuhocap.com>

Chuck,

   Your memory is just fine. Perl does have a pretty slick method for
incorporating find style searches. There should be a command on your
system called "find2perl" which will generate the propper perl code for
you. You can use it like this:
   find2perl /home/chuck -name "*.txt"
and the code will be dumped to the screen ( or you can redirect it with
> newperl.pl ).
As for searching for the text in the files .... someone else will have
to jump in here to help with that one.

Good luck,
Eric


Chuck Goldstein wrote:

> Am new to Perl.  An old (more ways than one) awk-user.  I
> would like to traverse subdirectories to any depth looking
> fof files matching 1) a name pattern (e.g. html) and 2) a
> pattern in text ... and then operate on said files.  Have a
> book on Perl but can't locate a paridigm for such.  The
> first part of my need is to replicate 'find' within Perl.
> Of course, I could use 'find' with a -exec  ... but in back
> of my memory someone once told me that Perl traversed
> directory structures very well.  Is this true ... or is my
> memory bad?
>
> Chuck



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

Date: Thu, 09 Aug 2001 20:26:01 -0000
From: Craig Berry <cberry@cinenet.net>
Subject: Re: traversing sub-directories
Message-Id: <Xns90F888A7F1B6Ecberrycinenetnet1@207.126.101.92>

Chuck Goldstein <chuckmg@acm.org> wrote in news:3B72ECD8.44993047@acm.org:

> Am new to Perl.  An old (more ways than one) awk-user.  I
> would like to traverse subdirectories to any depth looking
> fof files matching 1) a name pattern (e.g. html) and 2) a
> pattern in text ... and then operate on said files.  Have a
> book on Perl but can't locate a paridigm for such.  The
> first part of my need is to replicate 'find' within Perl.
> Of course, I could use 'find' with a -exec  ... but in back
> of my memory someone once told me that Perl traversed
> directory structures very well.  Is this true ... or is my
> memory bad?

perldoc File::Find

For simple cases, using shell's 'find' with an exec is often more 
straightforward.

-- 
Craig Berry <http://www.cinenet.net/~cberry/>
"That which is now known, was once only imagined." - William Blake



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

Date: Thu, 9 Aug 2001 21:31:20 +0200
From: "Steffen Müller" <tsee@gmx.net>
Subject: Update: (Perl) programming contest loosely based on the prisoners' dilemma
Message-Id: <9kuoa0$cqc$00$1@news.t-online.com>

Here's the follow-up I promised to translate:

This is just an idea, not the finished task description:

For every (contest) entry, an entity in a two-dimensional matrix that is
limited in size but border-less (picture a sphere's surface area). Every
entity can 'see' / analyse only thus far - say 2 units in each direction. If
there are opponents within its vicinity (sp?), the entry will see them. (A
matrix of 1's and 0's is passed to the function/object/module/package, you
name it.)
Each turn, each entry can move one space in any direction. Each entry has a
certain 'size' to begin with. Call it life points or population. If two
entries meet, they confront each other by the rules of the iterated PD (or
similar rules). Say 50 iterations. The winner grows, the looser shrinks.
This has something of an evolution.

More interesting would be if there weren't 'health points', but rather a
number of separated entities for each entry that are randomly distributed in
the matrix. If an entity looses a confrontation, it gets assimilated by the
winning strategy. If two instances of the same entry meet, nothing happens
(they are allies). In this modification, the entries would be able to
distinguish between allies and hostiles.

Problem: What happens when three entities meet at one place & time?

Anyway, as I stated before, this is just an idea for a task. I already feel
bad for posting so much off-topic that I am not going to continue ranting
about my ideas for an implementation.

That's why eMails are still very welcome.

Interested? Let me know, please.

Thanks,
Steffen






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

Date: Thu, 09 Aug 2001 14:23:21 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Using TRUE constant in IF expression??
Message-Id: <3B72E329.66605699@home.com>

"Carlos C. Gonzalez" wrote:
> 
> In another thread Tassilo and Anno brought up the dangers of using
> TRUE and FALSE constants in Perl.
> 
[snip sound reasons for not defining true/false constants]
> 
> In light of what Anno stated above can someone suggest a better way
> to write the if statement in the code below without having to say if
> (valid($string) eq "1") type of thing?  Writing it as "if
> (valid($string))" is much clearer IMO, especially to non-Perl
> programmers looking at my code.  But it would seem that this is
> problematic since any string value would evaluate to TRUE.

Why do you think this is a problem? You control how valid() is defined;
you can make it return anything you want. If all you care about is a
boolean then the actual values are pretty much irrelevant. Whether you
return 1, 'TRUE', or 'Hippopotamus' for a true value is up to you.

-mjc


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

Date: Thu, 09 Aug 2001 14:04:29 +0500
From: Robert Sherman <rsherman@ce.gatech.edu>
Subject: Re: Where can I learn to do this?
Message-Id: <3B72521D.B7B19FC5@ce.gatech.edu>

Stephen Edelblut wrote:
> 
> I know nothing about Perl/CGI. What I wanted to do is have a form on my site
> that had two text field and  send button. One text-box would be 'name' and
> the seond would be 'message'. What I want it to do is send the contents to a
> *.txt file on the server as 'name':'message' line-by-line in the text file.
> Where can I learn how to do this. Every example I've found involved very
> complex forms and I could never figure out if they saved the info to a file
> on the server. Thanks

read up on the CGI module...or not...(some people hate it, some love it,
but it is easy to use)

on cpan: http://search.cpan.org/search?dist=CGI.pm


the perl part would look kinda like (if you were to use CGI.pm):

---------------------------------------
#!/usr/bin/perl

#create CGI object
use CGI;
my $q = new CGI;

#get values from forms
my $name = $q->param("name");
my $message = $q->param("message");

#print values to file
open FILE, ">>file.txt";
print FILE "$name : $message\n";
close FILE;

#send confirmation to browser
print 	$q->header("text/html"),
     	$q->start_html("results"),
	$q->p("message stored"),
	$q->end_html;

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

test the code, add some error-handling...of course, to do this right,
you will probably want to lock the file and such, but the above should
give you a vague framework to start from.


go to www.htmlgoodies.com or something to learn how to create a
form...or buy webmaster in a nutshell from o'reilly...


--
robert sherman
css, cee
georgia institute of technology
atlanta, ga, usa


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

Date: Thu, 09 Aug 2001 15:39:25 -0400
From: Lou Moran <lmoran@wtsg.com>
Subject: Why is $i so popular?
Message-Id: <oep5ntcvi3fduk1dn895kdv3l60ks7gsjf@4ax.com>

Why is $i such a popular variable?

Is there any significance to it, or is it a foo bar sort of thing?
Just wondering.




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

Date: Thu, 09 Aug 2001 19:48:56 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Why is $i so popular?
Message-Id: <x74rrh9fpz.fsf@home.sysarch.com>

>>>>> "LM" == Lou Moran <lmoran@wtsg.com> writes:

  LM> Why is $i such a popular variable?
  LM> Is there any significance to it, or is it a foo bar sort of thing?
  LM> Just wondering.

sorta like foo but with a real technical origin. i stands for integer
and also iterator and was used way back in '57 with the first fortran as
loop variables. fortran has a bizarre feature, if you didn't declare a
variable, its type (integer or float) would be determined by the first
letter of the variable name. so it if began with I-N (IIRC) it was an
integer, otherwise a float. this led to the common use of I, J, K,
etc. as iterator or coordinate vars which lives on today.

and before that, it was commonly used in math for coordinates (i,j),
etc. so it is a very ancient usage.

but i doubt i will ever write code with $i. i just like more realistic
iterator names and i don't do much matrix math where it would be ok IMO.

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: Fri, 10 Aug 2001 06:24:09 +1000
From: "Sisyphus" <kalinabears@hdc.com.au>
Subject: Re: Win32::eventlog --  Help!?!?!
Message-Id: <997424942.933194@clover.origin.net.au>


"JB Lewis" <jblewisoh@yahoo.com> wrote in message
news:9387eabc.0108090655.2732955c@posting.google.com...
> Has anybody been able to actually retrieve the message text of an
> event log item?
>
> Here's the code I'm trying to use, but it always comes back that the
> GetMessageText failed.
>
> #!/perl/bin/perl.exe
>
> use strict;
> use warnings;
>
> use Win32::EventLog;
> use Time::Local 'timelocal';
>
> $Win32::EventLog::GetMessageText = 1;
>
> my ($handle, $recs, $base, $hashRef);
> my $x = 0;
>
> $handle=Win32::EventLog->new("System", "\\\\stp-ras1")
>         or die "Can't open Application EventLog\n";
> $handle->GetNumber($recs)
>         or die "Can't get number of EventLog records\n";
> print "$recs\n";
> $handle->GetOldest($base)
>         or die "Can't get number of oldest EventLog record\n";
> print "$base\n";
> #while ($x < $recs) {
> while ($x < 10) {
>         $handle->Read(EVENTLOG_FORWARDS_READ|EVENTLOG_SEEK_READ,
>                                   $base+$x,
>                                   $hashRef)
>                 or die "Can't read EventLog entry #$x\n";
> print "$hashRef->{Category}\n";
>         if ($hashRef->{Source} eq "Router") {
>                 Win32::EventLog::GetMessageText($hashRef) || die
> "Unable to get message from event $x\n";
>                 print "Entry $x: $hashRef->{Message}\n";
>         }
>         $x++;
>  }
>
> Is it something I'm doing wrong, or not doing at all, or what?
>
> TIA
> JBLewis

Hi,

Seems to work ok for me. I can run your script, as is, on my machine without
any problems ( though I didn't actually verify this by copy and pasting it).

First, try checking it on your local machine:
$handle=Win32::EventLog->new("System")

If it still doesn't work, try deleting:
Win32::EventLog::GetMessageText($hashRef) || die
 "Unable to get message from event $x\n";

If I've got it right, all you need is the line:
$Win32::EventLog::GetMessageText = 1;

You should then find that the 'Message' key is set, and printed out by:
print "Entry $x: $hashRef->{Message}\n";

If you can get it working on the local machine, then the problem must lie in
access to the remote machine.

Cheers,
Rob










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

Date: Thu, 09 Aug 2001 19:27:51 GMT
From: "Sean Hamilton" <sh@planetquake.com>
Subject: Re: Write to middle of file
Message-Id: <XuBc7.41259$b_3.3361909@news0.telusplanet.net>

> Don't delete A.  If your OS can't atomically to rename B to A
> implictly deleting the old A in the process (but allowing any
> processes already reading the old A to continue to do so) then that's

Developing for FreeBSD... time for a little experimentation I guess.

sh




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

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


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