Return values more than 256?

2013-03-07 Thread WFB
Hi, List,

First thank you for the help you provide. I follow this list quite a time
and learned a lot.

My problem, or question, respectively:
To test our software I use perl to start it several times. My perl script
gather some information and start then the program with different
parameters.

It works very well, but I have a problem with the return values of our
program. This return codes are all in an area from 55000 to 6.
I use open to invoke our program and print the output. Finally I use the $?
variable and print the error code in case of an error.

sub start_test_runner {
my ($tr = shift, $tr_params) = @_;
 my $pid = open(my $trexe, $tr \$tr_params\ |) or die Could not start
TestRunner. $!\n;
 while($trexe) {
 print $_ if $verbose;
}
waitpid($pid, 0);
close($trexe);
my $tr_ret = ($?8);

return $tr_ret;
}

now I read the perldoc perlvar section about $? and realised that only the
above 8 bits are used for the return value. The first 8 bit are used for
the system codes. As far as I that understand fit just 256 values in those
8 bits.
Obviously, the error codes given back from my scripts are always wrong.
For example, if the error code is 55028 I get 62464. With right shift the
script prints 244.

Is there another way in perl to get the proper return codes?

Thanks,
Wolfgang


Re: Return values more than 256?

2013-03-07 Thread Brandon McCaig
On Thu, Mar 07, 2013 at 10:21:40AM +0100, WFB wrote:
 Hi, List,

Hello,

 To test our software I use perl to start it several times. My
 perl script gather some information and start then the program
 with different parameters.
 
 It works very well, but I have a problem with the return values
 of our program. This return codes are all in an area from 55000
 to 6. I use open to invoke our program and print the
 output. Finally I use the $? variable and print the error code
 in case of an error.
 
 sub start_test_runner {
 my ($tr = shift, $tr_params) = @_;

I don't see a need for shift here. You can just assign the
arguments array to your list of parameters.

  my ($tr, $tr_params) = @_;

  my $pid = open(my $trexe, $tr \$tr_params\ |) or die Could not start
 TestRunner. $!\n;

Instead of escaping the double-quotes consider using the qq//
operator instead. You should also prefer the 3-argument open.

  my $pid = open(my $trexe, '-|', qq($tr $tr_params) or
  die Could not start TestRunner. $!;

  while($trexe) {
  print $_ if $verbose;

No need to reference the default variable with print. That is
implied with no arguments.

  print if $verbose;

 }
 waitpid($pid, 0);
 close($trexe);
 my $tr_ret = ($?8);
 
 return $tr_ret;
 }
 
 now I read the perldoc perlvar section about $? and realised that only the
 above 8 bits are used for the return value. The first 8 bit are used for
 the system codes. As far as I that understand fit just 256 values in those
 8 bits.
 Obviously, the error codes given back from my scripts are always wrong.
 For example, if the error code is 55028 I get 62464. With right shift the
 script prints 244.

I think on most platforms 0-255 is the range of possible exit
codes. What platform are you on?

Apparently Windows supports 32-bit integers... The Web suggests
that if you want to get full 32-bit integers on Windows then you
should use the Win32::Process module instead of open. It's not
portable, but at least it will do what you need.

Note: 55028  255 = 244. So what is happening is the original
exit code is being truncated down to one byte.

Regards,


-- 
Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org
Castopulence Software https://www.castopulence.org/
Blog http://www.bamccaig.com/
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'



signature.asc
Description: Digital signature


Re: Return values more than 256?

2013-03-07 Thread Brandon McCaig
On Thu, Mar 07, 2013 at 10:05:56AM -0500, Brandon McCaig wrote:
 Apparently Windows supports 32-bit integers... The Web suggests
 that if you want to get full 32-bit integers on Windows then you
 should use the Win32::Process module instead of open. It's not
 portable, but at least it will do what you need.

I suppose I should include a link[1][2] to the SO thread I
referenced since this advice is coming from there. Especially
because it offers an alternative option (using Win32::API to make
a system call with the PID). At a glance I'm not sure how to
access the standard streams with Win32::Process so perhaps using
the GetExitCodeProcess system call as mentioned in the SO thread
would be a convenient alternative to do everything you want. I
cannot say. :)

[1] 
http://stackoverflow.com/questions/6786812/is-it-possible-to-process-exit-codes-255-with-perl

[2] The thread is titled Is it possible to process exit codes 
255 with perl, just in case I typed the URL wrong. :)

Regards,


-- 
Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org
Castopulence Software https://www.castopulence.org/
Blog http://www.bamccaig.com/
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'



signature.asc
Description: Digital signature


Net::DNS

2013-03-07 Thread shawn wilson
What am I doing wrong here? I'm obviously not understanding what
object is being returned by $packet.

use strict;
use warnings;

use Data::Dumper;
use Net::DNS::Resolver;

my $dns = Net::DNS::Resolver-new;

print rev_ip('8.8.8.8') . \n;

sub rev_ip
{
  my ($ip) = @_;

  my $packet = $dns-search($ip);
  my @authority = $packet-authority;
  my $string = join ', ', map { $_-name } @authority;
  return [$ip] $string;
}


What I want is a reverse lookup ptr:
 % dig -x 8.8.8.8 | grep PTR
;8.8.8.8.in-addr.arpa.  IN  PTR
8.8.8.8.in-addr.arpa.   40076   IN  PTR google-public-dns-a.google.com.

So, per the script, the return value should be:
[8.8.8.8] google-public-dns-a.google.com

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Net::DNS

2013-03-07 Thread Lawrence Statton

On 03/07/2013 10:21 AM, shawn wilson wrote:

use Data::Dumper;
use Net::DNS::Resolver;

my $dns = Net::DNS::Resolver-new;

print rev_ip('8.8.8.8') . \n;

sub rev_ip
{
   my ($ip) = @_;

   my $packet = $dns-search($ip);
   my @authority = $packet-authority;
   my $string = join ', ', map { $_-name } @authority;
   return [$ip] $string;
}



Your results may vary, but for the vast majority of resolvers out there, 
you are not going to get any authority records - because most of them 
are not authoritative.


You probably want to be looking in the ANSWER.

Also - since you gave it the name - you already knew that part - it's 
what you gave the resolver.  What you want is the rdatastr method 
(perldoc Net::DNS::RR)



sub rev_ip
{
  my $string = join ', ', map { $_-rdatastr } $dns-search($_[0])-answer;
  return [$_[0]] $string;
}

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Net::DNS

2013-03-07 Thread shawn wilson
On Thu, Mar 7, 2013 at 11:40 AM, Lawrence Statton lawre...@cluon.com wrote:
 On 03/07/2013 10:21 AM, shawn wilson wrote:

 use Data::Dumper;
 use Net::DNS::Resolver;

 my $dns = Net::DNS::Resolver-new;

 print rev_ip('8.8.8.8') . \n;

 sub rev_ip
 {
my ($ip) = @_;

my $packet = $dns-search($ip);
my @authority = $packet-authority;
my $string = join ', ', map { $_-name } @authority;
return [$ip] $string;
 }


 Your results may vary, but for the vast majority of resolvers out there, you
 are not going to get any authority records - because most of them are not
 authoritative.

 You probably want to be looking in the ANSWER.

 Also - since you gave it the name - you already knew that part - it's what
 you gave the resolver.  What you want is the rdatastr method (perldoc
 Net::DNS::RR)


 sub rev_ip
 {
   my $string = join ', ', map { $_-rdatastr } $dns-search($_[0])-answer;
   return [$_[0]] $string;
 }

Yeah, I'm looking at the Net::DNS::RR doc... And I got it to work in
the test script doing this:
  my $packet = $dns-search($ip);
  my @authority = $packet-pre;
  my $string = join ', ', map { $_-rdata } @authority;

However, when I @EXPORT this function from a module, I get this:
Can't call method pre on an undefined value at lib/Misc.pm line 45,
 line 723793.

I've tried newing up Net::DNS::Resolver inside the sub with the same
result. I've also tried closuring $dns in the sub with the same
result.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Return values more than 256?

2013-03-07 Thread WFB
On 7 March 2013 16:05, Brandon McCaig bamcc...@gmail.com wrote:

 On Thu, Mar 07, 2013 at 10:21:40AM +0100, WFB wrote:
  Hi, List,

 Hello,

  To test our software I use perl to start it several times. My
  perl script gather some information and start then the program
  with different parameters.
 
  It works very well, but I have a problem with the return values
  of our program. This return codes are all in an area from 55000
  to 6. I use open to invoke our program and print the
  output. Finally I use the $? variable and print the error code
  in case of an error.
 
  sub start_test_runner {
  my ($tr = shift, $tr_params) = @_;

 I don't see a need for shift here. You can just assign the
 arguments array to your list of parameters.

   my ($tr, $tr_params) = @_;


Originally, it was:
my $tr = shift;
my $tr_params = shift;
For the email I formatted into the new form and obviously missed to remove
the shift :-)


   my $pid = open(my $trexe, $tr \$tr_params\ |) or die Could not
 start
  TestRunner. $!\n;

 Instead of escaping the double-quotes consider using the qq//
 operator instead. You should also prefer the 3-argument open.

   my $pid = open(my $trexe, '-|', qq($tr $tr_params) or
   die Could not start TestRunner. $!;

I often read the three letter open hint here on the list, but I did not
realize that that is also possible to use with pipes. Another thanks for
the qq hint.


   while($trexe) {
   print $_ if $verbose;

 No need to reference the default variable with print. That is
 implied with no arguments.

   print if $verbose;

  }
  waitpid($pid, 0);
  close($trexe);
  my $tr_ret = ($?8);
 
  return $tr_ret;
  }
 
  now I read the perldoc perlvar section about $? and realised that only
 the
  above 8 bits are used for the return value. The first 8 bit are used for
  the system codes. As far as I that understand fit just 256 values in
 those
  8 bits.
  Obviously, the error codes given back from my scripts are always wrong.
  For example, if the error code is 55028 I get 62464. With right shift the
  script prints 244.

 I think on most platforms 0-255 is the range of possible exit
 codes. What platform are you on?

 Apparently Windows supports 32-bit integers... The Web suggests
 that if you want to get full 32-bit integers on Windows then you
 should use the Win32::Process module instead of open. It's not
 portable, but at least it will do what you need.

 I will use the Win32::Process module. I am on Windows 7 x64 and this will
never run on any unix system.
Thanks, for this tip and the link in the other email.

Note: 55028  255 = 244. So what is happening is the original
 exit code is being truncated down to one byte.

 Regards,


Thanks a lot, Brandon!


 --
 Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org
 Castopulence Software https://www.castopulence.org/
 Blog http://www.bamccaig.com/
 perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
 q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
 tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'


 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1.4.10 (GNU/Linux)

 iQIcBAEBAgAGBQJROKzUAAoJEN2n1gIi5ZPynHcQAKtqHtZdxzcBoZpkyVKaNTI4
 BF0AtlUesQwEh197pMgDHWLfGIFfG9yD1pTLPHgI9zUAnMIUd4VpMCy0PLOZo5XY
 Lz+l51BSWS+v2O1tQyfhNhIMM+6uzXu1JoTsSyQbVUIjZBDn+MCUF067UUHfBEsh
 XO00Vf4CT/9kIa71+PgNY7ql0mKM7VScAIanGnXVw+QEhiapPGHkl1/33mAD1HU0
 L5iTUJtunyvZLTn36gw4qoheJ0nGy1O0IbERp0wFyXaquizajBeYpx4RfnEDAEN3
 ykxDt7YcRD3TcHw1FayZb2x57QRX/CXLB8S50FgjM+0a5aD3PjvN/ECzdLN69raF
 7rqmQC+uZ8qnJo/Kq4w1BytQV+Kzc23jzg89gTM0oLHgJ+TsGzz6ET8GM5uoFtjM
 BEWVUCjt8b0m50AE9z5UD38VmBJc/tyMVoJGTueVuN/W37izNASUG3jtgzx8AT0W
 3lewj+oBxKylhaQJjqDg0RerNuq0J4+ALl3Dp9k42anJr/AMTs+oc0z68j+ILXRW
 sdujaw+qhgoFzNpkOeEkpGqQmkR3X4s7BBDJb/NOuLnIoVzFzTPhLlb4xIe05+WB
 zzEPRiJCOesmSs/q+yClFiJj39lm7guZ0tS1/9MdZnlaeNi75Nh1DdQQjdTrkbyR
 QXgZ7Ezrnng0PRugQDxw
 =jE8+
 -END PGP SIGNATURE-




Re: Net::DNS

