1 Replies - 9501 Views - Last Post: 10 December 2012 - 06:24 AM

#1 Hollywood0728  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-September 09

Trying to accomplish this script

Posted 09 December 2012 - 11:54 AM

I am having issues completing my script because I have no clue what I am doing, just learning. Created an application that prompts a menu to create users and groups. When the user enters a number it brings them to that task. I keep getting an error when I actually try to create a user or group.

Maintenance Tasks Menu

1 - Create UNIX Group
2 - Drop UNIX Group
3 - Create User
4 - Drop User
5 - Exit


Please enter the task number you wish execute
3
Enter user name to create
steve
Can't locate object method "useradd" via package "steve" (perhaps you forgot to load "steve"?) at Phase3IP.pl line 32, <> line 2.


What does this mean and how do i fix it?

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to accomplish this script

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Trying to accomplish this script

Posted 10 December 2012 - 06:24 AM

You have run afoul of "indirect object notation", one of the apparently cool features included in Perl way back when which is now widely recognized as having been a Very Bad Idea because it can cause things to be interpreted rather strangely - in exactly the way that you've encountered. Basically, when the Perl parser sees "useradd $user_input", it first checks to see whether "useradd" is a function it knows about, which it doesn't, so then it assumes that you must have meant to call the "useradd" method on the $user_input object. $user_input is "steve" and there's no class named "steve", so the method "steve->useradd" doesn't exist, which produces the error message you reported.

In this case, it looks like you're trying to use shell commands rather than Perl commands, so you need to enclose those commands in backticks so that Perl knows to pass them to the shell instead of trying to parse them itself: `useradd $user_input`; That should get it working for you (assuming that you're running as root so that you have permission to use these commands, of course).

However, speaking of things in Perl that seemed like a good idea at the time, the Switch module is another of them. You are strongly advised to avoid it like the plague that it is. Be aware that Switch has been considered deprecated for some time now and, as of Perl 5.14, Switch has been removed from the core Perl distribution entirely. If you are able to use Perl 5.10 or later, use given/when instead:

use 5.010;
given ($user_input) {
  when 1 { ... }
  when 2 { ... }
  default { ... }
}


If you have to use an older Perl and don't like if/elsif cascades, you can still work something up using a hash of coderefs, although it's a somewhat more advanced construct:

my %options (
  1 => sub { ... },
  2 => sub { ... },
);
if (exists $options{$user_input}) {
  $options{$user_input}->();
} else {
  print $some_error_message;
}



More general tips:

- Don't comment out "use strict;"! If strict complains about something, it's very likely to be something you don't really want to be doing anyhow. It's there to help you, if only you will let it.

- When running the code you posted, I also get a bunch of warnings at startup saying "Unquoted string "break" may clash with future reserved word" and "Useless use of a constant (break) in void context. Apparently, even with the Switch module, "break" is not used in Perl.

- You've got a lot of duplicated code in your cases. Probably good to factor that out.

If you'd like me to do so and this isn't a homework assignment, I'd be happy to do a quick revision/rewrite of the code you posted to show how I would do it in good, modern Perl.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1