[15672] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3085 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 18 11:05:48 2000

Date: Thu, 18 May 2000 08:05:23 -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: <958662323-v9-i3085@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Thu, 18 May 2000     Volume: 9 Number: 3085

Today's topics:
    Re: "perldoc -f local" fails <rootbeer@redcat.com>
    Re: $0 doesn't give full path <billy@arnis-bsl.com>
    Re: $0 doesn't give full path (Bart Lateur)
    Re: $0 doesn't give full path (Villy Kruse)
    Re: $0 doesn't give full path (M.J.T. Guy)
        [ CGI ] Can you know how long someone stayed connected? <charles.henry@engineer2k.com>
    Re: [ CGI ] Can you know how long someone stayed connec <jhelman@wsb.com>
    Re: Accessing C++ data objects from PERL <rootbeer@redcat.com>
    Re: Bug or Feature: Can't dereference array ref inside  nobull@mail.com
    Re: catching eval warnings and preventing them (M.J.T. Guy)
    Re: converting array of arrayrefs to hash (Peter J Scott)
        DBI to retrieve table meta data from ms access? (Johann)
    Re: Delay in Perl nobull@mail.com
    Re: File lock nobull@mail.com
    Re: Fixed number of fields <jphillip@onyx.southwind.net>
    Re: Fixed number of fields <jeff@vpservices.com>
    Re: Getopt::Std 1 == 0 ??? gregsptl1962@my-deja.com
        Help on namespaces <notacceptingspam@nowhere.can>
    Re: Help on namespaces <jhelman@wsb.com>
        Help, Install Perl Solaris Sun chrisperry38@hotmail.com
    Re: Help, Install Perl Solaris Sun <elaine@chaos.wustl.edu>
    Re: How can I read the volume label of a CDROM on Linux nobull@mail.com
        How Do I Source a File with Environment Variables? (Cory Phillips)
    Re: How Do I Source a File with Environment Variables? (Rafael Garcia-Suarez)
    Re: How to convert a long integer into hex <uackermann@orga.com>
    Re: How to convert a long integer into hex <uackermann@orga.com>
    Re: How to convert a long integer into hex <sb@muccpu1.muc.sdm.de>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Thu, 18 May 2000 08:03:18 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: "perldoc -f local" fails
Message-Id: <Pine.GSO.4.10.10005180659160.25459-100000@user2.teleport.com>

On Thu, 18 May 2000, Jim Cook wrote:

> It seems that no matter what I type after perldoc, I get the message
> 
> No documentation found for "*".

With the star, or what? But it sounds as if your perldoc (or the pod
files, or something) isn't properly installed.

> I downloaded the sources and built my own, and did a "make install"

Well, without seeing your site, I'd have to say, "try again". :-)
Double-check your paths and other settings. But if you're still stuck at
that point, ask again. Cheers!

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



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

Date: Thu, 18 May 2000 10:04:55 GMT
From: Ilja <billy@arnis-bsl.com>
Subject: Re: $0 doesn't give full path
Message-Id: <8g0f81$kdk$1@nnrp1.deja.com>

In article <958641990.555191@shelley.paradise.net.nz>,
  "Tintin" <you.will.always.find.him.in.the.kitchen@parties> wrote:
>
> "| " <new_user@email.msn.com> wrote in message
> news:Od3dYIIw$GA.348@cpmsnbbsa07...
> >
> > > how can i get the fullpath? there has to be a way.
> >
> >
> >  I'll preface this by saying that I'm a novice to Perl, so my advice may
> be
> > off base but I think
> >
> > $dir = system("pwd")
> >
> > should work on a Unix/Linux sys
>
> make it
>
> $dir = `pwd`;
>

Folks, are you just kidding ?

The OP's questions was:

# looks like when evaluated in a cgi (using CGI.pm), $0 is the fullpath.
elsewhere it's the basename. # today on a linux machine i found that even in
a cgi $0 is the # basename. how can i get the fullpath? there has to be a
way.

So OP asked about full path of script which is executing, which has *nothing*
common with current working directory.

And even if you need to get current working directory, there is no need to
execute an external 'pwd' command. Just use Cwd module.

Ilja.


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Thu, 18 May 2000 10:39:44 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: $0 doesn't give full path
Message-Id: <3924c72a.11302027@news.skynet.be>

[bloop] wrote:

>> how can i get the fullpath? there has to be a way.
>
> I'll preface this by saying that I'm a novice to Perl, so my advice may be
>off base but I think
>
>$dir = system("pwd")
>
>should work on a Unix/Linux sys

That tells you the current directory, not necessarily the working
directory.

There's a (standard) module, Cwd (current working directory) whit hte
same result:

	use Cwd;
	$dir = cwd();

If it's the first thing you do, you might be able to combine a relative
path in $0 with the current directory from cwd(). Er... drop the
basename from $0 and do a chdir(), followed by a cwd()?

I'm not sure this covers everything. What if the script is launched
without any path specification, and searched for by the shell in
$ENV{PATH}?

-- 
	Bart.


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

Date: 18 May 2000 12:17:34 GMT
From: vek@pharmnl.ohout.pharmapartners.nl (Villy Kruse)
Subject: Re: $0 doesn't give full path
Message-Id: <slrn8i7nqe.3sj.vek@pharmnl.ohout.pharmapartners.nl>

On Thu, 18 May 2000 10:04:55 GMT, Ilja <billy@arnis-bsl.com> wrote:

>
># looks like when evaluated in a cgi (using CGI.pm), $0 is the fullpath.
>elsewhere it's the basename. # today on a linux machine i found that even in
>a cgi $0 is the # basename. how can i get the fullpath? there has to be a
>way.
>

The content of $0 would be inherited from the argv[0] variable, which
again gets its contents from the exec() system call which launched
the program.  The caller could put anything in that value; it usualy is
the name of the program or the full pathname of the program depending
on how the programmer felt the day he wrote that part.  The argv[0]
variable can also be totaly unrelated to the name of the program.



Check perldoc -f exec to see how easy it is to put bogus values in
the called perl program's $0 varaible.

=item exec LIST

   [snip ...]
If you don't really want to execute the first argument, but want to lie
to the program you are executing about its own name, you can specify
the program you actually want to run as an "indirect object" (without a
comma) in front of the LIST.  (This always forces interpretation of the
LIST as a multivalued list, even if there is only a single scalar in
the list.)  Example:

    $shell = '/bin/csh';
    exec $shell '-sh';		# pretend it's a login shell

or, more directly,

    exec {'/bin/csh'} '-sh';	# pretend it's a login shell

When the arguments get executed via the system shell, results will
be subject to its quirks and capabilities.  See L<perlop/"`STRING`">
for details.





>So OP asked about full path of script which is executing, which has *nothing*
>common with current working directory.
>
>And even if you need to get current working directory, there is no need to
>execute an external 'pwd' command. Just use Cwd module.
>

So there is no good solution to the OP's problem, unfortunately.



Villy


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

Date: 18 May 2000 14:35:29 GMT
From: mjtg@cus.cam.ac.uk (M.J.T. Guy)
Subject: Re: $0 doesn't give full path
Message-Id: <8g0v3h$mc0$1@pegasus.csx.cam.ac.uk>

lone wolf <eng40490@yahoo.com.sg> wrote:
>looks like when evaluated in a cgi (using CGI.pm), $0 is the fullpath.
>elsewhere it's the basename. today on a linux machine i found that even in a
>cgi $0 is the basename. how can i get the fullpath? there has to be a way.

See    perldoc FindBin

This module uses a variety of heuristics to try to find the appropriate
directory.   But as noted elsewhere in this thread, in general there's
no "right" answer to this question.


Mike Guy


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

Date: Thu, 18 May 2000 16:49:44 +0100
From: "Charles Henry" <charles.henry@engineer2k.com>
Subject: [ CGI ] Can you know how long someone stayed connected?
Message-Id: <8g10an$gcg$1@reader1.fr.uu.net>

I need a Perl module that allows me to know how much long someone has
remained connected to my server.

