[29872] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1115 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Dec 14 00:09:43 2007

Date: Thu, 13 Dec 2007 21:09:07 -0800 (PST)
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, 13 Dec 2007     Volume: 11 Number: 1115

Today's topics:
    Re: (1)[0] ok but not 1[0] <sensorflo@gmail.com>
    Re: (1)[0] ok but not 1[0] <ben@morrow.me.uk>
    Re: FAQ 4.58 How can I know how many entries are in a h <xueweizhong@gmail.com>
    Re: File::Find doesn't go down thru directory <cmic@caramail.com>
        How to write a "caller-inspecting" module? <socyl@987jk.com.invalid>
        most elegant way to split text file randomly into n par <markus.dehmann@gmail.com>
    Re: Need help integrating a perl command into a shell s <tadmc@seesig.invalid>
    Re: Newbie help with telephone number and date formatti <some@body.com>
        Question about "undef'ing" variables <some@body.com>
    Re: Question about "undef'ing" variables <krahnj@telus.net>
        question..? <hpobox@gmail.com>
    Re: split is not convinient <1usa@llenroc.ude.invalid>
    Re: split is not convinient <tadmc@seesig.invalid>
    Re: split is not convinient <tadmc@seesig.invalid>
    Re: split is not convinient <tadmc@seesig.invalid>
    Re: split is not convinient <krahnj@telus.net>
    Re: Using the DBI to connect to an Oracle server w/o co <google@hoodfamily.org>
    Re: Using the DBI to connect to an Oracle server w/o co <rkb@i.frys.com>
    Re: Using the DBI to connect to an Oracle server w/o co xhoster@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 13 Dec 2007 15:25:26 -0800 (PST)
From: Florian Kaufmann <sensorflo@gmail.com>
Subject: Re: (1)[0] ok but not 1[0]
Message-Id: <f759e0d7-2186-4856-9604-cba97d0d8956@d4g2000prg.googlegroups.com>

> then why even do it if it will have so many mistakes? it won't even help
> you in your goal to learn perl. and publishing it with a disclaimer that

Because even now in the early stages I profit from my document. As
said, I learn things by writing down what I read, summarize in my own
words.

I can look up things faster because its much denser, my documents
internal logic and organization matches more the way I am thinking
then the Perl documentations I have. In those, a particular idea/rule
is seldom in one single place. Instead, its spread over a couple of
places. There are too little clear statements which rules apply to
exactly which things - often these docs try to specify rules by using
isolated examples. So its hard for me to get to know the general/basic
rules.

Say I want to learn differentation. Then I prefer to learn a pretty
basic rule like
f'(x)=lim( h -> 0 ; (f(x+h)-f(x)) / h )
http://en.wikipedia.org/wiki/Derivative#Definition_via_difference_quotients

Maybe along with a good visualization in form of a diagram. Maybe also
some very often used basic rules, knowing that they derive from the
rule above and don't really introduce new ideas/rules
(f*g)' = f*g' + f'*g
(f+g)' = f' + g'
 ... http://en.wikipedia.org/wiki/Derivative#Rules_for_finding_the_derivative

But surely I don't like if the book doesn't tell me about the above
formulas and just tells me that I have to know the following rules to
understand differentiation and be able to differentiate.
(x**a)' = a*x**(a-1)
sqrt(x)' = 1 / ( 2 * sqrt(x) )
 ... etc etc ... a very long list. http://en.wikipedia.org/wiki/Table_of_derivatives

These rules are just isolated examples of the basic rules. If I know
the basic rules well, I can deduce these rules by myself.

> it has mistakes is even worse. just consider it a private document for
> yourself and keep it that way. if you want to get feedback you could
> publish the url but make sure it says this is not considered a
> professional work.

Right!


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

Date: Thu, 13 Dec 2007 23:56:46 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: (1)[0] ok but not 1[0]
Message-Id: <uet835-vgm2.ln1@osiris.mauzo.dyndns.org>


Quoth "jl_post@hotmail.com" <jl_post@hotmail.com>:
> On Dec 12, 6:28 am, Florian Kaufmann <sensor...@gmail.com> wrote:
> >
> > The following two are equivalent
> > print 1,2,3;
> > print (1,2,3);
> 
> But the following two lines are not equivalent:
> 
>    my @a = (1,2,3);
>    my @a = 1,2,3;
> 
> Parentheses sometimes make a big difference, because they can change
> the order of when operators get evaluated.

<sigh>

In both
    print (1, 2, 3);
and
    (1,2,3)[1]
(and
    if (1==2) { }
for that matter) the parens have a quite different function from in
    @a = (1, 2, 3);
 . In the last case, they are indeed just for precedence; in the others,
they are part of the syntax, and omitting them does more than simply
change the evaluation order.

Ben



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

Date: Thu, 13 Dec 2007 18:59:21 -0800 (PST)
From: Todd <xueweizhong@gmail.com>
Subject: Re: FAQ 4.58 How can I know how many entries are in a hash?
Message-Id: <c57314b4-5047-48d3-b733-2557b915b8d1@e6g2000prf.googlegroups.com>



xhos...@gmail.com wrote:
> $ perl -le '$x{$_}=1 foreach 1..1e6; print scalar %x; print scalar keys %x'
>
> 644020/1048576
> 1000000

Really cool discovery.

-Todd


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

Date: Thu, 13 Dec 2007 15:47:58 -0800 (PST)
From: cmic <cmic@caramail.com>
Subject: Re: File::Find doesn't go down thru directory
Message-Id: <90de142f-39b0-4cb2-bf31-496cd7f725aa@d27g2000prf.googlegroups.com>

On 13 d=E9c, 23:58, mer...@stonehenge.com (Randal L. Schwartz) wrote:
=2E.......
>
> If you're *in* "wanted", use $_.
> If you're *after* "wanted", use $File::Find::name.
>
> Don't confuse the two.  They both have their purpose.

OK I dig it now
Thx Randal. Sorry for the unformatted post..
--
cmic

>
> print "Just another Perl hacker,"; # the original
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 009=
5
> <mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl traini=
ng!



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

Date: Fri, 14 Dec 2007 01:31:17 +0000 (UTC)
From: kj <socyl@987jk.com.invalid>
Subject: How to write a "caller-inspecting" module?
Message-Id: <fjsmd4$3do$1@reader1.panix.com>




Is it possible to write a module Foo such that if one wrote

  package Bar;
  use Foo;

  # rest of Bar defined here

  sub last_sub_in_Bar { do_something() }

  last_top_level_statement_in_Bar();

  # suitable moment!

  1; # end of Bar

then, at some *suitable moment* Foo would automatically inspect
the Bar package (the "caller") and do something (e.g. add some
methods to Bar) on the basis of this inspection.  The "suitable
moment" I'm referring to is right after last_sub_in_Bar has been
defined and "last_top_level_statement_in_Bar();" has been executed.

After playing around endlessly with CHECKs and INITs, I still have
not come up with a solution to this problem.  The closest to a
solution I can come up with is to move the "use Foo" statement to
right after last_top_level_statement_in_Bar().

Is what I'm trying to do possible?

TIA!

kj
-- 
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.


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

Date: Thu, 13 Dec 2007 21:03:58 -0800 (PST)
From: Markus Dehmann <markus.dehmann@gmail.com>
Subject: most elegant way to split text file randomly into n parts?
Message-Id: <1558c20a-d295-4bc9-8d0c-e2c4023605c7@i29g2000prf.googlegroups.com>

What is the most elegant way to split a text file randomly into n
parts? If the original file contains m lines, the n new file should
each contain about m/n lines, chosen uniformly at random.

Leaving the actual handling of files aside here is a naive solution:

my $n = 10;
foreach my $line (0..9999){
    my $r = rand;
    for(my $i=$n; $i>=0; --$i){
        if($r > $i/$n){
            print "Line $line: Print to file $i\n";
            last;
        }
    }
}

Are there other ways to do it? This seems like a very typical perl
problem, so it would be interesting to see what solutions others come
up with!

-Markus


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

Date: Fri, 14 Dec 2007 00:17:01 GMT
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Need help integrating a perl command into a shell script.
Message-Id: <slrnfm3ifs.gl.tadmc@tadmc30.sbcglobal.net>

Daryl Rose <rosede12@gmail.com> wrote:

> they have a single line in them that define a specific
> path.  i.e. FILEDIR="/opt/<subdir>/file"
>
> This path needs to be changed.  The opt directory is the only
> consistent within this path.  The name of the subdir, and the file
> name change.


Then you meant "e.g." instead of "i.e.".

    http://www.wsu.edu/~brians/errors/e.g.html



> perl -ip -e 's/SEARCH/REPLACE/' $var1/$var2
         ^
         ^

That code will not change the contents of any files (because
it does not make any output).

   perl -i -p -e 's/SEARCH/REPLACE/' $var1/$var2



-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Thu, 13 Dec 2007 22:31:51 -0500
From: somebody <some@body.com>
Subject: Re: Newbie help with telephone number and date formatting
Message-Id: <ZeKdnehYf4W6ZPzanZ2dnUVZ_gqdnZ2d@comcast.com>

On Wed, 12 Dec 2007 23:33:47 +0000, Joost Diepenmaat wrote:
artial strings. You can use regular expressions for that.
> 
> $phone_number =~ s/(\d{3})(\d{3})(\d+)/$1-$2-$3/;
> 
> Joost.


Thanks, this was very close to what I needed to do.



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

Date: Thu, 13 Dec 2007 22:40:23 -0500
From: somebody <some@body.com>
Subject: Question about "undef'ing" variables
Message-Id: <iL2dncniwMa6ZvzanZ2dnUVZ_s6mnZ2d@comcast.com>

In the code snippet below, I process each line of a file using the split
command.  After the variables $a, $b, $c, and $d are used, I need to
ensure they're "undef'd" before processing the next line, so the value
assigned from the previous line is not defined.  So the line
($a, $b, $c, $d) = (0) x 4; resets these variables.  (In reality there
are more variables). This seems to slow down my program significantly.  Is
there a better way to re-initialize these variables?  Maybe define them
local to the sub instead of globally?

-Thanks


use vars qw($a $b $c $d);


sub MySub
{

 open(FILE,"<$myfile") or die "Can't open $myfile: $!";

  while ( <FILE> ) {

    chomp;

    @myline = split /\'/;

    #Need to undef these vars after each record.
    ($a, $b, $c, $d) = (0) x 4;


    Do stuff here with $a, $b, etc.




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

Date: Fri, 14 Dec 2007 04:28:15 GMT
From: "John W. Krahn" <krahnj@telus.net>
Subject: Re: Question about "undef'ing" variables
Message-Id: <4762065C.72EE48B1@telus.net>

somebody wrote:
> 
> In the code snippet below, I process each line of a file using the split
> command.  After the variables $a, $b, $c, and $d are used, I need to
> ensure they're "undef'd" before processing the next line, so the value
> assigned from the previous line is not defined.  So the line
> ($a, $b, $c, $d) = (0) x 4; resets these variables.  (In reality there
> are more variables). This seems to slow down my program significantly.  Is
> there a better way to re-initialize these variables?  Maybe define them
> local to the sub instead of globally?

perldoc -f my
perldoc perlsub
http://perl.plover.com/FAQs/Namespaces.html



John
-- 
use Perl;
program
fulfillment


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

Date: Thu, 13 Dec 2007 20:40:36 -0800 (PST)
From: malboros <hpobox@gmail.com>
Subject: question..?
Message-Id: <ce532a29-cc78-4b4c-a134-b3f284d00c1c@d21g2000prf.googlegroups.com>

X-No-Archive:


Hi all happy holidays .....
well i hope this message  going in to the right newsgroups ......
i just start learning Perl .... and i would like to know to know a
free Perl editor !!!!  easy to use ...
thanks in advance !!!!!!


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

Date: Fri, 14 Dec 2007 00:07:07 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: split is not convinient
Message-Id: <Xns9A05C27D29E81asu1cornelledu@127.0.0.1>

Mark Seger <Mark.Seger@hp.com> wrote in news:4761B241.6050108@hp.com:

> thanks, but I thought someone had said earlier that there was a 
> different behavior using $_ and explicitly using my own variable and I
> still don't get that as it seems to me they're one in the same, unless
> of course $_ has special properties I don't know about.  and that's 
> always possible...

That was claimed by Joost Diepenmaat <joost@zeekat.nl> in
news:4761409b$0$16792$e4fe514c@dreader27.news.xs4all.nl

You misinterpreted my refutation by example in 
Message-ID: <Xns9A056803382C5asu1cornelledu@127.0.0.1>

As an aside, 

"John W. Krahn" <krahnj@telus.net> wrote in news:47617F5B.2644EC69
@telus.net:

> Some "text" files may not contain a final newline and if the last
> character is "0" the last line read will be false but defined.  If the
> Input Record Separator is set to the length 1 ($/ = \1;) then any "0"
> character read in will be false but defined.

Strictly speaking, if the final newline is omitted, the file is not a 
text file, but I do understand that we encounter the missing final 
newline in files that are otherwise intended to be text files.

Sinan 


-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>



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

Date: Fri, 14 Dec 2007 00:17:00 GMT
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: split is not convinient
Message-Id: <slrnfm3hfc.gl.tadmc@tadmc30.sbcglobal.net>

Mark Seger <Mark.Seger@hp.com> wrote:

> thanks, but I thought someone had said earlier that there was a 
> different behavior using $_ and explicitly using my own variable 


$_ is a package (global) variable.

global variables are "bad", in general.


> and I 
> still don't get that as it seems to me they're one in the same, 


Perl has two completely separate sets of variables, package variables
and lexical variables.

$_ is in one set, and a my()'d variable is in the other set.


See:

      "Coping with Scoping":

      http://perl.plover.com/FAQs/Namespaces.html


> unless 
> of course $_ has special properties I don't know about.


See above.  :-)


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Fri, 14 Dec 2007 00:17:01 GMT
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: split is not convinient
Message-Id: <slrnfm3gpg.gl.tadmc@tadmc30.sbcglobal.net>

Joost Diepenmaat <joost@zeekat.nl> wrote:
> On Thu, 13 Dec 2007 06:09:38 -0600, Tad J McClellan wrote:

>> Actually, it should most likely be:
>> 
>>     while ( my $var = <> )
>> 
>
> As has been mentioned somewhere above, that's simply incorrect code in 
> most circumstances. 


In what way is it incorrect?


> while (<handle>) { ... } is a special case and if you 
> really must use a named variable the construct should be
>
> while (defined(my $var = <>)) { ... }


Perl adds the defined() test for you whether you type it or not.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Fri, 14 Dec 2007 00:17:00 GMT
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: split is not convinient
Message-Id: <slrnfm3glq.gl.tadmc@tadmc30.sbcglobal.net>

Mark Seger <Mark.Seger@hp.com> wrote:

> 0[root@cag-dl380-01 collectl]# cat test.pl
    ^^^^
    ^^^^

I hope you are not really trying out Perl snippets as the superuser...


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Fri, 14 Dec 2007 04:15:37 GMT
From: "John W. Krahn" <krahnj@telus.net>
Subject: Re: split is not convinient
Message-Id: <47620366.FD665D66@telus.net>

"A. Sinan Unur" wrote:
> 
> As an aside,
> 
> "John W. Krahn" <krahnj@telus.net> wrote in news:47617F5B.2644EC69
> @telus.net:
> 
> > Some "text" files may not contain a final newline and if the last
> > character is "0" the last line read will be false but defined.  If the
> > Input Record Separator is set to the length 1 ($/ = \1;) then any "0"
> > character read in will be false but defined.
> 
> Strictly speaking, if the final newline is omitted, the file is not a
> text file, but I do understand that we encounter the missing final
> newline in files that are otherwise intended to be text files.

Which is why I put "text" inside quotes.  (Its hard to do air-quotes on
usenet.)

:-)

John
-- 
use Perl;
program
fulfillment


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

