Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > magic vars and $!

Reply
Thread Tools

magic vars and $!

 
 
Peter Michael
Guest
Posts: n/a
 
      02-18-2004
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

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      02-18-2004
Peter Michael <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
 
Peter Michael
Guest
Posts: n/a
 
      02-18-2004
Hi Anno,


"Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
news:c0vd7r$jkg$(E-Mail Removed)-Berlin.DE...

[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



 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-19-2004
Peter Michael <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi Anno,
>
>
> "Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
> news:c0vd7r$jkg$(E-Mail Removed)-Berlin.DE...
>
> [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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use self.vars in class.method(parameters, self.vars) caccolangrifata Python 18 07-22-2011 10:22 PM
How do I declare global vars or class vars in Python ? Linuxguy123 Python 7 02-20-2009 06:45 PM
magic / counter-magic? (detect loading file?) Giles Bowkett Ruby 9 12-17-2007 05:42 AM
app vars and cache vars Jon ASP .Net 3 12-14-2004 08:52 PM



Advertisments