简体   繁体   中英

I have created a module in perl but i do not understand how to use it

So here is the module i have created:

#!/usr/bin/perl


use warnings;
use strict;
package Module1;

my @user;
my $count = 0;
my @test;
my @last =qx(last);
my @logins;
my %logins_by_user;
my @UserDiskUsage;
#my $pass =qx(sudo passwd -S @user);

#Opens the file /etc/passwd and puts the users with an uid over 1000 but less that 65000 into an array.
sub userloggins {
open( my $passwd, "<", "/etc/passwd") or die "/etc/passwd failed to open.\n";

while (my $lines = <$passwd>) {
    my @splitarray = split(/\:/, $lines );
    if( $splitarray[2] >= 1000 && $splitarray[2] < 65000) {

        $user[$count] =$splitarray[0];
        #print "$user[$count]\n";
        $count++;
    }
}
close $passwd;

#Counts how many times each user has logged in and stores them in a hash, then sorts them

for my $user (@user) {
$logins_by_user{$user} = grep /^\Q$user\E /, `last '$user'`;
}

for my $user (
sort { $logins_by_user{$b} <=> $logins_by_user{$a}  ||  $a cmp $b }     @user 
) {
print("$user: $logins_by_user{$user}\n");
}
}
userloggins(); 
#del 2, monitor user password age
print "-------------------------------------------------------\n";

PasswordAge();

sub PasswordAge {
my $days = 10;
my $currentdate = (qx(date +%s)) / 86400;
for my $i (0...$#user) {
    my @shadowDays = qx(sudo grep $user[$i] /etc/shadow | cut -d: -f3);
    if ($shadowDays[0] < ($currentdate - $days)){

        print ("User $user[$i] has not changed their password in $days days.\n");
    }
    #print "$currentdate\n";



    #print "$user Has not changed their password since $splitpass\n"; 
}
}

The subroutine i want to send data to is called PasswordAge. The variable i want to send data in to through user input,from another script, is called $days. How can i do this ? I've tried looking online searching for answers but i don't quite get it how i should do it in my case. Hope you understand.

The answer to your question as asked is:

  • Save your module as 'Module1.pm'.
  • use Module1;
  • In your script: Module1::PasswordAge();

However at risk of sounding a bit patronising - it's probably worth reading up on eg perlmod as the process of creating modules - whilst not massively difficult, is somewhat unusual to be doing before understanding how to import and load a module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM