For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2004 > warn ?!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author warn ?!
Jayakumar Rajagopal

2004-03-26, 11:15 pm

Hi,
In my program , $sth->execute fails since oracle tables already =
present. It is very natural. But the waring message output line 37 is =
different(just behaves as print) from same kind of message at line 57. =
Any suggestions please..
regards,
Jay=20

regression Testing table( default : reg_test_cols) name :=20
Not able to create reg_test_cols <?????? ***** this =
output is not similar as line 57 *** >
discrepancy report table ( default : disc_table) name :=20
Not able to create disc_table
at /tmp/selfextract.XXXXXX/install line 57, <> chunk 5. =20

26 $query=3Dqq { create table reg_test_cols
27 ( tabname varchar2(40),
28 colname varchar2(40),
29 iskey varchar2(1),
30 tocomp varchar2(1),
31 primary key (tabname,colname) )
32 };
33
34 $sth=3D$dbh->prepare($query)|| warn " $DBI::errstr \n";
35 unless ($sth->execute() )
36 {
37 warn " Not able to create reg_test_cols \n" ;
38 }
39 else
40 {
41 print " Successfully created table reg_test_cols \n";
42 }
43=20
44 =20
45 =20
46 $query=3Dqq { create table disc_table
47 ( tabname varchar2(40),
48 querycols varchar2(2000),
49 cond varchar2(2000),
50 rerun varchar2(1),
51 seq number(4),
52 primary key (tabname,cond,seq) )
53 };
54 $sth=3D$dbh->prepare($query) || warn " $DBI::errstr\n ";
55 unless ($sth->execute() )
56 {
57 warn " Not able to create disc_table\n " ;
58 }
Alok Bhatt

2004-03-26, 11:15 pm

Hi Jai,

A similar question was posted just yesterday. The
difference between between the two lines (37 and 57)
is that the second line contains a space after "\n".
As pointed earlier, you can take take it as a
debugging option (ie remove \n or add a space and perl
prints out the line no. too)

:-)
Alok Bhatt

--- Jayakumar Rajagopal <JRajagopal@eagleinvsys.com>
wrote:
> Hi,
> In my program , $sth->execute fails since
> oracle tables already present. It is very natural.
> But the waring message output line 37 is
> different(just behaves as print) from same kind of
> message at line 57. Any suggestions please..
> regards,
> Jay
>
> regression Testing table( default : reg_test_cols)
> name :
> Not able to create reg_test_cols
> <?????? ***** this output is not similar as line 57
> *** >
> discrepancy report table ( default : disc_table)
> name :
> Not able to create disc_table
> at /tmp/selfextract.XXXXXX/install line 57, <>
> chunk 5.
>
> 26 $query=qq { create table reg_test_cols
> 27 ( tabname varchar2(40),
> 28 colname varchar2(40),
> 29 iskey varchar2(1),
> 30 tocomp varchar2(1),
> 31 primary key
> (tabname,colname) )
> 32 };
> 33
> 34 $sth=$dbh->prepare($query)|| warn "
> $DBI::errstr \n";
> 35 unless ($sth->execute() )
> 36 {
> 37 warn " Not able to create
> reg_test_cols \n" ;
> 38 }
> 39 else
> 40 {
> 41 print " Successfully created table
> reg_test_cols \n";
> 42 }
> 43
> 44
> 45
> 46 $query=qq { create table disc_table
> 47 ( tabname varchar2(40),
> 48 querycols varchar2(2000),
> 49 cond varchar2(2000),
> 50 rerun varchar2(1),
> 51 seq number(4),
> 52 primary key
> (tabname,cond,seq) )
> 53 };
> 54 $sth=$dbh->prepare($query) || warn "
> $DBI::errstr\n ";
> 55 unless ($sth->execute() )
> 56 {
> 57 warn " Not able to create
> disc_table\n " ;
> 58 }
>
> --
> To unsubscribe, e-mail:
> beginners-unsubscribe@perl.org
> For additional commands, e-mail:
> beginners-help@perl.org
> <http://learn.perl.org/>
> <http://learn.perl.org/first-response>
>
>



__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html
R. Joseph Newton

2004-03-27, 11:53 pm

Alok Bhatt wrote:

>
> Hi Jai,


Hi Alok,

Please don't top-post. Instead, follow the material you are responding to, and
trim extraneous matter.

> ....
> ...


