CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl Scripting Help

 



dustin.brown1
New User

Dec 14, 2015, 12:50 PM

Post #1 of 2 (963 views)
Perl Scripting Help Can't Post

Hey all, taking a class on UNIX Programming and going through a Perl phase at the moment. I have made the following script, but I am unable to get the add group and delete group sections to work. Can anyone provide some guidance for me? It is greatly appreciated.


Code
 
#!/usr/bin/perl

use warnings;
use strict;

my $user_grp = read_doc("/etc/passwd");

my %admin_menu = (
1 => sub {
my $username = get_input("Enter username: ");
if ( exists $user_grp->{$username} ) {
print "\nUser ", $username, " exist!";
return;
}

system( "useradd", "-u $$", "-g", "users", "$username" );
},
2 => sub {
my $username = get_input("Enter username: ");
if ( exists $user_grp->{$username} ) {

system( "userdel", "$username" )
if get_input("Do you really want to remove the $username
?: ") =~
/\by\b/i;
}
},

3 => sub {
my $group_name = get_input("Enter Group Name: ");
if ( exists $group_name->{$group_name} ) {
print "\nUser ", $group_name, " exist!";
return;
}

system( "groupadd -g $group_id $group_name" );
},
4 => sub {
my $group_name = get_input("Enter Group Name: ");
if ( exists $group_name->{$group_name} ) {

system( "groupdel", "$group_name" )
if get_input("Do you really want to remove the $group_na
+me
?: ") =~
/\by\b/i;
}
},

5 => sub { get_input("Exiting the Program\n"); exit(); },
);

do {
admin_menu();
$choice = get_input("Enter your choice: ");
if ( $choice < 1 or $choice > 5 ) {
admin_menu();
}
else { $admin_menu{$choice}->(); }

} while (1);

sub get_input {
my $prompt = shift;
print $prompt;

chomp( my $value = <> );
return $value;
}

sub read_doc {
my $file = shift;
my %user;
open my $fh, '<', $file or die $!;
while (<$fh>) {
my ( $name, $grp ) = ( split /:/ )[ 0, 4 ];
$user{$name} = $grp;
}

return \%user;
}

sub admin_menu {
print << "UNIX_ADMIN";

Admin Menu

To Add a User, press 1:
To Delete a User, press 2:
To Add a group, press 3:
To Delete a group, press 4:
To Exit this Menu, press 5:

UNIX_ADMIN

}


The issue I get for adding a group is: Can't use string ("Test2") as a HASH ref while "strict refs" in use at ./example3 line 35, <> line 2.

The error I get for deleting a group that I made previously in an earlier class, I get: Can't use string ("Test") as a HASH ref while "strict refs" in use at ./example3 line 44, <> line 2.


FishMonger
Veteran / Moderator

Dec 14, 2015, 2:48 PM

Post #2 of 2 (955 views)
Re: [dustin.brown1] Perl Scripting Help [In reply to] Can't Post

The script you posted won't compile.

Quote
c:\test>dustin.pl
Global symbol "$group_id" requires explicit package name (did you forget to declare "my $group_id"?) at C:\test\dustin.pl line 40.
Global symbol "$group_na" requires explicit package name (did you forget to declare "my $group_na"?) at C:\test\dustin.pl line 47.
Global symbol "$choice" requires explicit package name (did you forget to declare "my $choice"?) at C:\test\dustin.pl line 59.
Global symbol "$choice" requires explicit package name (did you forget to declare "my $choice"?) at C:\test\dustin.pl line 60.
Global symbol "$choice" requires explicit package name (did you forget to declare "my $choice"?) at C:\test\dustin.pl line 60.
Global symbol "$choice" requires explicit package name (did you forget to declare "my $choice"?) at C:\test\dustin.pl line 63.
Execution of C:\test\dustin.pl aborted due to compilation errors.


Did you run a different version of the script when you received your error?


(This post was edited by FishMonger on Dec 14, 2015, 2:49 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives