[21995] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4217 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 4 18:11:09 2002

Date: Wed, 4 Dec 2002 15:10:15 -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           Wed, 4 Dec 2002     Volume: 10 Number: 4217

Today's topics:
        read from a process <sunil_franklin@hotmail.com>
    Re: read from a process <nobull@mail.com>
    Re: read from a process <goldbb2@earthlink.net>
    Re: read from a process <nobull@mail.com>
        read variables and values in them at runtime. <sunil_franklin@hotmail.com>
    Re: read variables and values in them at runtime. <jurgenex@hotmail.com>
    Re: read variables and values in them at runtime. <nobull@mail.com>
    Re: read variables and values in them at runtime. (Malcolm Dew-Jones)
    Re: read variables and values in them at runtime. (Malcolm Dew-Jones)
    Re: RegExp: Capturing parentheses in quantified sub-pat (Tad McClellan)
    Re: RegExp: Capturing parentheses in quantified sub-pat (Tad McClellan)
    Re: Warnings when creating an array of hashes <krahnj@acm.org>
    Re: Win32 and *NIX, cryptographically secure random num (Sisyphus)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 5 Dec 2002 00:58:40 +0530
From: "Sunil" <sunil_franklin@hotmail.com>
Subject: read from a process
Message-Id: <kKsH9.8$T1.135@news.oracle.com>

All,
    I have got the following routine which take a arbitrary sql script and
executes it after connecting to a oracle DB.
    I would like the results of this script to be available in my program
(may be in an array). is this possible. How can I read from this?
    I can redirect the output to a file by using
            open SQLP, "|sqlplus -s /nolog > output.log" or die "cannot open
sqlplus\n";
    But I may not always be having write permissions on the dir.

    Any Ideas?

Thanks,
Sunil.



sub doScript($$$$)
{
    my ($scriptName,$userName,$password,$outputFileName) = @_;

    open SQLP, "|sqlplus -s /nolog " or die "cannot open sqlplus\n";
    print SQLP "connect $userName/$password \n";
    print SQLP "\@$scriptName; \n";
    close SQLP;

    my $status = $CHILD_ERROR & 127;
    if ($status)
    {
        print(" Error Executing Script: $scriptName");
        die;
    }
    else
    {
        return 1;
    }
}







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

Date: 04 Dec 2002 19:42:47 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: read from a process
Message-Id: <u9hedtvadk.fsf@wcl-l.bham.ac.uk>

"Sunil" <sunil_franklin@hotmail.com> writes:

> Subject: read from a process

Actually a better way to phrase what you are tring to ask would be:

 "How can I open a pipe both to and from a command?"

And that, indeed, is exactly how it is phrased when this question
appears in the FAQ.

Unfortunately the FAQ is not necessarily the full answer in this case.

>     I would like the results of this script to be available in my program
> (may be in an array). is this possible. How can I read from this?
>     I can redirect the output to a file by using
>             open SQLP, "|sqlplus -s /nolog > output.log" or die "cannot open
> sqlplus\n";
>     But I may not always be having write permissions on the dir.
> 
>     Any Ideas?

Redirect the output to somewhere to which you will always have write
permissions.

One neat way to do this is to open a temporary file with IO::File->new_tmpfile

my $tempfile =  IO::File->new_tmpfile or die $!;
open SQLP, "|sqlplus -s /nolog >&" . fileno($tempfile) or die $!;
seek($tempfile,0,0);


-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Wed, 04 Dec 2002 15:33:37 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: read from a process
Message-Id: <3DEE66A1.B7B0238@earthlink.net>

Brian McCauley wrote:
[snip]
> my $tempfile =  IO::File->new_tmpfile or die $!;
> open SQLP, "|sqlplus -s /nolog >&" . fileno($tempfile) or die $!;
> seek($tempfile,0,0);

When you pipe-open to a string with metacharacters (like ">&") in it,
perl uses /bin/sh or the equivilant to run the command.

If the handle created by new_tmpfile has F_CLOEXEC set on it (the
default for all handles whose filedescriptor is greater than $^F), then
the handle will closed by the exec() that starts the shell, and not be
visible to it.

In addition to that, due to the nature of dup()ed filedescriptors, it is
unsafe to seek() on $tempfile while the other process is writing to it.
So don't do that until the other process exits.

So, to make this work, you need to do:

   my $tempfile = do {
      local $^F = 0x7FFFFFFF;
      IO::File->new_tmpfile
   } or die $!;
   open SQLP, "|sqlplus -s /nolog >&" . fileno($tempfile) or die $!;
   # do all of your writing to SQLP
   close(SQLP);
   seek($tempfile,0,0);

If you used open2, you don't have to worry about $^F and close-on-exec,
but would merely have to do:

   my $tempfile = IO::File->new_tmpfile or die $!;
   my $pid = open2(\*SQLP, ">&".fileno($tempfile),
      qw(sqlplus -s /nolog) );
   # print stuff to SQLP
   close(SQLP); waitpid($pid, 0);
   seek($tempfile,0,0);

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


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

Date: 04 Dec 2002 21:15:44 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: read from a process
Message-Id: <u93cpdv62n.fsf@wcl-l.bham.ac.uk>

Benjamin Goldberg <goldbb2@earthlink.net> writes:

> Brian McCauley wrote:
> [snip]
> > my $tempfile =  IO::File->new_tmpfile or die $!;
> > open SQLP, "|sqlplus -s /nolog >&" . fileno($tempfile) or die $!;
> > seek($tempfile,0,0);
> 
> When you pipe-open to a string with metacharacters (like ">&") in it,
> perl uses /bin/sh or the equivilant to run the command.
> 
> If the handle created by new_tmpfile has F_CLOEXEC set on it (the
> default for all handles whose filedescriptor is greater than $^F), then
> the handle will closed by the exec() that starts the shell, and not be
> visible to it.

You are of course correct.  I foolishly simplified the code I actually
use without checking I'd not broken.  Considering the number of times
I council others against this I should be placed in the virtual stocks
and pelted with virtual rotten fruit.  In mittigation a dental abcess
is currently poisoning my brain.

> In addition to that, due to the nature of dup()ed filedescriptors, it is
> unsafe to seek() on $tempfile while the other process is writing to it.
> So don't do that until the other process exits.

This was a simpler mistake.  I forgot to include the close() in my
example.
 
> So, to make this work, you need to do:

  [ snip valid correction ]

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Thu, 5 Dec 2002 01:06:35 +0530
From: "Sunil" <sunil_franklin@hotmail.com>
Subject: read variables and values in them at runtime.
Message-Id: <ORsH9.9$T1.58@news.oracle.com>

I have a simple perl package.
I would like to write a routine (dump_vars() ) which will print all the
string variables in the package and their corresponding values at runtime.
Is there any way I can achieve this?


Thanks,
Sunil.









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

Date: Wed, 04 Dec 2002 19:51:30 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: read variables and values in them at runtime.
Message-Id: <61tH9.35241$ic6.15629@nwrddc01.gnilink.net>

Sunil wrote:
> I have a simple perl package.
> I would like to write a routine (dump_vars() ) which will print all
> the string variables in the package and their corresponding values at
> runtime. Is there any way I can achieve this?

What don't you like in Data::Dumper?

jue




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

Date: 04 Dec 2002 19:57:24 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: read variables and values in them at runtime.
Message-Id: <u9d6ohv9p7.fsf@wcl-l.bham.ac.uk>

"Sunil" <sunil_franklin@hotmail.com> writes:

> I have a simple perl package.
> I would like to write a routine (dump_vars() ) which will print all the
> string variables in the package and their corresponding values at runtime.
> Is there any way I can achieve this?

The symbol table of package FOO is a hash called %FOO:: - yes it
really does end in a double-colon.  It contains wierd things called
typeglobs.

You can get a list of bare symbols in FOO with keys(%FOO::).

Suppose $sym='foo'

The expresion do { no strict 'refs'; *{"FOO::$sym"}{HASH} } will return a
reference to %FOO::foo if there is such a variable and undef
otherwise.

Similarly for other builtin types execpt that, unfortunately, for
SCALAR it aways returns a reference.

The documentation of typeglobs and symbol tables is rather scattered.
Some is in perldata and some in perlmod (and some probably in other
places).

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 4 Dec 2002 12:00:43 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: read variables and values in them at runtime.
Message-Id: <3dee5eeb@news.victoria.tc.ca>

Sunil (sunil_franklin@hotmail.com) wrote:
: I have a simple perl package.
: I would like to write a routine (dump_vars() ) which will print all the
: string variables in the package and their corresponding values at runtime.
: Is there any way I can achieve this?

I would first try

	perldoc Data::Dumper



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

Date: 4 Dec 2002 12:21:47 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: read variables and values in them at runtime.
Message-Id: <3dee63db@news.victoria.tc.ca>

Jürgen Exner (jurgenex@hotmail.com) wrote:
: Sunil wrote:
: > I have a simple perl package.
: > I would like to write a routine (dump_vars() ) which will print all
: > the string variables in the package and their corresponding values at
: > runtime. Is there any way I can achieve this?

: What don't you like in Data::Dumper?

I think his issue is how to find what variables to dump in the first
place.

Use the %package:: variable to find the names of package variables. 

Example of dumping symbol names in the package main

	$ perl -MData::Dumper
	print Dumper(\%main::);

Dumper can do many things, so it may well be worth figuring out how to use
it to extract the things you want.

