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

I basically can get all of this to work as seperate scripts, however i thought i would try to get the second script in and call on it as a subroutine. I am trying to get the output of the subroutine and assign it a value that i can use in the script. What am i doing wrong? I get this error when i try to run this: Can't modify constant item in scalar assignment at C:\scripts\perl\ChPass.pl line 13, near "$Password;" BEGIN not safe after errors--compilation aborted at C:\scripts\perl\ChPass.pl line 19. Here's my code. Any help would be appreciated. Ray
# Ray Espinoza # ChPass.pl # This script calls in the encrypted password file and decrypts it and + uses # its value as $Password to change the account password to $Password. ###################################################################### +########################### #!D:\perl\bin -w use strict; use Win32::AdminMisc; #use Win32::AdminMisc qw(SetPassword); chomp(my $Server = <STDIN>); my $User = "Administrator"; &getPassword = my $Password; my $Result = Win32::AdminMisc::SetPassword( $Server, $User, $Password) + || die "Couldn't change password on $Server: $^E"; print $Result; sub getPassword { use strict; use Crypt::Blowfish_PP; my $key="abcdefghijklmnopqrstuvwxyz123456789"; my $bf = Crypt::Blowfish_PP->new($key); open (IN,"phrase.txt") || die "Can't read phrase.txt: $!"; while (<IN>) { my $encrypted_password = $_; my $decrypted_password = $bf->decrypt($encrypted_password); print $decrypted_data; }
  • Comment on can i get the value outputed from my subroutine into my program as a value
  • Download Code

Replies are listed 'Best First'.
Re: can i get the value outputed from my subroutine into my program as a value
by physi (Friar) on Aug 15, 2001 at 21:07 UTC
    First: that what subs are for (to return a value to the main process)

    Serveral things went wrong here:

    &getPassword = my $Password;
    must be :
    my $Password = &getPassword;
    and your password subroutine has to return a value, which then is assigned to $Password in your main program part
    my $password = &getPassword; print "$password\n"; sub getPassword { my $pass = 'willi'; return $pass; }
    This will give you an willi as output.
    Hope this will help.
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
Re: can i get the value outputed from my subroutine into my program as a value
by andye (Curate) on Aug 15, 2001 at 21:18 UTC
    Ray, a couple of points, they're below... I've assumed that phrase.txt only contains one line, tell me if I've got the wrong end ofthe stick...
    #!D:\perl\bin -w use strict; use Win32::AdminMisc; #use Win32::AdminMisc qw(SetPassword); chomp(my $Server = <STDIN>); my $User = "Administrator"; my $Password = &getPassword; # ***********I've changed this line round - it doesn't # make sense the other way my $Result = Win32::AdminMisc::SetPassword( $Server, $User, $Password) + || die "Couldn't change password on $Server: $^E"; print $Result; sub getPassword { # use strict; #*************you don't need this line - you only have to 'use strict' + once per script use Crypt::Blowfish_PP; my $key="abcdefghijklmnopqrstuvwxyz123456789"; my $bf = Crypt::Blowfish_PP->new($key); open (IN,"phrase.txt") || die "Can't read phrase.txt: $!"; #*******this doesn't need to be a while loop if you're only expecting # one line my $encrypted_password = <IN>; #you probably want to close your file, as well close IN; my $decrypted_password = $bf->decrypt($encrypted_password); return $decrypted_password; # ************** you probably want to return this value, # i.e. make it the result of this subroutine, unless # I've completely misunderstood. Also, your there was a # typo in your variable name } #******** you no longer need a closing-curly to close the while loop, +but you do need one to close getPassword
    Hope I've picked up the showstoppers, give that a go. It looks a bit hurried because I need to go and cook dinner!

    andy.

      Thank you andye and everyone for your help. I really appreciate it. It does work. however i am getting an extra "1" at the end of the password. for example...$password gets assigned, after the decryption, "77ujump5" it becomes "77ujump51". ANy ideas why? thnx Ray I figured it out. I was having the script print the password on the console and also return a result(1 - success) :). Thanks to everyone for their help. Much Appreciated.
Re: can i get the value outputed from my subroutine into my program as a value
by wardk (Deacon) on Aug 15, 2001 at 21:11 UTC

    I am thinking you want to assign the results of getPassword to $Password... that would be something like:

    my $Password = getPassword();

    in order to do that you will want to return that password from getPassword...I notice that some code appears missing (you didn't close the subroutine block for getPassword), so maybe you are returning something from getPassword that isn't listed...

    something similar to:

    my $Password = getPassword(); sub getPassword { # do some stuff to create a password return $password; }