2013-03-07 Thread shawn wilson
On Thu, Mar 7, 2013 at 12:29 PM, Lawrence Statton lawre...@cluon.com wrote:
 On 03/07/2013 11:00 AM, shawn wilson wrote:

 However, when I @EXPORT this function from a module, I get this:
 Can't call method pre on an undefined value at lib/Misc.pm line 45,
   line 723793.


 if (my $answer = $packet-answer) { ... }


Ah, yeah that's right. thanks.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Net::DNS

2013-03-07 Thread shawn wilson
On Thu, Mar 7, 2013 at 2:04 PM, shawn wilson ag4ve...@gmail.com wrote:
 On Thu, Mar 7, 2013 at 12:43 PM, Lawrence Statton lawre...@cluon.com wrote:
 On 03/07/2013 11:29 AM, Lawrence Statton wrote:

 On 03/07/2013 11:00 AM, shawn wilson wrote:

 However, when I @EXPORT this function from a module, I get this:
 Can't call method pre on an undefined value at lib/Misc.pm line 45,
  line 723793.


 Well, without seeing the code or the parameters passed, I can only guess
 that your code is not coping with the case where there IS no answer for
 a given query.

 if (my $answer = $packet-answer) { ... }


 Oops ... your line 45 isn't MY line 45 ...

 if (my $packet = $dns-search(...)) {
   if (my $answer = $packet-answer()) {
... get some data out of the answer RRs 
   }
 }


 Yeah, I've pretty much got it - actually, I'm good on that part, just
 trying to figure why Net::Whois::ARIN isn't working like it is on cli:
 sub rev_ip
 {
   my ($ip) = @_;
   my $org = '';
   my $rev = '';

   $org = join , , map { $_-OrgName } grep { $_-can(OrgName) }
 $w-network($ip);

   my $packet = $dns-search($ip);
   $rev = join ', ', map { $_-rdatastr } grep { $_-type eq 'PTR' }
 $packet-answer if $packet;

   return [$ip] $org $rev;
 }

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Return values more than 256?

2013-03-07 Thread John W. Krahn

Brandon McCaig wrote:

On Thu, Mar 07, 2013 at 10:21:40AM +0100, WFB wrote:

Hi, List,


Hello,


To test our software I use perl to start it several times. My
perl script gather some information and start then the program
with different parameters.

It works very well, but I have a problem with the return values
of our program. This return codes are all in an area from 55000
to 6. I use open to invoke our program and print the
output. Finally I use the $? variable and print the error code
in case of an error.

sub start_test_runner {
my ($tr = shift, $tr_params) = @_;


I don't see a need for shift here. You can just assign the
arguments array to your list of parameters.

   my ($tr, $tr_params) = @_;


  my $pid = open(my $trexe, $tr \$tr_params\ |) or die Could not start
TestRunner. $!\n;


Instead of escaping the double-quotes consider using the qq//
operator instead. You should also prefer the 3-argument open.

   my $pid = open(my $trexe, '-|', qq($tr $tr_params) or
   die Could not start TestRunner. $!;


Or, instead of the three argument open, use the list option and you 
won't need quotes:


my $pid = open my $trexe, '-|', $tr, $tr_params
or die Could not start TestRunner. $!;

Also, be sure to close the pipe correctly:

close $trexe or warn $! ? Error closing $tr pipe: $!
: Exit status $? from $tr;




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Return values more than 256?

2013-03-07 Thread Brandon McCaig
On Thu, Mar 07, 2013 at 02:24:58PM -0800, John W. Krahn wrote:
 Or, instead of the three argument open, use the list option and you
 won't need quotes:
 
 my $pid = open my $trexe, '-|', $tr, $tr_params
 or die Could not start TestRunner. $!;

Good catch. :) I didn't think that open worked with a pipe and
LIST, but apparently it does now. It must have been an older Perl
that screamed at me, I guess. Thanks for pointing it out.

Regards,


-- 
Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org
Castopulence Software https://www.castopulence.org/
Blog http://www.bamccaig.com/
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'



signature.asc
Description: Digital signature


recognize input languages

2013-03-07 Thread Ken Peng
How can Perl recognize user's input languages? for example, if the 
message is in Chinese, the character encode will be GB2312. if it's in 
latin, the encode will be iso-8859-1, etc.


Thanks.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/