>
> A similar question was posted just yesterday. The
> difference between between the two lines (37 and 57)
> is that the second line contains a space after "\n".
> As pointed earlier, you can take take it as a
> debugging option (ie remove \n or add a space and perl
> prints out the line no. too)
>
> :-)
> Alok Bhatt


Not exactly. It is not the newline "\n" that would give the full output of the
warn function, but the $! variable, which contains the most recent warning or
error message. It is pretty much the same as with die(). The problem was not
space after the newline, but the presence of the newline. The $! variable
should not be followed by anything. If left as the last element of the warn or
die message, it will be unrolled to the line number in the program as well as
the line number in the currently selected filehandle.

warn "Something went wrong in function_name(), while doing something dangerous:
$!";

Greetings! E:\d_drive\perlstuff>perl -w
open IN, 'some_nonexistent_filename' or warn "Something went wrong in main(), "
.. "while doing something dangerous:\n $!";
my $stuff = <IN>;
print $stuff;

^Z
Something went wrong in main(), while doing something dangerous:
No such file or directory at - line 1.
readline() on closed filehandle IN at - line 3.
Use of uninitialized value in print at - line 4.


Joseph

James Edward Gray II

2004-03-27, 11:53 pm

On Mar 27, 2004, at 12:10 AM, R. Joseph Newton wrote:

> Not exactly. It is not the newline "\n" that would give the full
> output of the
> warn function, but the $! variable, which contains the most recent
> warning or
> error message.


I believe you are confusing two often paired, but not otherwise related
features of Perl.

warn() is a tool for delivering non-fatal error messages. It behaves
differently depending if the provided message does or doesn't end in a
newline character, as has been discussed in this thread.

$! is a Perl interface to C's "errno" variable. It is set by failed
system/library calls.

While it may often be useful to issue a warn()ing including $!, it is
by no means required. It may be useful to issue warn()ings including
many variables. $@ is a common example, in fact, it's the default
message warn() will display if not provided one. You may want to use
your own variables, so simply print a message in your own words about
what happened. That's what warn() is for.

James

R. Joseph Newton

2004-03-27, 11:53 pm

James Edward Gray II wrote:

> On Mar 27, 2004, at 12:10 AM, R. Joseph Newton wrote:
>
>
> I believe you are confusing two often paired, but not otherwise related
> features of Perl.


Actually, I was starting to think that I had misstated the above, by saying
"output of the warn function, but I think I was closer to the mark. You can
print $! and get the error message, but the line information is the product of
warn or die:

open IN, 'some_nonexistent_filename' or print $!;
^Z
Name "main::IN" used only once: possible typo at - line 1. # built-in compiler
warning
No such file or directory # simply printing $!

open IN, 'some_nonexistent_filename' or warn $!;
^Z
...
No such file or directory at - line 1.

>
> warn() is a tool for delivering non-fatal error messages. It behaves
> differently depending if the provided message does or doesn't end in a
> newline character, as has been discussed in this thread.
>
> $! is a Perl interface to C's "errno" variable. It is set by failed
> system/library calls.
>
> While it may often be useful to issue a warn()ing including $!, it is
> by no means required.


In this case, James, it is what the OP was looking for. He specifically saw a
problem with having the warning message come out as a simple print statement.
I'm not terribly concerned about the internal mplementation. I am speaking of
the effect. Unless I am missing something about what the OP wnated here.

> It may be useful to issue warn()ings including
> many variables. $@ is a common example, in fact, it's the default
> message warn() will display if not provided one. You may want to use
> your own variables, so simply print a message in your own words about
> what happened. That's what warn() is for.


I agree, which is why I tend to recommend the darn-near canonical form I
showed. Interestingly enough, it is almost the same generalized warning
produced by $@:
Greetings! E:\d_drive\perlStuff\hdr>perl -w
open IN, 'some_nonexistent_filename' or warn $@;
^Z
Name "main::IN" used only once: possible typo at - line 1.
Warning: something's wrong at - line 1.
except that the $@ adds the Warning: prefix.

It seems pretty clear to me, that although modular in their function, these Perl
built-in variables were precisely designed to work with the warn and die
functions. I'll hold with my essential point--that you get a lot more useful
information out of your warning by havng the $! at the end of your custom
message. I would recommend always using such custom messages also, and putting
design thought into them. That is why I suggested having them include the name
of the function containing them, and why I generally specify the operation being
attempted.

