magic vars and $!

P

Peter Michael

Hi,

I am looking for the correct way to deal with error checking when
performing system calls via magic variables, e.g.

use strict;
use English qw(-no_match_vars);

# trying to change UID
$UID = getpwnam "nobody";

The last line will not work for a non-privileged user. One idea to
detect this is

$! = 0;
$UID = getpwnam "nobody";
warn "cannot change uid: $!\n" unless 0 == $!;

Is there any "official way" to deal with error checking for magic
vars?

Thanks for any hints.

Best regards,

Peter
 
A

Anno Siegel

Peter Michael said:
Hi,

I am looking for the correct way to deal with error checking when
performing system calls via magic variables, e.g.

use strict;
use English qw(-no_match_vars);

Few people use that module.
# trying to change UID
$UID = getpwnam "nobody";

The last line will not work for a non-privileged user. One idea to
detect this is

$! = 0;
$UID = getpwnam "nobody";
warn "cannot change uid: $!\n" unless 0 == $!;

Is there any "official way" to deal with error checking for magic
vars?

I'd check if the assignment was successful.

do {
use integer;
$> = $_;
$> == $_ or die "Can't change uid ($>) to $_: $!";
} for scalar getpwnam 'nobody';

The integer pragma is useful because getpwnam may return negative userid's
as large positives, so the comparison is safer that way.

Anno
 
P

Peter Michael

Hi Anno,



[snip]
I'd check if the assignment was successful.

do {
use integer;
$> = $_;
$> == $_ or die "Can't change uid ($>) to $_: $!";
} for scalar getpwnam 'nobody';

thank you very much for the advice.
The integer pragma is useful because getpwnam may return negative userid's
as large positives, so the comparison is safer that way.

Negative uids? Do you mean "getpwnam may return large positive uids
as negative"? Can you please tell me what the exact reason for this
(C data type mismatch?) is?

Regards,

Peter
 
A

Anno Siegel

Peter Michael said:
Hi Anno,



[snip]
I'd check if the assignment was successful.

do {
use integer;
$> = $_;
$> == $_ or die "Can't change uid ($>) to $_: $!";
} for scalar getpwnam 'nobody';

thank you very much for the advice.
The integer pragma is useful because getpwnam may return negative userid's
as large positives, so the comparison is safer that way.

Negative uids? Do you mean "getpwnam may return large positive uids
as negative"? Can you please tell me what the exact reason for this
(C data type mismatch?) is?

Well, signed vs. unsigned binary. I've seen userid's specified as negative
integers.

Why the mismatch? I'm not sure, and I can't say I care very much. It
happens all the time with user- (and group-) id's. Nobody seems to be
sure whether they're signed or not, though I'm sure POSIX has put its
foot down about that. Treat them as native integers and you're fine.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top