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 Script

 



caton03
New User

Nov 21, 2013, 10:40 AM

Post #1 of 4 (1177 views)
Perl Script Can't Post

I am a newbie and am having problems with this script. Can anyone tell me where I am going wrong?


# Command line: ./newuid.pl small_passwd
# Command line: ./newuid.pl [filename]
#Verify that one command line argument was provided. If not, output a message to stdout.
if ( @ARGV < 1 ) {
print "File name must be specified.\n";
exit(1);
}
# Sort the uids in ascending order and extract the last element from the array.
@UID = $ARGV[1]
#Extract the highest numbered uid listed in the small passwd file.
cut -d:-f3/home/caton03/itec400/homework/ $ARGV[1];
#Extract the last element from the array.
$UID = (pop($UID))+1;
print "Next available User ID (UID) is $UID\n"
exit(1);


print "The new User ID number is ";
print $UID;
print "\n";
# end if

Code



      
    


FishMonger
Veteran / Moderator

Nov 21, 2013, 12:27 PM

Post #2 of 4 (1173 views)
Re: [caton03] Perl Script [In reply to] Can't Post

The only portion that you're doing "correctly" is the test for the number of args passed to the script.

Perl arrays are zero based, so the index of the first element is 0, not 1.


Quote
# Sort the uids in ascending order and extract the last element from the array.
@UID = $ARGV[1]

That does not do any sorting as your comment states. It assigned the 2nd element of @ARGV to the 1st element of @UID.


Quote
#Extract the highest numbered uid listed in the small passwd file.
cut -d:-f3/home/caton03/itec400/homework/ $ARGV[1];

That is not the correct way to run an external command and if it were, it would not save the output of that command. Also, shelling out to the cut command is a funky non perl way of getting the uid.


Quote
#Extract the last element from the array.
$UID = (pop($UID))+1;

There is no guarantee that the highest numbered uid will be on the last line of the file.


(This post was edited by FishMonger on Nov 21, 2013, 12:27 PM)


Laurent_R
Veteran / Moderator

Nov 21, 2013, 3:45 PM

Post #3 of 4 (1168 views)
Re: [FishMonger] Perl Script [In reply to] Can't Post


In Reply To
The only portion that you're doing "correctly" is the test for the number of args passed to the script.


Even then, yes, it more or less works, but it is debatable whether this is really the right way of doing it.

The basic idea is that there needs to be one and only one argument (not just at least one).

So something like this would be more precise about the requirement and also more "Perlish":


Code
@ARGV == 1 or die "File name must be specified.\n";



FishMonger
Veteran / Moderator

Nov 21, 2013, 5:12 PM

Post #4 of 4 (1166 views)
Re: [Laurent_R] Perl Script [In reply to] Can't Post

You are correct and that's why I put correctly in quotes. I probably should have expanded on that as you did.

 
 


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

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