Joseph


James Edward Gray II

2004-03-27, 11:53 pm

On Mar 27, 2004, at 1:32 PM, R. Joseph Newton wrote:

> It seems pretty clear to me, that although modular in their function,
> these Perl
> built-in variables were precisely designed to work with the warn and
> die
> functions. I'll hold with my essential point--that you get a lot more
> useful
> information out of your warning by havng the $! at the end of your
> custom
> message.


You still sound pretty to me. ;)

Fact: If the warn() message ends is a newline character, the line
number is not appended.

Fact: This has nothing to do with ANY variables, it is the way warn()
is designed.

Fact: This is the original question of this thread and what most of
the replies address. You might like to reread them at this point.

Please run these two one liners, observing the differences in output,
for a good example of this:

perl -e 'warn "Test Warning"'

perl -e 'warn "Test Warning\n"'

Hope that clears things up.

James

Randy W. Sims

2004-03-27, 11:53 pm

James Edward Gray II wrote:

> Fact: This has nothing to do with ANY variables, it is the way warn()
> is designed.


Trivia:

Did you know that $! does NOT contain an error string. It contains the
error *number*. The only reason you see a error sting is that it has an
overloaded stringification operator that calls strerror() on the numeric
value that it contains.

# assuming the file 'foo' does not exist in current directory.

perl -e 'open FOO,"foo" or print $!+0'
=> 2

perl -MPOSIX -e 'open FOO,"foo";print strerror $!+0'
=> No such file or directory

Randy.

R. Joseph Newton

2004-03-27, 11:53 pm

James Edward Gray II wrote:

> On Mar 27, 2004, at 1:32 PM, R. Joseph Newton wrote:
>
>
> You still sound pretty to me. ;)


> Fact: If the warn() message ends is a newline character, the line
> number is not appended.
>
> Fact: This has nothing to do with ANY variables, it is the way warn()
> is designed.


I see. It does seem a bit quirky, though, to use whitespace in this way. I
wasn't really aware of this oddity in the design of warn(). I guess it makes
sense, somehow. It makes more sense, though, to just provide some context
iformation, then get the more specific error or warning message ctained in the
variable. Whatever the particular features that come and go with the choice of
whitespace, it makes more sense to explicitly call for the information provided
in $!.

>
>
> Fact: This is the original question of this thread and what most of
> the replies address. You might like to reread them at this point.
>
> Please run these two one liners, observing the differences in output,
> for a good example of this:


Sorry, they are non-portable:
reetings! E:\d_drive\perlStuff>perl -e 'warn "Test Warning"'
an't find string terminator "'" anywhere before EOF at -e line 1.

>
>
> perl -e 'warn "Test Warning"'
>
> perl -e 'warn "Test Warning\n"'
>
> Hope that clears things up.
>
> James


Got it. Nice point. Now back to business. You get a lot more information back
by asking for information.

Joseph

R. Joseph Newton

2004-03-27, 11:53 pm

"Randy W. Sims" wrote:

> James Edward Gray II wrote:
>
>
> Trivia:
>
> Did you know that $! does NOT contain an error string. It contains the
> error *number*. The only reason you see a error sting is that it has an
> overloaded stringification operator that calls strerror() on the numeric
> value that it contains.
>
> # assuming the file 'foo' does not exist in current directory.
>
> perl -e 'open FOO,"foo" or print $!+0'
> => 2
>
> perl -MPOSIX -e 'open FOO,"foo";print strerror $!+0'
> => No such file or directory
>
> Randy.


Cool. Somehow, though It seems {6 * 1 == 12 / 2}-ish. By the time the variable
is effectively accessed, the text is there.

Greetings! E:\d_drive\perlStuff>perl
open IN, 'some_nonexistent_filename' or print $!;
^Z
No such file or directory

What is intriguing to me in this is that an overloaded operator wouuld be
attched to a variable. this sounds like it gets into prtions of Perl that I've
never really delved into. Is $! actually sored as a number?

Joseph

John W. Krahn

2004-03-27, 11:53 pm

"Randy W. Sims" wrote:
>
> James Edward Gray II wrote:
>
>
> Trivia:
>
> Did you know that $! does NOT contain an error string. It contains the
> error *number*. The only reason you see a error sting is that it has an
> overloaded stringification operator that calls strerror() on the numeric
> value that it contains.