You can access the %package:: hash directly to check each variable name,
and its value.  The below uses a symbolic ref, which could be avoided or
controlled in various ways to allow "use strict" in the rest of a program. 


	$a_string = "This is a sample value which will be seen";

	my $not_seen = "This symbol and value will not be seen"; 

	foreach $name (keys %main::)
	{ print $name , " => " ;
	  print ${"main::$name"};
	  print "\n";
	}




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

Date: Wed, 4 Dec 2002 16:16:15 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: RegExp: Capturing parentheses in quantified sub-patterns -- how is it supposed to work? (ADDITION)
Message-Id: <slrnausvlf.4mh.tadmc@magna.augustmail.com>

Julian Mehnle <julian@mehnle.net> wrote:
> Julian Mehnle <julian@mehnle.net> wrote:

>> From a string, I want to capture items separated by whitespace 


> And: no, I don't want to use split(), because the real code looks like this:

> ...so I can capture quoted parameters ("ab cd") as a whole.


So you want to split a whitespace separated string, except
when inside double quotes?

That is a FAQ.

   perldoc -q split


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


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

Date: Wed, 4 Dec 2002 16:23:45 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: RegExp: Capturing parentheses in quantified sub-patterns -- how is it supposed to work?
Message-Id: <slrnaut03h.4mh.tadmc@magna.augustmail.com>

Julian Mehnle <julian@mehnle.net> wrote:

> From a string, I want to capture items separated by whitespace (parameter
> parsing).
> 
> IMO, the pattern  (\S+)(?:\s+(\S+))*  should do the job fine and capture the
> first as well as every following non-whitespace item (if any). But it does
> only capture the first and the last items.


> but then how to do it?


   /(\S+)/g 


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


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

Date: Wed, 04 Dec 2002 22:31:57 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Warnings when creating an array of hashes
Message-Id: <3DEE8221.CFF788A6@acm.org>

"Tassilo v. Parseval" wrote:
> 
> Just realized that I forgot something to mention.
> 
> Also sprach Stephen Patterson:
> 
> >     # make the queue into an array of hashes
> >     for (my $i = 0; $i < @queue; ++$i) {
> >         $queue[$i] =~ s/\s+/ /g; # remove extraneous spaces
> >         my @job = split / /, $queue[$i];
> >         $queue[$i] = (Rank  => $job[0],
> >                       Owner => $job[1],
> >                       Job   => $job[2],
> >                       Files => $job[3],
> >                       Size  => $job[4]
> >                      );
> >      }
> 
> The above (after it has been corrected) can be made shorter by using a
> hash-slice. The idea is that you can set multiple hash-values with one
> statement. Also, the foreach-loop can be written more Perlishly:
> 
>     for my $i (@queue) {
>         $i =~ s/\s+/ /g;
>         my %h; @h{ qw/Rank Owner Job Files Size/ } = split / /, $i;
>         $i = \%h;
>     }

It can be made even shorter by removing one of the substitution
operators:

    for my $i ( @queue ) {
        my %h;
        @h{ qw/Rank Owner Job Files Size/ } = split ' ', $i;
        $i = \%h;
    }

Or more simply as:

    for ( @queue ) {
        my %h;
        @h{ qw/Rank Owner Job Files Size/ } = split;
        $_ = \%h;
    }



John
-- 
use Perl;
program
fulfillment


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

Date: 4 Dec 2002 14:32:04 -0800
From: kalinabears@hdc.com.au (Sisyphus)
Subject: Re: Win32 and *NIX, cryptographically secure random numbers with PERL
Message-Id: <e615828f.0212041432.57727afa@posting.google.com>

"Michael D. Carey" <michael@giantsquidmarks.com> wrote in message news:<3dedd730_3@spamkiller.newsgroups.com>...
> Can someone suggests a method to generate cryptographically
> secure random numbers in PERL?  This method must function on
> both Win32 (ActiveState) and *NIX.  I need a function to
> generate keys for Symmetric Ciphers.
> 
> I wish Win32 had /dev/random.  Thanks in advance...

There are possibly Win32 'dev/random' type packages available. Maybe
some googling would be fruitful.

I'm just putting the final touches on a pure perl implementation of
the (cryptographically secure) MicaliSchnorr PRBG. It uses Math::GMP,
for which I have Win32 binaries
(http://robgil.hypermart.net/w32perl/math_gmp.html) - but I don't know
whether you have Math::GMP on your nix machine(s).

An outline of (and pseudo-code for) the MicaliScnorr PRBG can be found
in Chapter 5 of 'Handbook of Applied Cryptography' - available in PDF
and PS format from http://www.cacr.math.uwaterloo.ca/hac/ .

If you'd like a copy of my implementation, let me know.

Cheers,
Rob


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

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


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