magic vars and $!

Discussion in 'Perl Misc' started by Peter Michael, Feb 18, 2004.

  1. 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
     
    Peter Michael, Feb 18, 2004
    #1
    1. Advertising

  2. Peter Michael

    Anno Siegel Guest

    Peter Michael <> 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
     
    Anno Siegel, Feb 18, 2004
    #2
    1. Advertising

  3. Hi Anno,


    "Anno Siegel" <-berlin.de> wrote in message
    news:c0vd7r$jkg$-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
     
    Peter Michael, Feb 18, 2004
    #3
  4. Peter Michael

    Anno Siegel Guest

    Peter Michael <> wrote in comp.lang.perl.misc:
    > Hi Anno,
    >
    >
    > "Anno Siegel" <-berlin.de> wrote in message
    > news:c0vd7r$jkg$-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
     
    Anno Siegel, Feb 19, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    419
  2. Keith

    Analog and Report Magic

    Keith, Jun 4, 2004, in forum: HTML
    Replies:
    0
    Views:
    379
    Keith
    Jun 4, 2004
  3. Linuxguy123
    Replies:
    7
    Views:
    720
    Paddy O'Loughlin
    Feb 20, 2009
  4. caccolangrifata
    Replies:
    18
    Views:
    420
    Chris Torek
    Jul 22, 2011
  5. Giles Bowkett
    Replies:
    9
    Views:
    425
    Giles Bowkett
    Dec 17, 2007
Loading...

Share This Page