[13666] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1076 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 15 15:47:38 1999

Date: Fri, 15 Oct 1999 12:47:20 -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: <940016839-v9-i1076@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 15 Oct 1999     Volume: 9 Number: 1076

Today's topics:
        Proper way to test if a hash key's value is defined. (Ghost)
    Re: Proper way to test if a hash key's value is defined <uri@sysarch.com>
    Re: Proper way to test if a hash key's value is defined <ltl@rgsun5.viasystems.com>
    Re: Proper way to test if a hash key's value is defined <makkulka@cisco.com>
    Re: Proper way to test if a hash key's value is defined (Ghost)
    Re: Proper way to test if a hash key's value is defined <uri@sysarch.com>
    Re: Proper way to test if a hash key's value is defined (Ghost)
    Re: Proper way to test if a hash key's value is defined (Tad McClellan)
        Q: DBD Informix 0.62 selectall_arrayref returns garbled <mjs2@gte.com>
        Q: DBI::Msql Problem erik_lembke@my-deja.com
    Re: Q: DBI::Msql Problem (Michael Budash)
    Re: Q: DBI::Msql Problem erik_lembke@my-deja.com
    Re: Q: DBI::Msql Problem (Michael Budash)
        QMAIL AND PERL (Chris Burton)
    Re: QMAIL AND PERL <gellyfish@gellyfish.com>
    Re: QMAIL AND PERL <Chris@fatlarry.karoo.co.uk>
    Re: QMAIL AND PERL <uri@sysarch.com>
    Re: QMAIL AND PERL <david@gigawatt.com>
    Re: Question about AlarmCall <rootbeer@redcat.com>
        qw and x operator <marcel.grunauer@lovely.net>
    Re: qw and x operator <rootbeer@redcat.com>
    Re: qw and x operator (Abigail)
    Re: qw and x operator (Tad McClellan)
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Thu, 14 Oct 1999 22:08:11 GMT
From: ghost24@bctek.com (Ghost)
Subject: Proper way to test if a hash key's value is defined.
Message-Id: <s0cl2bqahu227@corp.supernews.com>

I have been having problems when using hashes of getting the error,
Use of uninitialized value at tstfile.pl line 12.

I finaly fixed it using the following code:

#!/usr/bin/perl -w
use MPEG::MP3Info;
use strict;
my $file = 'im_so_happy.mp3';
my $tag = get_mp3tag($file) or die "No TAG info!\n";
my $key;
for $key (keys %$tag) {
    if (defined $key){
      print "$key: $tag->{$key}\n" if defined($tag->{$key});
    }
}

However, according to perldoc -f defined : 
 ... snip ...
On the other hand, use of defined() upon aggregates (hashes and arrays)
is not guaranteed to produce intuitive results, and should probably be
avoided.

When used on a hash element, it tells you whether the value is defined,
not whether the key exists in the hash.  Use L</exists> for the latter
purpose.
 .... snip ....

Which is it?! do I use it to test whether the value is defined or is
it not guaranteed to work??

In The perl cookbook it say:
 # does %HASH have a value for $key ?
 if (exists($HASH{$KEY})){
     #it exists
 }else{ #it doesnt }

Using this in my code verifies that the $key exists in my Hash but
does not verify that the HASH{$KEY} is not uninitialized. So it 
really doesnt have a value unless you consider unitialized a value?!

How can a key exist without being initialized.. 
   HASH{$KEY} = "";     <-- would that give me an unitialized key

Im a bit confused!

Thanks in advance for any help!..
-- 
 ....................................................................
      ++ ghost24@bctek.com ++ http://www.bctek.com/~ghost24 ++      
 ....................................................................
WARNING: Do not read if you are pregnant or nursing or if you are at
risk or are being treated for high blood pressure, heart, liver,
thyroid or psychiatric disease,nervousness, anxiety, depression,
seizure disorder, stroke or difficulty in urination.
Consult you health care professional before reading!!
 ....................................................................



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

Date: 14 Oct 1999 18:30:38 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Proper way to test if a hash key's value is defined.
Message-Id: <x79055k229.fsf@home.sysarch.com>

>>>>> "G" == Ghost  <ghost24@bctek.com> writes:

  G> my $tag = get_mp3tag($file) or die "No TAG info!\n";

  G> for $key (keys %$tag) {
  G>     if (defined $key){

all keys returns by keys are defined. an undefined value when used as a
key will be converted to '' which is what keys will return. so that if
is a waste of electrons.

  G>       print "$key: $tag->{$key}\n" if defined($tag->{$key});

that makes more sense since a value could be undef. but you should read
more about what get_mp3tag returns to be sure. 


  G> On the other hand, use of defined() upon aggregates (hashes and arrays)
  G> is not guaranteed to produce intuitive results, and should probably be
  G> avoided.

  G> When used on a hash element, it tells you whether the value is defined,
  G> not whether the key exists in the hash.  Use L</exists> for the latter
  G> purpose.
  G> .... snip ....

  G> Which is it?! do I use it to test whether the value is defined or is
  G> it not guaranteed to work??

you are not clear in your confusion. there is no contradiction in those
two quotes as you imply. an aggregate is %hash or @array, not a scalar
element of those. 

  G>  if (exists($HASH{$KEY})){
  G>      #it exists

  G> Using this in my code verifies that the $key exists in my Hash but
  G> does not verify that the HASH{$KEY} is not uninitialized. So it 
  G> really doesnt have a value unless you consider unitialized a value?!

undef IS a value. it is the undefined value. 

  G> How can a key exist without being initialized.. 
  G>    HASH{$KEY} = "";     <-- would that give me an unitialized key
        ^
missing $ there.

	$hash{ 'key' } = undef ;
	$hash{ 'null' } = '' ;

	print "key exists\n" if exists $hash{ 'key' } ;
	print "key value defined\n" if defined $hash{ 'key' } ;
	print "null value defined\n" if defined $hash{ 'null' } ;

you can have an existing key (test with exists) with ANY scalar
value. also missing keys in a hash will give the undef value. hence you
can't tell if a key doesn't exist or just has the undef value using
defined. but keys will only return the actual keys so you don't need to
test if they are defined as you did above.

  G> Im a bit confused!

we can tell. go read perldata (and again). you have to grok hashes to be
able to use perl in any serious fashion. they are not hard. you just
don't have the correct image in your brain yet.

uri



-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


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

Date: 14 Oct 1999 22:40:35 GMT
From: lt lindley <ltl@rgsun5.viasystems.com>
Subject: Re: Proper way to test if a hash key's value is defined.
Message-Id: <7u5m53$4lb$1@rguxd.viasystems.com>

Ghost <ghost24@bctek.com> wrote:
:>How can a key exist without being initialized.. 
:>   HASH{$KEY} = "";     <-- would that give me an unitialized key

:>Im a bit confused!

You are very close.  Just consider now that undefined is a perfectly
good value for a scalar variable.  Hash values can be any scalar
which includes strings, numbers, references and even undef.

$hash{first} = 'a value'; # key exists and value is defined
print 'does not exist' unless (exists $hash{second}); # no key exists yet
$hash{second} = undef;    # now key does exist, but value not defined.

You check for the existence of a key (but that isn't necessary in your
loop where you get the set of keys from the 'keys' function), you
check for definedness of a value (a particular value in the hash).

What the doc was saying was don't try to check the definedness of the
entire hash.

print 'true' if defined %hash; # is not useful to you

-- 
// Lee.Lindley   /// I used to think that being right was everything.
// @bigfoot.com  ///  Then I matured into the realization that getting
////////////////////   along was more important.  Except on usenet.


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

Date: Thu, 14 Oct 1999 15:46:07 -0700
From: Makarand Kulkarni <makkulka@cisco.com>
Subject: Re: Proper way to test if a hash key's value is defined.
Message-Id: <38065D2F.5D0B7BAC@cisco.com>

{ Ghost wrote:

> for $key (keys %$tag) {
>     if (defined $key){

No need to do this.  Return array from keys always has all items defined

>       print "$key: $tag->{$key}\n" if defined($tag->{$key});
>     }
> }
> How can a key exist without being initialized..
>    HASH{$KEY} = "";     <-- would that give me an unitialized key

When a key is added to a hash a value kicks in automatically.
You create keys in two ways.

(1) adding a key/value pair.
$hash->{$key}= $some_value
where $some_value is either undef or any other value ( and undef IS a value
).

(2)  Inadvertently because of autovivification
eg.
use Data::Dumper ;
$h= {} ;
if  ( exists (  $h->{a}{b} )  ) {
print " inside the loop " , Dumper $h ;
}
print "outside the loop" ,  Dumper $h ;

in this example $h->{a} exists once control is outside the loop.
The value is  {} (empty hash).  The value kicking in here depends on
context.
--



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

Date: Thu, 14 Oct 1999 23:19:28 GMT
From: ghost24@bctek.com (Ghost)
Subject: Re: Proper way to test if a hash key's value is defined.
Message-Id: <s0cp80c4hu279@corp.supernews.com>

In article <x79055k229.fsf@home.sysarch.com>,
	Uri Guttman <uri@sysarch.com> writes:
> you are not clear in your confusion. there is no contradiction in those
> two quotes as you imply. an aggregate is %hash or @array, not a scalar
> element of those. 

    Thank you, I understand know. I didnt make the connection that since
  $hash{'blah'} is returning a scalar it is no longer considered part of
  an aggregate. So what the documentation means is dont rely on behavior
  of defined (%hash) but its fine to do defined $hash{'blah'}.

> 
> 	$hash{ 'key' } = undef ;
> 	$hash{ 'null' } = '' ;
> 
> 	print "key exists\n" if exists $hash{ 'key' } ;
> 	print "key value defined\n" if defined $hash{ 'key' } ;
> 	print "null value defined\n" if defined $hash{ 'null' } ;

Some more document searching says that:  			
  $key = "";
  defined($key); <-- returns true.

OK. makes sense.

So "" is a defined value of a zero length string. However, 
some experimenting revealed that although "" is defined, 
undefined eq ""? Why?

example:

for $key (keys %$tag) {
    if ($tag->{$key} eq ""){
	   print "$key: \"\" \n";
    }elsif (defined $tag->{$key}) {
	   print "$key: $tag->{$key}\n";
    }else { 
	   print "$key: <undefined>\n";
    }    
}

The undefined tag matches the eq "" printing:
   MYTAG: ""
   Use of uninitialized value at mp3tag2.pl line 14.

However, If switch to:

for $key (keys %$tag) {
    if (defined $tag->{$key}) {
	   if ($tag->{$key} eq "") {
		  print "$key: \"\"\n";
	   }else{
		  print "$key: $tag->{$key}\n";
	   }
    }else{
	   print "$key: <undefined>\n";
    }
}

It prints properly:

  MYTAG: <undefined>
	
So my question is why is the Undefined value eq "" but 
defined("") returns true?  
	true - Undef eq ""
     true - Defined("")

-- 
 ....................................................................
      ++ ghost24@bctek.com ++ http://www.bctek.com/~ghost24 ++      
 ....................................................................
WARNING: Do not read if you are pregnant or nursing or if you are at
risk or are being treated for high blood pressure, heart, liver,
thyroid or psychiatric disease,nervousness, anxiety, depression,
seizure disorder, stroke or difficulty in urination.
Consult you health care professional before reading!!
 ....................................................................



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

Date: 14 Oct 1999 19:28:52 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Proper way to test if a hash key's value is defined.
Message-Id: <x74sftjzd7.fsf@home.sysarch.com>

>>>>> "G" == Ghost  <ghost24@bctek.com> writes:

  G> Some more document searching says that:  			
  G>   $key = "";
  G>   defined($key); <-- returns true.

  G> OK. makes sense.

  G> So "" is a defined value of a zero length string. However, 
  G> some experimenting revealed that although "" is defined, 
  G> undefined eq ""? Why?

because undef is converted to '' in a string context (and to 0 in a
numeric context). it you are running with -w (you are using that?) then
you will get warnings when some of those conversions occur.


  G> example:

  G> for $key (keys %$tag) {
  G>     if ($tag->{$key} eq ""){
  G> 	   print "$key: \"\" \n";

learn how to not need backslashes in quotes. it is very ugly to see.

and in general, my rule is to use single quotes when there is no
interpolation in the string. so a null string is ''. some like "" only
then since '' looks like a single ". in the above case it would be
better to use '' since that is fine inside "".

  G> The undefined tag matches the eq "" printing:
  G>    MYTAG: ""
  G>    Use of uninitialized value at mp3tag2.pl line 14.

correct. as i said above.

  G> However, If switch to:

  G> for $key (keys %$tag) {
  G>     if (defined $tag->{$key}) {
  G>     }else{
  G> 	   print "$key: <undefined>\n";
  G>     }
  G> }
	
  G> So my question is why is the Undefined value eq "" but 
  G> defined("") returns true?  
  G> 	true - Undef eq ""
  G>      true - Defined("")

see above.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


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

Date: Fri, 15 Oct 1999 00:23:03 GMT
From: ghost24@bctek.com (Ghost)
Subject: Re: Proper way to test if a hash key's value is defined.
Message-Id: <s0csv7s9hu248@corp.supernews.com>

In article <x74sftjzd7.fsf@home.sysarch.com>,
	Uri Guttman <uri@sysarch.com> writes:

> because undef is converted to '' in a string context (and to 0 in a
> numeric context). it you are running with -w (you are using that?) then
> you will get warnings when some of those conversions occur.

Doing some more searching I found the answer, and am posting it
for others who may be as confused as I was.

It is not that undef is converted to '' in a string context or a 0
in a numeric context. Whats hapening is in the comparison the undef
and "" are being converted into True or False statements.

--snip
From Perl Journal #14: What is truth by Nathan Torkington

Rules for determining wether something is true or false:

1. true/false are scalar concepts
2. undef is false
3. "" is false
4. 0 is false
5. 0.0 is false
6. "0" is false
7. all else is true.

--snip

thus:

$x = "";
$y = undef;
print "x: is defined\n" if defined $x;
print "y: is undefined\n" unless defined $y;
print "x = y\n" if ($x eq $y);        # x is false & y is false

prints:
x: is defined
y: is undefined
x = y



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

Date: Thu, 14 Oct 1999 21:55:07 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Proper way to test if a hash key's value is defined.
Message-Id: <rh16u7.id4.ln@magna.metronet.com>

Ghost (ghost24@bctek.com) wrote:
: In article <x74sftjzd7.fsf@home.sysarch.com>,
: 	Uri Guttman <uri@sysarch.com> writes:

: > because undef is converted to '' in a string context (and to 0 in a
: > numeric context). it you are running with -w (you are using that?) then
: > you will get warnings when some of those conversions occur.

: Doing some more searching I found the answer, and am posting it
: for others who may be as confused as I was.

: It is not that undef is converted to '' in a string context or a 0
: in a numeric context. 


   Yes it is.

   If you still think that, then keep thinking, 'cause you don't
   "have it" yet.

   Maybe this (from perlop.pod) will help:

 ------------------------------
When presented with something which may have several different
interpretations, Perl uses the principle B<DWIM> (expanded to Do What I Mean
- not what I wrote) to pick up the most probable interpretation of the
source.  This strategy is so successful that Perl users usually do not
suspect ambivalence of what they write.  However, time to time Perl's ideas
differ from what the author meant.
 ------------------------------

   You need to get an understanding of Perl's rules in regards
   to truth.


: Whats hapening is in the comparison the undef
: and "" are being converted into True or False statements.


   That sentence doesn't make any sense.

   Strings cannot be "converted" to true/false.

   They can participate in an expression though.

   The _result of evaluating the expression_ is true/false.

   The expression may even consist of only a string.


: --snip
: From Perl Journal #14: What is truth by Nathan Torkington

: Rules for determining wether something is true or false:

: 1. true/false are scalar concepts
: 2. undef is false  (#3 below)

   Because it is converted into the empty string.

: 3. "" is false
: 4. 0 is false

   Because it is converted to the string "0" (#6 below)

: 5. 0.0 is false

   Because it is converted to the string "0" (#6 below)

: 6. "0" is false
: 7. all else is true.

: --snip

   
   Here is a Perl program that seems to show that the above
   list is missing something:

-------------------
#!/usr/bin/perl
use strict;

$_ = 0.0e10;

if ( $_ )
   { print "true\n" }
else
   { print "false\n" }
-------------------

output:

false



   I like Randal's explanation of truth in the Llama (paraphrased):

      the expression is evaluated in a scalar context

      convert the expression to a string (unless it already is a 
      string)

      if it is one of the two strings '0' or '' (empty string)
         false
      else
         true

   Much easier to remember.

   But it requires that you know how perl converts numbers to strings.



   So I also present the second way, phrased slightly differently
   from gnat's:

   false if:

      "numerically zero" (covers 4 and 5 and my program's usage)

      undef

      '' (empty string)

      '0'

   true otherwise.


   The 2 ways of saying it are compatible because "numerically zero"
   numbers are converted to '0', and undef is converted to ''.


: print "x = y\n" if ($x eq $y);        # x is false & y is false


   Your comment does not match your code.

   Here the expression is:   $x eq $y

   So the print will execute if the _values_ of $x and $y are the same.

   It will execute for either of the below set of assignments:

      $x = $y = '';   # false values

      $x = $y = '1';  # true values

   So the print will print if $x is true and $y is true!

   The opposite of what your comment says.


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: Wed, 13 Oct 1999 10:39:43 -0400
From: Michael Sullivan <mjs2@gte.com>
Subject: Q: DBD Informix 0.62 selectall_arrayref returns garbled data
Message-Id: <380499AF.1A37BBDC@gte.com>

I just "upgraded" my DBI and DBD Informix to 1.13 and 0.62 in Perl
5.004_04. I am running on AIX 4.21
After the upgrade, my script which gathers data from the db fails in the

following strange way:

my $ary_ref = selectall_arrayref( "select id, name from foo order by
id" );
for my $row ( @{$ary_ref} ) {
  print "$$row[0] $$row[1]\n";
}
# NOTE this is a typed in example so it might have a typo

Produces:

1 longentry1
2 shortnrty1
3 barbarrty1
4 dumbarrty1

should have been

1 longentry1
2 short
3 barbar
4 dumb

It looks like the fetching loop is not initializing the buffer. Is this
a known problem or am I missing a semicolon somewhere.

Thanks
    mike






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

Date: Wed, 13 Oct 1999 07:44:33 GMT
From: erik_lembke@my-deja.com
Subject: Q: DBI::Msql Problem
Message-Id: <7u1d8v$7vq$1@nnrp1.deja.com>

Hi folks,

I tried to get all column names from a msql table using the
DBI module.

Msql has no SHOW command, so I used following code:

sub get_keys_of_table {
my $table = shift;
my $array = shift;

	my $sth = $dbh->prepare("SELECT * from $table")
		|| die "Can't prepare statements: $DBI:errstr";
	my $rc = $sth->execute
		|| die "Can't execute statement: $DBI::errstr";

	my $list = $sth->fetchrow_hashref;
	@$array = keys %$list;

return 1;
}

Problem with this simple function is that the talbe must not be empty.

Is this a bug or a feature?

cheers Erik





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


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

Date: Wed, 13 Oct 1999 09:46:17 -0700
From: mbudash@sonic.net (Michael Budash)
Subject: Re: Q: DBI::Msql Problem
Message-Id: <mbudash-1310990946170001@ppp-216-101-156-232.dialup.snrf01.pacbell.net>

In article <7u1d8v$7vq$1@nnrp1.deja.com>, erik_lembke@my-deja.com wrote:

>Hi folks,
>
>I tried to get all column names from a msql table using the
>DBI module.
>
>Msql has no SHOW command, so I used following code:
>
>sub get_keys_of_table {
>my $table = shift;
>my $array = shift;
>
>        my $sth = $dbh->prepare("SELECT * from $table")
>                || die "Can't prepare statements: $DBI:errstr";
>        my $rc = $sth->execute
>                || die "Can't execute statement: $DBI::errstr";
>
>        my $list = $sth->fetchrow_hashref;
>        @$array = keys %$list;
>
>return 1;
>}
>
>Problem with this simple function is that the talbe must not be empty.
>
>Is this a bug or a feature?
>
>cheers Erik

per the docs for DBD::mysql.pm, try:

$dbh->prepare("LISTFIELDS $table")

good luck!
-- 
Michael Budash ~~~~~~~~~~ mbudash@sonic.net


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

Date: Thu, 14 Oct 1999 07:23:42 GMT
From: erik_lembke@my-deja.com
Subject: Re: Q: DBI::Msql Problem
Message-Id: <7u40dn$4o2$1@nnrp1.deja.com>

In article <mbudash-1310990946170001@ppp-216-101-156-
232.dialup.snrf01.pacbell.net>,
  mbudash@sonic.net (Michael Budash) wrote:
> In article <7u1d8v$7vq$1@nnrp1.deja.com>, erik_lembke@my-deja.com
wrote:
>
> >Hi folks,
> >
> >I tried to get all column names from a msql table using the
> >DBI module.
> >
> >Msql has no SHOW command, so I used following code:
> >
> >sub get_keys_of_table {
> >my $table = shift;
> >my $array = shift;
> >
> >        my $sth = $dbh->prepare("SELECT * from $table")
> >                || die "Can't prepare statements: $DBI:errstr";
> >        my $rc = $sth->execute
> >                || die "Can't execute statement: $DBI::errstr";
> >
> >        my $list = $sth->fetchrow_hashref;
> >        @$array = keys %$list;
> >
> >return 1;
> >}
> >
> >Problem with this simple function is that the talbe must not be
empty.
> >
> >Is this a bug or a feature?
> >
> >cheers Erik
>
> per the docs for DBD::mysql.pm, try:
>
> $dbh->prepare("LISTFIELDS $table")
>
> good luck!

Thanx for the tip,

i tried following code:

my $dbh = DBI->connect('DBI:mSQL:test', undef, undef, {
	RaiseError => 1
});

 my $sth = $dbh->prepare("LISTFIELDS materialclass") || die "Can't
prepare statement: $DBI::errstr";
 my $rc = $sth->execute || die "Can't execute statement: $DBI::errstr";
 my $test = $sth->fetchall_arrayref;
  foreach (@$test) {
  	print $_ . "\n";
  }


but i didn't get any output.

any ideas?

> --
> Michael Budash ~~~~~~~~~~ mbudash@sonic.net
>


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


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

Date: Thu, 14 Oct 1999 10:34:45 -0700
From: mbudash@sonic.net (Michael Budash)
Subject: Re: Q: DBI::Msql Problem
Message-Id: <mbudash-1410991034450001@ppp-216-101-156-52.dialup.snrf01.pacbell.net>

In article <7u40dn$4o2$1@nnrp1.deja.com>, erik_lembke@my-deja.com wrote:

>In article <mbudash-1310990946170001@ppp-216-101-156-
>232.dialup.snrf01.pacbell.net>,
>  mbudash@sonic.net (Michael Budash) wrote:
>> In article <7u1d8v$7vq$1@nnrp1.deja.com>, erik_lembke@my-deja.com
>wrote:
>>
>> >Hi folks,
>> >
>> >I tried to get all column names from a msql table using the
>> >DBI module.
>> >
>> >Msql has no SHOW command, so I used following code:
>> >
>> >sub get_keys_of_table {
>> >my $table = shift;
>> >my $array = shift;
>> >
>> >        my $sth = $dbh->prepare("SELECT * from $table")
>> >                || die "Can't prepare statements: $DBI:errstr";
>> >        my $rc = $sth->execute
>> >                || die "Can't execute statement: $DBI::errstr";
>> >
>> >        my $list = $sth->fetchrow_hashref;
>> >        @$array = keys %$list;
>> >
>> >return 1;
>> >}
>> >
>> >Problem with this simple function is that the talbe must not be
>empty.
>> >
>> >Is this a bug or a feature?
>> >
>> >cheers Erik
>>
>> per the docs for DBD::mysql.pm, try:
>>
>> $dbh->prepare("LISTFIELDS $table")
>>
>> good luck!
>
>Thanx for the tip,
>
>i tried following code:
>
>my $dbh = DBI->connect('DBI:mSQL:test', undef, undef, {
>        RaiseError => 1
>});
>
> my $sth = $dbh->prepare("LISTFIELDS materialclass") || die "Can't
>prepare statement: $DBI::errstr";
> my $rc = $sth->execute || die "Can't execute statement: $DBI::errstr";
> my $test = $sth->fetchall_arrayref;
>  foreach (@$test) {
>        print $_ . "\n";
>  }
>
>
>but i didn't get any output.
>
>any ideas?

using v2.0400, i changed this:

   my $test = $sth->fetchall_arrayref;

to this:

   my $test = $sth->{'NAME'};

and it worked fine.

hth-
-- 
Michael Budash ~~~~~~~~~~ mbudash@sonic.net


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

Date: Thu, 14 Oct 1999 17:26:30 GMT
From: Chris@fatlarry.karoo.co.uk (Chris Burton)
Subject: QMAIL AND PERL
Message-Id: <380611af.8858485@news.karoo.co.uk>

Hi All

i am trying to get QMAIL and PERL to work together  but no luck,

i have qmaile running perl via a .qmail file but i can't seem to use
the enviroment varibles - e.g HOST LOCAL RECIPIENT

Please help me

it's best if you contact me at chris@fatlarry.karoo.co.uk

Thanks

Chris


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

Date: 15 Oct 1999 09:28:11 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: QMAIL AND PERL
Message-Id: <3806e59b_1@newsread3.dircon.co.uk>

Chris Burton <Chris@fatlarry.karoo.co.uk> wrote:
> Hi All
> 
> i am trying to get QMAIL and PERL to work together  but no luck,
> 
> i have qmaile running perl via a .qmail file but i can't seem to use
> the enviroment varibles - e.g HOST LOCAL RECIPIENT
> 

In what way cant you use it ?  A snippet of code might be useful - with
the information here it is impossible to know where the problem lies.

/J\
-- 
"You look lovely this evening. Have you decreased in mass?" - Alien,
The Simpsons


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

Date: Fri, 15 Oct 1999 14:53:07 +0600
From: Chris Burton <Chris@fatlarry.karoo.co.uk>
Subject: Re: QMAIL AND PERL
Message-Id: <cuoGOJ+424uGP=IqKC+XM2E4gjVt@4ax.com>

Ok my problem is 

i have made a .qmail file that executes a perl programme called
rules.pl, i have been told that when this type of thing happens qmail
passes 3 variables, 1 HOST 2 LOCAL 3 RECIPIENT. i can't get the perl
programme to write the RECIPIENT variable to a file called email
my print statement contains this

open (emailfile,">>email");
print emaiilfile $ENV{'RECEIPIENT'};
close (emailfile);

Please help because i'm stuck big time

Thanks for any help in advance


Chris


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

Date: 15 Oct 1999 11:46:33 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: QMAIL AND PERL
Message-Id: <x7bta0iq3q.fsf@home.sysarch.com>

>>>>> "CB" == Chris Burton <Chris@fatlarry.karoo.co.uk> writes:

  CB> i have made a .qmail file that executes a perl programme called
  CB> rules.pl, i have been told that when this type of thing happens qmail
  CB> passes 3 variables, 1 HOST 2 LOCAL 3 RECIPIENT. i can't get the perl
  CB> programme to write the RECIPIENT variable to a file called email
  CB> my print statement contains this

you should say environment variables. i know that is what it does and
your code shows it below. but it pays to be accurate in your descriptions.

  CB> open (emailfile,">>email");
  CB> print emaiilfile $ENV{'RECEIPIENT'};
  CB> close (emailfile);

i have to wonder if that was cut and pasted or retyped, for you have at
least 2 major typos in that code.

that should be a clue how to fix it.

uri


-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


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

Date: Fri, 15 Oct 1999 12:09:39 -0400
From: "Dave Kaufman" <david@gigawatt.com>
Subject: Re: QMAIL AND PERL
Message-Id: <8lIN3.720$25.31646@nntp1>

Chris Burton <Chris@fatlarry.karoo.co.uk> wrote...
> ... i can't get the perl
> programme to write the RECIPIENT variable to a file called email
>
> open (emailfile,">>email");

first, you should be checking the return value from open() to see if it
failed, for instance:

open (emailfile,">>email") or die "error opening emailfile: $!";
# $! will contain the last O/S error such as "permission denied" etc...

> print emaiilfile $ENV{'RECEIPIENT'};

might be a cut-and-paste error, but you've misspelled RECIPIENT in that
line...

hth,

-dave




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

Date: Tue, 12 Oct 1999 13:42:53 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: Question about AlarmCall
Message-Id: <Pine.GSO.4.10.9910121336540.19155-100000@user2.teleport.com>

On Tue, 12 Oct 1999, Ilya wrote:

> Regarding the AlarmCall module: Can it possibly take a normal
> statement, not a function, as an argument?

Nothing in Perl takes either statements or functions as an argument. You
may be thinking of a coderef. Cheers!

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



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

Date: Wed, 13 Oct 1999 19:49:54 GMT
From: Marcel Grunauer <marcel.grunauer@lovely.net>
Subject: qw and x operator
Message-Id: <me0EOIYsk3czLbMlSVwXqN=zmYzA@4ax.com>

Hi!

I've come across a behavior of the qw() operator in conjunction with
the x operator that I can't explain. I've checked perlop.


#!/usr/bin/perl -w
use strict;
my %test;
@test{qw/a b c/} = ('n') x 3;
print "$_ => $test{$_}\n" foreach keys %test;


produces, as expected:

a => n
b => n
c => n


If I replace the hash slice line above with

@test{qw/a b c/} = qw/n/ x 3;

it produces:

Bareword "x" not allowed while "strict subs" in use at ...
Unquoted string "x" may clash with future reserved word at ... 
syntax error at ..., near "qw/n/ x "
Number found where operator expected at ..., near "x 3"
	(Do you need to predeclare x?)

The following two work ok:

@test{qw/a b c/} = (qw/n/) x 3;
@test{qw/a b c/} = (split(' ', q/n/)) x 3;

This is what perlop says:

    qw/STRING/
            Returns a list of the words extracted out of STRING,
            using embedded whitespace as the word delimiters. It is
            exactly equivalent to
                split(' ', q/STRING/);

Now, I thought that qw/n/ returns "a list of the words extracted out
of 'n'", which I understood to be a list with one element. But that
doesn't seem to be the case.

Isn't qw/n/ equivalent to ('n') ?
Isn't (qw/n/) equivalent to (('n')), which flattens to ('n') and so
just serves to separate the qw() from the x operator? But why is that
necessary?

Where am I going wrong?

Thanks


-- 
Marcel, Perl Padawan
sub AUTOLOAD{$_=$AUTOLOAD;s;.*::;;;y;_; ;;print}&Just_Another_Perl_Hacker;


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

Date: Wed, 13 Oct 1999 14:34:56 -0700
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: qw and x operator
Message-Id: <Pine.GSO.4.10.9910131433190.25558-100000@user2.teleport.com>

On Wed, 13 Oct 1999, Marcel Grunauer wrote:

> @test{qw/a b c/} = qw/n/ x 3;

> Bareword "x" not allowed while "strict subs" in use at ...

Looks as if that should work, but it's a parsing error. If anyone actually
wrote such code in real life, I'd file a bug report and see what comes of
it. :-)

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



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

Date: 13 Oct 1999 16:59:01 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: qw and x operator
Message-Id: <slrn80a04l.nk2.abigail@alexandra.delanet.com>

Marcel Grunauer (marcel.grunauer@lovely.net) wrote on MMCCXXXIV September
MCMXCIII in <URL:news:me0EOIYsk3czLbMlSVwXqN=zmYzA@4ax.com>:
|| 
|| @test{qw/a b c/} = qw/n/ x 3;
|| 
|| it produces:
|| 
|| Bareword "x" not allowed while "strict subs" in use at ...
|| Unquoted string "x" may clash with future reserved word at ... 
|| syntax error at ..., near "qw/n/ x "
|| Number found where operator expected at ..., near "x 3"
|| 	(Do you need to predeclare x?)

Well, to be truely pedantic, the documentation doesn't define this
behaviour. For list context, only the behaviour when the left operand
is in parenthesis is defined:

       Binary "x" is the repetition operator.  In scalar context,
       it returns a string consisting of the left operand
       repeated the number of times specified by the right
       operand.  In list context, if the left operand is a list
       in parentheses, it repeats the list.

           print '-' x 80;             # print row of dashes

           print "\t" x ($tab/8), ' ' x ($tab%8);      # tab over

           @ones = (1) x 80;           # a list of 80 1's
           @ones = (5) x @ones;        # set all elements to 5


The examples however, suggest that Perl does something useful with
'x' in list context when the left operand is not in parenthesis.

Based on that, I would assume that

    @test{qw/a b c/} = qw/n/ x 3;

would lead to:

    %test = (a => '111', b => undef, c => undef);

This is because the left operand of x will be evaluated in scalar context,
hence the split qw// does is done to @_, and the amount of produced fields
is returned. This being 1, leads to 1 x 3 which is '111'.

It looks to me that you have stumbled upon a bug. Perhaps you should use
perlbug.

Your code, however, needs the parens.


Abigail
-- 
%0=map{reverse+chop,$_}ABC,ACB,BAC,BCA,CAB,CBA;$_=shift().AC;1while+s/(\d+)((.)
(.))/($0=$1-1)?"$0$3$0{$2}1$2$0$0{$2}$4":"$3 => $4\n"/xeg;print#Towers of Hanoi


  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----


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

Date: Thu, 14 Oct 1999 05:07:00 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: qw and x operator
Message-Id: <kf64u7.tp2.ln@magna.metronet.com>

Tom Phoenix (rootbeer@redcat.com) wrote:
: On Wed, 13 Oct 1999, Marcel Grunauer wrote:

: > @test{qw/a b c/} = qw/n/ x 3;

: > Bareword "x" not allowed while "strict subs" in use at ...

: Looks as if that should work, but it's a parsing error. If anyone actually
: wrote such code in real life, I'd file a bug report and see what comes of
: it. :-)


   Note also that you get the same messages if you replace the
   line above with just:

x 3;

   Looks like perl is taking "@test{qw/a b c/} = qw/n/" as an
   entire statement, and then resuming the parse...


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

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


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