wsanders has asked for the wisdom of the Perl Monks concerning the following question:

cuserid() is supposed to return the name of the user associated with the EUID of the process. I have a shell: I logged in as "wsanders", then "su - adudaren". Both my EUID and UID are the number associated with 'adudaren'. However, cuserid() gives me different results:
1) perl -e 'use POSIX; print cuserid' wsanders 2) perl -e 'print POSIX::cuserid' adudaren
I would expect cuserid to return "adudaren" no matter how I call it, it's getlogin() that returns the name of the user associated with the controlling terminal (the original logger-inner.) What's the difference between 1 and 2? Thanks, -w (there, that's better)
  • Comment on Diff between 'print POSIX::cuserid' and 'use POSIX; print cuserid'
  • Download Code

Replies are listed 'Best First'.
Re: Diff between 'print POSIX::cuserid' and 'use POSIX; print cuserid'
by linuxer (Curate) on Jan 08, 2013 at 00:10 UTC

    In your first example, you did a use POSIX; in the second, you did not. Why?

    Without loading POSIX you won't be able to call a function like POSIX::cuserid.

    Enable strictwarnings and see yourself what happens:
    $ perl -wE 'say POSIX::cuserid' Name "POSIX::cuserid" used only once: possible typo at -e line 1. say() on unopened filehandle cuserid at -e line 1.
    But with loading POSIX:
    $ perl -MPOSIX -wE 'say POSIX::cuserid' linuxer

    Updates

    • replaced strict with warnings; removed strict from code examples; the warning is the crucial part.
      Thanks, yes, so in #2 POSIX isn't loaded, so perl -e 'print POSIX::cuserid' should return nothing, actually. That's the answer. There is something odd about this user's environment that is causing it to print his username. In *my* environment, and root's, it behaves the way it is supposed to. Not going to investigate further.
Re: Diff between 'print POSIX::cuserid' and 'use POSIX; print cuserid'
by choroba (Cardinal) on Jan 08, 2013 at 01:00 UTC
    Please, fix the problems indicated by others. Note that cuserid and $> both reflect changes made from within the script:
    $ sudo perl -MPOSIX - $UID print cuserid, " $< $>\n"; $> = shift; print cuserid, " $< $>\n"; __END__
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Diff between 'print POSIX::cuserid' and 'use POSIX; print cuserid'
by LanX (Saint) on Jan 07, 2013 at 23:49 UTC
    I think I can spot a difference ... but before you reformat your post with code-tags I'm not sure enough to tell. ;-)

    UPDATE:

    yeah better! =)

    > perl -we 'print POSIX::cuserid' Name "POSIX::cuserid" used only once: possible typo at -e line 1. print() on unopened filehandle cuserid at -e line 1. > perl -we 'use POSIX;print POSIX::cuserid' lanx

    So what happens if you also use  POSIX?

    Cheers Rolf