Did you know that $! is one of those "magical" variables that contains
both a number and a string at the same time! AMAZING BUT TRUE! Here is
an exerpt from the perl source:

case '!':
#ifdef VMS
sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
sv_setpv(sv, errno ? Strerror(errno) : "");
#else
{
int saveerrno = errno;
sv_setnv(sv, (NV)errno);
#ifdef OS2
if (errno == errno_isOS2 || errno == errno_isOS2_set)
sv_setpv(sv, os2error(Perl_rc));
else
#endif
sv_setpv(sv, errno ? Strerror(errno) : "");
errno = saveerrno;
}
#endif
SvNOK_on(sv); /* what a wonderful hack! */
break;


Where you can see that sv_setpv() sets the string part of $! and
sv_setnv() sets the numeric part.


John
--
use Perl;
program
fulfillment
Randy W. Sims

2004-03-28, 10:10 pm

On 3/27/2004 10:12 PM, John W. Krahn wrote:
> "Randy W. Sims" wrote:
>
>
>
> Did you know that $! is one of those "magical" variables that contains
> both a number and a string at the same time! AMAZING BUT TRUE! Here is
> an exerpt from the perl source:


I regret to inform you that you are correct :-/

perl -MDevel::P -e '$e=$!=2;print Dump($!)'

SV = PVMG(0x1c1bc58) at 0x1aaeb94
REFCNT = 1
FLAGS = (GMG,SMG,pNOK,pPOK)
IV = 2
NV = 2
PV = 0x1aa0128 "No such file or directory"\0
CUR = 25
LEN = 26
MAGIC = 0x1abb470
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = '\0'
MG_OBJ = 0x1aaeba0
MG_LEN = 1
MG_PTR = 0x1aa014c "!"

I don't know where I got it from that strerror was called in an
overriden stringization method, but I would have sworn to it until I saw
your message. In fact, I /still/ believe it to be true... just in some
other context: maybe a different variable? maybe it was ruby instead of
perl? I *know* I got that from somewhere...

Oh, well. Thanks for the correction John.

BTW, anyone know why Devel::P won't print the correct data unless the
variable is first used in an expression? (i.e. the above one-liner won't
work if you take out the '$e=' part of the expression.

Regards,
Randy.

Randy W. Sims

2004-03-28, 10:10 pm

On 3/27/2004 9:15 PM, R. Joseph Newton wrote:

> Cool. Somehow, though It seems {6 * 1 == 12 / 2}-ish. By the time the variable
> is effectively accessed, the text is there.


I never said that it was usefull ;-)

> Greetings! E:\d_drive\perlStuff>perl
> open IN, 'some_nonexistent_filename' or print $!;
> ^Z
> No such file or directory
>
> What is intriguing to me in this is that an overloaded operator wouuld be
> attched to a variable. this sounds like it gets into prtions of Perl that I've
> never really delved into. Is $! actually sored as a number?


As John pointed out, it is stored as both a number and a string. Here is
a small example of how you can do something similar in your own code.
See 'perldoc overload' for more info.

#!/usr/bin/perl

package NumStr;

use strict;
use warnings;

use overload
'""' => \&stringify,
'0+' => \&numify,
fallback => 1;

sub new {
my ($class, $num, $str) = @_;
return bless {
num => $num || 0,
str => $str || '',
}, $class
}

sub numify {
my $self = shift;
return $self->{num};
}

sub stringify {
my $self = shift;
return $self->{str};
}

package main;

use strict;
use warnings;

my $ns = NumStr->new(42, 'The Answer to Life, the Universe, and
Everything');
printf("as number = '%d', as string = '%s'\n", $ns, $ns);

__END__

You can simulate more closely with XS:

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

int
_get(pTHX_ SV *sv, MAGIC *mg)
{
/*
IF this is a std error lookup
get errno and store in NV slot,
then call strerror and store in PV slot.
ELSE
a custom error has been stored; do nothing
END
*/
return 1;
}

int
_set(pTHX_ SV *sv, MAGIC *mg)
{
/*
store custom errno in NV slot
set global flag so _get() knows this is not a std error lookup
*/
return 1;
}

MODULE = StrError PACKAGE = StrError

void
create(num, str)
SV *num;
SV *str;
PREINIT:
SV *sv;
MAGIC *mg;
CODE:
sv = get_sv("StrError", TRUE); /* creates $main::StrError */

sv_magic(sv, 0, '\0', "StrError", 8);

mg = mg_find(sv, '\0');
if (mg != NULL) {
mg->mg_virtual->svt_get = &_get;
mg->mg_virtual->svt_set = &_set;
}

sv_setnv(sv, SvNV(num));
sv_setpv(sv, SvPV_nolen(str));
SvNOK_on(sv);


R. Joseph Newton

2004-03-28, 10:10 pm

"Randy W. Sims" wrote:

> #!/usr/bin/perl
>
> package NumStr;


This makes sense to me. As a package, it makes sense to have overloaded operations.
This is why I was asking if the value was really stored as a number.

> use strict;
>


....

> printf("as number = '%d', as string = '%s'\n", $ns, $ns);
>
> __END__
>
> You can simulate more closely with XS:
>
> #include "EXTERN.h"
> #include "perl.h"
> #include "XSUB.h"


Looks interesting, but I am not really familiar enough with XS to know what to make of
it.

I'll look at it again when I've had some time to check out the XS libraries.

Joseph

Paul Johnson

2004-03-28, 10:10 pm

On Sun, Mar 28, 2004 at 09:41:09AM -0500, Randy W. Sims wrote:
> On 3/27/2004 9:15 PM, R. Joseph Newton wrote:
>
>
> As John pointed out, it is stored as both a number and a string. Here is
> a small example of how you can do something similar in your own code.
> See 'perldoc overload' for more info.
>
> #!/usr/bin/perl
>
> package NumStr;


[ snip ]

> my $ns = NumStr->new(42, 'The Answer to Life, the Universe, and Everything');
> printf("as number = '%d', as string = '%s'\n", $ns, $ns);


use Scalar::Util "dualvar";

my $ns = dualvar 42, 'The Answer to Life, the Universe, and Everything';
printf("as number = '%d', as string = '%s'\n", $ns, $ns);

> You can simulate more closely with XS:


dualvar is XS.

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net
Wc -Sx- Jones

2004-03-28, 10:11 pm

>
> my $ns = dualvar 42, 'The Answer to Life, the Universe, and Everything';
> printf("as number = '%d', as string = '%s'\n", $ns, $ns);



Wow, learnt something new :)

use Scalar::Util "dualvar";

my $que = dualvar 54, 'What do you get when you multiply 6 by 9';
printf("\nas number = '%d', as string = '%s'\n", $que, $que);


I learned that I need to re-read all of
Douglas Adams books...
John W. Krahn

2004-03-28, 11:30 pm

"Randy W. Sims" wrote:
>
> On 3/27/2004 10:12 PM, John W. Krahn wrote:
>
> I regret to inform you that you are correct :-/
>
> perl -MDevel::P -e '$e=$!=2;print Dump($!)'
>
> [snip]
>
> I don't know where I got it from that strerror was called in an
> overriden stringization method, but I would have sworn to it until I saw
> your message. In fact, I /still/ believe it to be true... just in some
> other context: maybe a different variable? maybe it was ruby instead of
> perl? I *know* I got that from somewhere...


Well, if you are still using Perl version 1 then it is only a number,
but as of Perl version 2.0 up to and including the current version
(5.8.3) it is a number and a string. Also note that the variable $^E
has the same property.


John
--
use Perl;
program
fulfillment
Randy W. Sims

2004-03-29, 3:33 pm

James Edward Gray II wrote:

> Fact: This has nothing to do with ANY variables, it is the way warn()
> is designed.


Trivia:

Did you know that $! does NOT contain an error string. It contains the
error *number*. The only reason you see a error sting is that it has an
overloaded stringification operator that calls strerror() on the numeric
value that it contains.

# assuming the file 'foo' does not exist in current directory.

perl -e 'open FOO,"foo" or print $!+0'
=> 2

perl -MPOSIX -e 'open FOO,"foo";print strerror $!+0'
=> No such file or directory

Randy.
Randy W. Sims

2004-03-30, 4:38 am

Paul Johnson wrote:

> use Scalar::Util "dualvar";
>
> my $ns = dualvar 42, 'The Answer to Life, the Universe, and Everything';
> printf("as number = '%d', as string = '%s'\n", $ns, $ns);


doh! I /knew/ there was a module that did that. I looked for it, but I
just couldn't seem to find it.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com