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

My apologies for using incorrect syntax in my question. I thought I was responding to a previous thread, not creating a new one (thus, the lack sufficient information, I assumed it was already there).

Anyway, I rewritten the code, to make it a bit smaller. My situation is this: My school used to have their student/faculty login system setup, so each user loged in with their full name "First Last". This 'full name' was actually a psydonym for the real UNIX username, which was u character, followed by 3-5 addititonal digits (ie u881).

We have since upgraded to a new server, and while doing so, decided to change the real unix usernames, from numbers, to a short version of the user name

Gabriel Rigg, u881 becomes Gabriel Rigg, grigg

I have two files in my posession:
1) a file containing old usernumbers, and full names:
-------------------------usrbynum.txt---------------------
u880 name: Gabe Girard u881 name: Gabriel Rigg u882 name: Genna Bourget ... for about 263 of them
----------------------------EOF----------------------------
and 2) a file containing new shortnames, and full names
-------------------------srnames.txt-----------------------
fkasierderrick::1333:20::::Freya Kasier Derrick grigg::1460:20::::Gabriel Rigg omeara::1025:20::::Gary O'Meara
---------------------------EOF-----------------------------

There are a few issues to deal with. First, not everyone in short/full is in num/full, but I can deal with that later. However, here is my code:

#!/usr/bin/perl $oldserv = "./testing"; $newserv = "./testing2"; $usernumfile = "./usrbynum.txt"; $usershortfile = "./srnames.txt"; open USERNUMS, "< $usernumfile" or die "$!"; open SHORTNAMES, "< $usershortfile" or die "$!";# open file with user while (<SHORTNAMES>) { # loop through all lines of shor +tnames my ($longname,$shortname) = (split /:/)[7,0]; # seperat +e by those ugly colen $finishedusers{$longname} = $shortname; chomp %finishedusers; } my %lookup; # Hash %lookup while(<USERNUMS>) { # loop through file with user numbers ($uid) = /(u\d{3,5})/; # User ID find yes ($longname2) = /([A-Z]\w+)/; chomp $longname2; $lookup{$longname2} = $uid; # Setup hash %lookup with keys " +Long Name"=> "usernum" } while (($longname2, $uid) = each(%lookup)) { next unless exists $finishedusers{$longname2}; system ("cp '$oldserv/$uid/Documents/*' '$newserv/$finishedusers{$ +longname2}/Documents/'"); print "Copying Files for $finishedusers{longname2}"; } close USERNUMS; close SHORTNAMES;

To test whether or not the hashes of the different files were working, I commented most of the code out, and told it to to print to the screen. For example:
while (<SHORTNAMES>) { # loop through all lines of shor +tnames my ($longname,$shortname) = (split /:/)[7,0]; # seperat +e by those ugly colen $finishedusers{$longname} = $shortname; chomp %finishedusers; } while (($templong,$tempshort) = each(%finishedusers)) { print "long: $templong \n short: $tempshort \n\n }
For both of the files and their while() statements used to extract data for the hashes, they printed the exact correct statements (even the newlines added up, so there were none because of the chomp). So, I know that, individually at least, these statements work.

The final few lines of the code is supposed to match shortnames to old usernumbers, via a matching long name (since its the shared constant between the files), and move all the files from the old usernumber/Directory to the short/Directory.

I added 'use strict; use warnings;', and got the following output:
Global symbol "$oldserv" requires explicit package name at doctrans2.p +l line 6. Global symbol "$newserv" requires explicit package name at doctrans2.p +l line 7. Global symbol "$usernumfile" requires explicit package name at doctran +s2.pl line 8. Global symbol "$usershortfile" requires explicit package name at doctr +ans2.pl line 9. Global symbol "$usernumfile" requires explicit package name at doctran +s2.pl line 11. Global symbol "$usershortfile" requires explicit package name at doctr +ans2.pl line 12. Global symbol "%finishedusers" requires explicit package name at doctr +ans2.pl line 15. Global symbol "%finishedusers" requires explicit package name at doctr +ans2.pl line 16. Global symbol "$uid" requires explicit package name at doctrans2.pl li +ne 21. Global symbol "$longname2" requires explicit package name at doctrans2 +.pl line 22. Global symbol "$longname2" requires explicit package name at doctrans2 +.pl line 23. Global symbol "$longname2" requires explicit package name at doctrans2 +.pl line 24. Global symbol "$uid" requires explicit package name at doctrans2.pl li +ne 24. Global symbol "$longname2" requires explicit package name at doctrans2 +.pl line 27. Global symbol "$uid" requires explicit package name at doctrans2.pl li +ne 27. Global symbol "%finishedusers" requires explicit package name at doctr +ans2.pl line 28. Global symbol "$longname2" requires explicit package name at doctrans2 +.pl line 28. Global symbol "$oldserv" requires explicit package name at doctrans2.p +l line 29. Global symbol "$uid" requires explicit package name at doctrans2.pl li +ne 29. Global symbol "$newserv" requires explicit package name at doctrans2.p +l line 29. Global symbol "%finishedusers" requires explicit package name at doctr +ans2.pl line 29. Global symbol "$longname2" requires explicit package name at doctrans2 +.pl line 29. Global symbol "%finishedusers" requires explicit package name at doctr +ans2.pl line 30. Execution of doctrans2.pl aborted due to compilation errors.

However, without use strict and use warnings, the code execute with no errors, but no results.

Expected result was that a file ./testing/u881/Documents/test.txt would be moved to ./testing2/grigg/Documents/ . However, it does not get moved (obviously).

Any suggestions?

Replies are listed 'Best First'.
Re: Still having problems with user 'backup' script
by reneeb (Chaplain) on Dec 02, 2004 at 11:25 UTC
    If you use use strict;, you have to declare all variables with 'my', e.g.
    #! /usr/bin/perl use strict; my $var; $var = 'Hello World'; print $var,"\n";


    To copy files I would recommend to use File::Copy or File::NCopy
      If you use use strict;, you have to declare all variables with 'my',

      That's a bit of an over-simplification. See the discussion of use strict 'vars' in perldoc strict.

      This generates a compile-time error if you access a variable that wasn't declared via our or use vars, localized via my(), or wasn't fully qualified.

      Using my is only one of a number of ways to satisfy use strict 'vars' - albeit the most common.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: Still having problems with user 'backup' script
by Anonymous Monk on Dec 02, 2004 at 14:47 UTC
    Why did you add use strict and use warnings?
Re: Still having problems with user 'backup' script
by trammell (Priest) on Dec 03, 2004 at 12:40 UTC
    Regarding your "no results" problem, I'd guess that "cp" is choking because you're asking it to create a new directory as well as a new file. Perhaps you should check the status of your system() call, or use File::Copy.