Anyone who got through the - basic - HTTP Authentication Process is
considered to be connected (that's my meaning).

Thanks for any help you may provide.

--
Makau.




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

Date: Thu, 18 May 2000 15:04:48 GMT
From: Jeff Helman <jhelman@wsb.com>
Subject: Re: [ CGI ] Can you know how long someone stayed connected?
Message-Id: <392406B2.4AD9C94B@wsb.com>

Charles Henry wrote:
> 
> I need a Perl module that allows me to know how much long someone has
> remained connected to my server.
> 
> Anyone who got through the - basic - HTTP Authentication Process is
> considered to be connected (that's my meaning).
> 
> Thanks for any help you may provide.
> 
> --
> Makau.

Sorry, but there's really no such beast, since HTTP is a stateless
protocol.  You could approximate this process by either recording the
entry time somewhere (db, file, whatever) or setting a client cookie
with the entry time stamped, but there's no good way to tell when
someone leaves unless they explicitly go through a "logout" procedure
that you define.  The closest you can come is knowing the time of their
last request.

Sorry,
JH

----------------------------------------------------------------
Jeff Helman                 Product Manager -- Internet Services
jhelman@wsb.com                    CCH Washington Service Bureau
----------------------------------------------------------------

99 little bugs in the code, 99 bugs in the code.
Fix one bug, compile again, 100 little bugs in the code.
100 little bugs in the code, 100 bugs in the code.
Fix one bug, compile again, 101 little bugs in the code...

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


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

Date: Thu, 18 May 2000 06:26:15 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: Accessing C++ data objects from PERL
Message-Id: <Pine.GSO.4.10.10005180625370.25459-100000@user2.teleport.com>

On Tue, 16 May 2000, Richard Pyne wrote:

> Can anyone please direct me to a good source of information on how to
> access C++ data objects from PERL?

I don't know anything special about C++ data objects - can you use pack
and unpack, though?

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



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

Date: 18 May 2000 12:30:17 +0100
From: nobull@mail.com
Subject: Re: Bug or Feature: Can't dereference array ref inside hash
Message-Id: <u9ya58ksdy.fsf@wcl-l.bham.ac.uk>

stevel@coastside.net (Steve Leibel) writes:

> $myhash = {
>     arrayref => (),
> };
> 
> In theory I should be able to dereference the arrayref using syntax such as
> 
>     @$myhash->{arrayref}

Operator presedence: the @ binds more tightly than the ->
 
> However every variant of that gets errors, as the following program shows.

Not true, you just haven't tried the right variant :-)

> Is this a bug or a feature?

Feature.

> # Trying parens doesn't help.  This gets:
> #    "Scalar found where operator expected"
> #
> #push (@($myhash->{arrayref}), 47);

In this case you override the precedence using {} not ().  Kinda hard
to explain why. Just file it under "pathalogically eclectic".

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


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

Date: 18 May 2000 14:17:16 GMT
From: mjtg@cus.cam.ac.uk (M.J.T. Guy)
Subject: Re: catching eval warnings and preventing them
Message-Id: <8g0u1c$l18$1@pegasus.csx.cam.ac.uk>

In article <8fstf8$ks7$1@nnrp1.deja.com>,  <mda@discerning.com> wrote:
>for reasons i'd be hard put to justify coherently, i've got
>some code similar to this:
>
>   use strict;
>   my $expr = 'sub {return $whatever}';
>   my $closure = eval $expr;
>   die "closure making failed on $expr: $@" if $@;
>   die "eval didn't make a sub" unless $closure && ref($closure)
>eq 'CODE';
>
>running this i get:
>Global symbol "$whatever" requires explicit package name at (eval 1)
>line 1.
>eval didn't make a sub at test.pl line 6.
>
>Problem 1:
>Of course $whatever is not defined. But why is it printing this,
>rather than raising an exception? I don't want random console
>output. The first die is not executed, but the second one is.
>I *want* the warnings; i don't want to simply do no strict.
>No, it doesn't help to say: eval {eval $substring};

That's a bug in Perl versions prior to 5.6.0.    The error message
is actually being output as a warning, so doesn't get into $@.
You can catch the error with $SIG{__WARN__}.

Or upgrading to 5.6.0 may be easier.


Mike Guy


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

Date: Tue, 16 May 2000 16:21:16 GMT
From: peter@PSDT.com (Peter J Scott)
Subject: Re: converting array of arrayrefs to hash
Message-Id: <0AeU4.32010$q_4.823829@news1.gvcl1.bc.home.com>

In article <8frpon$bdr$1@nnrp1.deja.com>,
 wisecounselor@my-deja.com writes:
> I wanted to
>turn
>
>[
>   { "name" => "name_value1", "key2" => "val2", ...}
>   { "name" => "name_value2", "key2" => "val2", ...}
>]
>
>into
>
>{ "name_value1" => [ { original hashref 1} ],
>  "name_value2" => [ { original hashref 2} ]
>}
>
>so I could just iterate over keys of this one hash, and then go through
>the arrayref which should have the rows of data for that key.  Is there
>an easier way to do the whole thing, or at least a way to transform the
>data, my way doesn't work, the first value of "name" works, but the
>remaining keys only get one value where they should have many values.

Your code will work the way you want only if the items in the original array
with the same 'name' key are contiguous.

>brian
>
>my $seen;
>my %menus; # the new hash
>
># $m is an arrayref of hashrefs
>
>for my $item (@$m)
>{
>	if($seen ne $item->{name})
>	{
>		$menus{ $item->{name} } = [ $item ];
>	}
>	else
>	{
>		push @{ $menus{$item->{name}} }, $item;
>	}
>	$seen = $item->{name};
>}

The magic of autovivification takes care of your problem and simplifies the
code to one line to boot:

my %menus;
my $m = [{ name => 'name1', key => 'val1' }, 
	 { name => 'name2', key => 'val2' },
	 { name => 'name1', key => 'val3' }];
push @{ $menus{$_->{name}} }, $_ for @$m;

use Data::Dumper;
print Dumper \%menus;




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

Date: Thu, 18 May 2000 13:50:11 GMT
From: x@y.not (Johann)
Subject: DBI to retrieve table meta data from ms access?
Message-Id: <8F38660D7sjkfdljdfsl@63.211.125.90>

Has anyone been able to get the table meta data out of an ms access db?

I need to get each columns' datatype and length, but to no avail.  I
assume the problem is that access won't or can't disclose such info,
and not that DBI:DBD can't ascertain it.

BTW I am using DBI:ODBC on win32 platform and can otherwise connect,
insert, update, select just fine.

Thanks!

Johann


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

Date: 18 May 2000 12:32:39 +0100
From: nobull@mail.com
Subject: Re: Delay in Perl
Message-Id: <u9wvksksa0.fsf@wcl-l.bham.ac.uk>

"Alex Shi" <chpshi@connection.com> writes because he can't be bothered
to read the manual:

> How can I set a time delay or sleep (5 or 10 seconds) in Perl?

perldoc -f sleep

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


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

Date: 18 May 2000 12:37:20 +0100
From: nobull@mail.com
Subject: Re: File lock
Message-Id: <u9vh0cks27.fsf@wcl-l.bham.ac.uk>

jimtaylor5@aol.com (Jimtaylor5) writes:

> Can someone tell me what I am doing wrong, or a more effective way of
> accomplishing this task. Your help is appreciated.

If our help is appreciated why have you ignored all the help I gave
yesterday when you posted the exact same question?

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


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

Date: Thu, 18 May 2000 13:52:51 GMT
From: John Phillips <jphillip@onyx.southwind.net>
Subject: Re: Fixed number of fields
Message-Id: <TASU4.914$GD2.146550@nntp1.onemain.com>

No problem. Send samples/examples.

R <risj5@hotmail.com> wrote:
> I'm working on a script that handles the input from a webpage-form so that
> all fields are filled in to a file. I.e not only the fields that are filled
> in by the user, the number of fields must be fixed.

> If a user don't fill in the address, that field must be filled in anyway in
> the file by the perlscript.

> Anyone who knows how to do this??

> Thanks in advance

> Rikard



-- 



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

Date: Thu, 18 May 2000 07:25:30 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: Fixed number of fields
Message-Id: <3923FD5A.D2E00D08@vpservices.com>

R wrote:
> 
> I'm working on a script that handles the input from a webpage-form so that
> all fields are filled in to a file. I.e not only the fields that are filled
> in by the user, the number of fields must be fixed.
> 
> If a user don't fill in the address, that field must be filled in anyway in
> the file by the perlscript.

The script needs to know the names of all the fields and do a loop
through that list of names.  For each name on the list, if there is a
filled in form parameter, add that to the record, and if not, add undef
or '', or 'NULL' or whatever you want to use to indicate an empty field.

-- 
Jeff


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

Date: Thu, 18 May 2000 11:47:22 GMT
From: gregsptl1962@my-deja.com
Subject: Re: Getopt::Std 1 == 0 ???
Message-Id: <8g0l86$r2k$1@nnrp1.deja.com>

In article <8ftfjd$8da$1@nnrp1.deja.com>,
  Andreas Kahari <andkaha@my-deja.com> wrote:
> In article <8fs5q8$q3s$1@nnrp1.deja.com>,
>   gregsptl1962@my-deja.com wrote:
> > #!/usr/bin/perl
> > #!/usr/bin/perl -dw
> >
> > use Getopt::Std;
> > getopts ('c:');  # set -c to the skip count
> >
> > $Getopt::Std::opt_c = '1' unless $Getopt::Std::opt_c;
> >          # Set count by default to 1
> >
> > print "$Getopt::Std::opt_c\n"; # Debug Why's -c 0 == 1???
> >
> > Sent via Deja.com http://www.deja.com/
> > Before you buy.
> >
>
> You probably meant to say
>
> use Getopt::Std;
> getopts('c:');
> $opt_c = '1' unless defined($opt_c); # use defined to see if it was
> defined or not
> print "$opt_c\n";
>
> /A
>
> --
> # Andreas Kähäri, <URL:http://hello.to/andkaha/>.
> # All junk email is reported to the appropriate authorities.
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
Yes! That's it thanks for the help. It always seems easy once you've got
the answers. (meaning I've made that mistake before (if -- defined).
 Also I now notice that the formatting changes upon posting, good thing
perl needs ";" as EOL delimiter!


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Thu, 18 May 2000 14:36:22 GMT
From: "Jim Stout" <notacceptingspam@nowhere.can>
Subject: Help on namespaces
Message-Id: <GdTU4.245$T41.6940@newsread1.prod.itd.earthlink.net>

Hello,

I'm somewhat of a Perl rookie with a question regarding namespaces and
subroutine/function call semantics when using the 'package' declaration.
Note that I'm am not referring to Perl modules here, just subroutines
inside packages. If I have the following:

package foo;
sub bar{
does whatever;
}

I "require" the package 'foo' elsewhere. I know to access variables in
foo I need to qualify them - that is,

$something = $foo::variable;.

Are the same semantics required to call a subroutine in a package? That
is, should subroutine 'bar' be called as,

&foo::bar();

or as,

&bar();

or are both correct.

Any help appreciated. The manuals aren't clear to me on this.

Thanks.






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

Date: Thu, 18 May 2000 15:01:09 GMT
From: Jeff Helman <jhelman@wsb.com>
Subject: Re: Help on namespaces
Message-Id: <392405D7.90F28D04@wsb.com>

Jim Stout wrote:
> 
> Hello,
> 
> I'm somewhat of a Perl rookie with a question regarding namespaces and
> subroutine/function call semantics when using the 'package' declaration.
> Note that I'm am not referring to Perl modules here, just subroutines
> inside packages. If I have the following:
> 
> package foo;
> sub bar{
> does whatever;
> }
> 
> I "require" the package 'foo' elsewhere. I know to access variables in
> foo I need to qualify them - that is,
> 
> $something = $foo::variable;.
> 
> Are the same semantics required to call a subroutine in a package? That
> is, should subroutine 'bar' be called as,
> 
> &foo::bar();
> 
> or as,
> 
> &bar();
> 
> or are both correct.
> 
> Any help appreciated. The manuals aren't clear to me on this.
> 
> Thanks.

While you're in package foo, you can call subroutine bar with 

&bar;

Outside of package foo, you'll need to use an explicit package
reference:

&foo::bar();

It's a good programming practice, though, to use explicit packages
references even where perl lets you get away without them, since it
makes debugging your code 6 months down the line a hell of a lot easier.

Hope this helps,
JH
 
----------------------------------------------------------------
Jeff Helman                 Product Manager -- Internet Services
jhelman@wsb.com                    CCH Washington Service Bureau
----------------------------------------------------------------

99 little bugs in the code, 99 bugs in the code.
Fix one bug, compile again, 100 little bugs in the code.
100 little bugs in the code, 100 bugs in the code.
Fix one bug, compile again, 101 little bugs in the code...

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


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

Date: Thu, 18 May 2000 13:31:48 GMT
From: chrisperry38@hotmail.com
Subject: Help, Install Perl Solaris Sun
Message-Id: <8g0rc4$1sm$1@nnrp1.deja.com>

Looking for resources on instructions on installing perl on a Sun
Solaris 2.6 Sparc system.

I downloaded a large source file for Solaris and need some help
installing it.

TIA


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Thu, 18 May 2000 13:51:01 GMT
From: Elaine Ashton <elaine@chaos.wustl.edu>
Subject: Re: Help, Install Perl Solaris Sun
Message-Id: <B5496D8A.4455%elaine@chaos.wustl.edu>

in article 8g0rc4$1sm$1@nnrp1.deja.com, chrisperry38@hotmail.com at
chrisperry38@hotmail.com quoth:

> Looking for resources on instructions on installing perl on a Sun
> Solaris 2.6 Sparc system.

http://www.sunfreeware.com/ for pre-made packages or

'./Configure -de' in the source directory followed by a 'make'

enjoy.

e.




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

Date: 18 May 2000 12:41:15 +0100
From: nobull@mail.com
Subject: Re: How can I read the volume label of a CDROM on Linux?
Message-Id: <u9u2fwkrvo.fsf@wcl-l.bham.ac.uk>

rgb <tiemaster@home.com> writes:

> The title says it all.  I looked for a Linux program to do this, but I
> must be blind or not very good at searching for things.  Can anyone
> point me in the right direction?

Try a Linux newsgroup, this is a Perl newsgroup.

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


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

Date: Thu, 18 May 2000 13:43:48 GMT
From: cory-p@usa.net (Cory Phillips)
Subject: How Do I Source a File with Environment Variables?
Message-Id: <3923f2ca.1057347@news.jsc.nasa.gov>

We have a text file on our web systems with a bunch of environment
variables in it.  We have some CGI scripts written in Unix shell and
it's easy to source the file for the variables with

 . /path/to/file/env_vars

How do I accomplish this same thing from Perl?  Do I do it with back
quotes?

Cory



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

Date: Thu, 18 May 2000 14:04:48 GMT
From: garcia_suarez@hotmail.com (Rafael Garcia-Suarez)
Subject: Re: How Do I Source a File with Environment Variables?
Message-Id: <slrn8i7u9u.398.garcia_suarez@r-garcia.kazibao.net>

Cory Phillips wrote in comp.lang.perl.misc:
>We have a text file on our web systems with a bunch of environment
>variables in it.  We have some CGI scripts written in Unix shell and
>it's easy to source the file for the variables with
>
>. /path/to/file/env_vars
>
>How do I accomplish this same thing from Perl?  Do I do it with back
>quotes?

No. Backquotes (or the 'system' function) launch a subprocess and thus
only the subprocess environment is altered.
You have to read the file and alter environment yourself.
Something like that may help: (assuming the file containing env vars
looks like
  VAR1=value1
  VAR2=value2
  etc...
)

open FILE, 'envfile.sh' or die "Can't open it: $!\n";
while (<FILE>) {
  my ($a,$b) = split /\s*=\s*/, $_, 2;
  $ENV{$a} = $b;
}
close FILE;

Note: Of course this code is not very secure. You must at least ensure
that the envvar file and its directory are writable only by root. Or
something like that.


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

Date: Thu, 18 May 2000 13:28:28 +0200
From: Ulrich Ackermann <uackermann@orga.com>
Subject: Re: How to convert a long integer into hex
Message-Id: <3923D3DC.1807F882@orga.com>

Andrew Goode wrote:
> 
> I have a long integer which I want to convert into hex.
> 
> e.g.
>     $value = 9969772182300;
>     the hex value should be 44BABB1C
                              ^^^^^^^^
I think this is not the hex value from 9969772182300!

> But with any of these conversions, I get an integer overflow error, or a
> value of FFFFFFFF:
> 
>     1) $hexValue = hex($value);
>     2) $hexValue = sprintf ("%X", $value);
>     3) $hexValue = sprintf ("%lX", $value);
> 
> I have read Math::BigInt, but am unsure of the usage.
> 
> I hope someone can offer a simple solution to this seemingly simple problem.
I can not offer you a simple solution. Maybe there is one, but I am a
Perl newbie and 
do not no it.
But I have a little script, which might do what you want (it's not
tested!!!)
#############################################################
	#!/usr/local/bin/perl -w

	use Math::BigInt;
	use strict; 

	my $bigDec = Math::BigInt->new(9969772182300);
	my $hex = bigInt2Hexascii($bigDec);

	print "hex(main) = $hex\n";

	sub bigInt2Hexascii {
	  my $bigInt = shift;
	  my $hexAscii = "";

	  for (my $i = 100; $i >= 0; $i--) { 
	    my $big16 = Math::BigInt->new(16);
	    my $temp = $bigInt/($big16**$i);
	    next if $temp == 0;
	    $bigInt -= $temp*($big16**$i);
	    my $hex = unpack "h", pack "I*", $temp;
	    $hexAscii .= $hex;
	  }

	  return $hexAscii;
	}
#############################################################

Hope this helps?!

Ulrich


-- 
Ulrich Ackermann
ORGA Kartensysteme GmbH (SY-PEAT-STA)
Tel.:+49.5254.991-925 
mailto:uackermann@orga.com



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

Date: Thu, 18 May 2000 14:41:28 +0200
From: Ulrich Ackermann <uackermann@orga.com>
Subject: Re: How to convert a long integer into hex
Message-Id: <3923E4F8.A17BC02B@orga.com>

Ulrich Ackermann wrote:
> But I have a little script, which might do what you want (it's not
> tested!!!)

The script needed some corrections and looks now like this:

###################################################################
#!/usr/local/bin/perl -w

use Math::BigInt;
use strict; =


my $bigDec =3D Math::BigInt->new($ARGV[0]);
print "bigDec =3D $bigDec\n";

my $hex =3D bigInt2Hexascii($bigDec);

print "hex =3D $hex\n";

sub bigInt2Hexascii {
  my $bigInt =3D shift;
  my $hexAscii =3D "";
  my $test16 =3D Math::BigInt->new(16);

  die "Int-Zahl zu gro=DF!\n" if $bigInt/($test16**300) >=3D 1;

  for (my $i =3D 300; $i >=3D 0; $i--) {
    my $big16 =3D Math::BigInt->new(16);
    my $temp =3D $bigInt/($big16**$i);
    $bigInt -=3D $temp*($big16**$i);
    my $hex =3D unpack "h", pack "I*", $temp;
    $hexAscii .=3D $hex;
  }
  $hexAscii =3D~ s/^0*//;
  unless ( (length($hexAscii) % 2) =3D=3D 0 ) {
    $hexAscii =3D "0" . $hexAscii;
  }
  return $hexAscii;
}
###################################################################

Ulrich


-- =

Ulrich Ackermann
ORGA Kartensysteme GmbH (SY-PEAT-STA)
Tel.:+49.5254.991-925 =

mailto:uackermann@orga.com


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

Date: 18 May 2000 14:01:26 GMT
From: Steffen Beyer <sb@muccpu1.muc.sdm.de>
Subject: Re: How to convert a long integer into hex
Message-Id: <8g0t3m$h1f$1@solti3.sdm.de>

In article <8g0d60$i99$1@dove.qut.edu.au>, Andrew Goode <a.goode@qut.edu.au> wrote:

> I have a long integer which I want to convert into hex.

> e.g.
>     $value = 9969772182300;
>     the hex value should be 44BABB1C

> But with any of these conversions, I get an integer overflow error, or a
> value of FFFFFFFF:

>     1) $hexValue = hex($value);
>     2) $hexValue = sprintf ("%X", $value);
>     3) $hexValue = sprintf ("%lX", $value);

> I have read Math::BigInt, but am unsure of the usage.

> I hope someone can offer a simple solution to this seemingly simple problem.

> Thanks
> Andrew

-------------------- cut here -------------------- cut here --------------------

Suggestion for the perlfaq4 manpage:
------------------------------------

How do I convert from hexadecimal to decimal:

1) $int = 0xDEADBEEF;
   $dec = sprintf("%d", $int);
2) $int = hex("DEADBEEF");
   $dec = sprintf("%d", $int);
3) $int = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8)));
   $dec = sprintf("%d", $int);
4) use Bit::Vector;
   $vec = Bit::Vector->new_Hex(32, "DEADBEEF");
   $dec = $vec->to_Dec();

How do I convert from decimal to hexadecimal:

1) $hex = sprintf("%X", 3735928559);
2) $hex = unpack("H*", pack("N", 3735928559));
3) use Bit::Vector;
   $vec = Bit::Vector->new_Dec(32, -559038737);
   $hex = $vec->to_Hex();
4) use Bit::Vector;
   $vec = Bit::Vector->new_Dec(33, 3735928559);
   $vec->Resize(32); # suppress leading 0 if unwanted
   $hex = $vec->to_Hex();

How do I convert from octal to decimal:

1) $int = 033653337357; # note the leading 0!
   $dec = sprintf("%d", $int);
2) $int = oct("33653337357");
   $dec = sprintf("%d", $int);
3) use Bit::Vector;
   $vec = Bit::Vector->new(32);
   $vec->Chunk_List_Store(3, split(//, reverse "33653337357"));
   $dec = $vec->to_Dec();

How do I convert from decimal to octal:

1) $oct = sprintf("%o", 3735928559);
2) use Bit::Vector;
   $vec = Bit::Vector->new_Dec(32, -559038737);
   $oct = reverse join('', $vec->Chunk_List_Read(3));

How do I convert from binary to decimal:

1) $int = unpack("N", pack("B32",
      substr("0" x 32 . "11011110101011011011111011101111", -32)));
   $dec = sprintf("%d", $int);
2) use Bit::Vector;
   $vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111");
   $dec = $vec->to_Dec();

How do I convert from decimal to binary:

1) $bin = unpack("B*", pack("N", 3735928559));
2) use Bit::Vector;
   $vec = Bit::Vector->new_Dec(32, -559038737);
   $bin = $vec->to_Bin();

The remaining transformations (e.g. hex -> oct, bin -> hex, etc.)
are left as an exercise to the inclined reader. :-)

Note: The advantage of the Bit::Vector module is that it works with
      numbers of ANY size and that it is optimized for speed (it is
      written in C, internally, and uses very efficient algorithms;
      for instance it uses a divide-and-conquer algorithm for conversions
      to and from decimal which should be the fastest possible around).

-------------------- cut here -------------------- cut here --------------------

Hope this helps!

Regards,
-- 
    Steffen Beyer <sb@engelschall.com>
    http://www.engelschall.com/u/sb/whoami/ (Who am I)
    http://www.engelschall.com/u/sb/gallery/ (Fotos Brasil, USA, ...)
    http://www.engelschall.com/u/sb/download/ (Free Perl and C Software)


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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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 V9 Issue 3085
**************************************


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