Date: Thu, 13 Dec 2007 16:29:18 -0800 (PST)
From: Tim <google@hoodfamily.org>
Subject: Re: Using the DBI to connect to an Oracle server w/o connecting to a  database
Message-Id: <b5db2ff3-3b39-4fd5-a9fb-d07569962093@i29g2000prf.googlegroups.com>

On Dec 12, 4:01 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Tim <goo...@hoodfamily.org>:
>
> > I've searched for the answer to this problem and have not seen it
> > (which suggests what the answer might be ;-) ). Is it possible to use
> > DBI (and DBD::Oracle) to connect to an Oracle server without
> > connecting to a database? This would be the DBI equivalent of:
>
> > sqlplus /nolog
> > SQL> connect user/passwd@database;
>
> Is this not connecting to a database? I think you will have to explain a
> bit more about what you are actually trying to acheive.
>
> Ben

Yes, this connects to the database, just not from the command-line.
Functionally, in perl what I wanted to do was the DBI->connect... to
connect to the server and then in a separate call, connect to the
database itself.

The reason boils down to the fact that Sybase doesn't allow connecting
to a database from the connect call, but requires a separate
subsequent call. I was hoping to write some perl code portable enough
that when the time comes to switch this particular database from
Sybase to Oracle I wouldn't have to modify perl code.


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

Date: Thu, 13 Dec 2007 17:57:02 -0800 (PST)
From: Ron Bergin <rkb@i.frys.com>
Subject: Re: Using the DBI to connect to an Oracle server w/o connecting to a  database
Message-Id: <a027c9c8-a273-40f1-8d35-a589dd5f1aa7@i29g2000prf.googlegroups.com>

On Dec 13, 4:29 pm, Tim <goo...@hoodfamily.org> wrote:
> On Dec 12, 4:01 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>
> > Quoth Tim <goo...@hoodfamily.org>:
>
> > > I've searched for the answer to this problem and have not seen it
> > > (which suggests what the answer might be ;-) ). Is it possible to use
> > > DBI (and DBD::Oracle) to connect to an Oracle server without
> > > connecting to a database? This would be the DBI equivalent of:
>
> > > sqlplus /nolog
> > > SQL> connect user/passwd@database;
>
> > Is this not connecting to a database? I think you will have to explain a
> > bit more about what you are actually trying to acheive.
>
> > Ben
>
> Yes, this connects to the database, just not from the command-line.
> Functionally, in perl what I wanted to do was the DBI->connect... to
> connect to the server and then in a separate call, connect to the
> database itself.
>
> The reason boils down to the fact that Sybase doesn't allow connecting
> to a database from the connect call, but requires a separate
> subsequent call. I was hoping to write some perl code portable enough
> that when the time comes to switch this particular database from
> Sybase to Oracle I wouldn't have to modify perl code.

You, apparently, haven't tried using the DBI (with DBD::Sybase) to
connect to your database.

http://search.cpan.org/~mewp/DBD-Sybase-1.08/Sybase.pm


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

Date: 14 Dec 2007 01:09:08 GMT
From: xhoster@gmail.com
Subject: Re: Using the DBI to connect to an Oracle server w/o connecting to a database
Message-Id: <20071213200911.615$0D@newsreader.com>

Tim <google@hoodfamily.org> wrote:
> On Dec 12, 4:01 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth Tim <goo...@hoodfamily.org>:
> >
> > > I've searched for the answer to this problem and have not seen it
> > > (which suggests what the answer might be ;-) ). Is it possible to use
> > > DBI (and DBD::Oracle) to connect to an Oracle server without
> > > connecting to a database? This would be the DBI equivalent of:
> >
> > > sqlplus /nolog
> > > SQL> connect user/passwd@database;
> >
> > Is this not connecting to a database? I think you will have to explain
> > a bit more about what you are actually trying to acheive.
> >
> > Ben
>
> Yes, this connects to the database, just not from the command-line.
> Functionally, in perl what I wanted to do was the DBI->connect... to
> connect to the server and then in a separate call, connect to the
> database itself.
>
> The reason boils down to the fact that Sybase doesn't allow connecting
> to a database from the connect call, but requires a separate
> subsequent call.

In DBI?  Can you show the code you use to connect to Sybase using DBI?


Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 1115
***************************************


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