Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > split .ini file for passwords against usernames

Reply
Thread Tools

split .ini file for passwords against usernames

 
 
ipog_1234@yahoo.com
Guest
Posts: n/a
 
      10-12-2005
Hi:
Purpose: to read .ini file to get a password for a respective user.

Problem:I was running this perl script and could not figure out how to
search for the username(here joe) and print the password.

..ini File: contains username and password with tab sequence in-between.

Code:
#!/software/bin/perl -w
use warnings;
$file='/home/work/password.ini';
$username="joe";
open(INFO, $file);
while(<INFO>){
if ( $_ ~= /^$username/ ){
($password)=split[1];

}
}
print "$password,\n";
close(INFO);

 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      10-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi:
> Purpose: to read .ini file to get a password for a respective user.
>
> Problem:I was running this perl script and could not figure out how to
> search for the username(here joe) and print the password.
>
> .ini File: contains username and password with tab sequence in-between.
>
> Code:
> #!/software/bin/perl -w
> use warnings;
> $file='/home/work/password.ini';
> $username="joe";
> open(INFO, $file);
> while(<INFO>){
> if ( $_ ~= /^$username/ ){
> ($password)=split[1];
>
> }
> }
> print "$password,\n";
> close(INFO);


1. Your program doesn't even compile!
2. How does your program fail? What is the output you see?

Try changing "($password)=split[1];" into "(undef,$password) = split;"
or "$password = (split)[1];"
I'm not entirely sure (make that: "I don't have a clue") what split[1]
does. It seems to be valid, but it doesn't seem to split on white space.
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-12-2005
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi:
> Purpose: to read .ini file to get a password for a respective user.
>
> Problem:I was running this perl script and could not figure out how to
> search for the username(here joe) and print the password.
>
> .ini File: contains username and password with tab sequence in-between.
>
> Code:
> #!/software/bin/perl -w
> use warnings;


"use strict" is missing.

> $file='/home/work/password.ini';
> $username="joe";
> open(INFO, $file);
> while(<INFO>){
> if ( $_ ~= /^$username/ ){
> ($password)=split[1];


A list slice needs an extra pair of parentheses. The parentheses around
$password are not needed:

my $password = (split)[1];

>
> }
> }
> print "$password,\n";
> close(INFO);
>


Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-12-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:

> open(INFO, $file);



You should always, yes *always*, check the return value from open():

open(INFO, $file) or die "could not open '$file' $!";


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-12-2005
Josef Moellers wrote:
> I'm not entirely sure (make that: "I don't have a clue") what split[1]
> does.


$ perl -MO=Deparse -e'($password) = split[1]'
$password = split(/[1]/, $_, 2);
-e syntax OK

> It seems to be valid, but it doesn't seem to split on white space.


It apparently assumes '[1]' is the pattern on which to split. Now,
where the limit of 2 came from, I have no idea.

Paul Lalli

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-12-2005
Paul Lalli wrote:
>
> $ perl -MO=Deparse -e'($password) = split[1]'
> $password = split(/[1]/, $_, 2);
> -e syntax OK
>
> It apparently assumes '[1]' is the pattern on which to split. Now,
> where the limit of 2 came from, I have no idea.


Oh. Yes I do. Because I provided a list context to split, and that
list contained only one item, Perl was smart enough to know that there
was no need to split more than once, as everything after the first
token would be thrown away. So it implicitly gave a limit of 2, to
save needless work.

Perl is often smarter than even I give it credit for...

Paul Lalli

 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      10-12-2005
Paul Lalli wrote:
> Paul Lalli wrote:
>
>>$ perl -MO=Deparse -e'($password) = split[1]'
>>$password = split(/[1]/, $_, 2);
>>-e syntax OK
>>
>>It apparently assumes '[1]' is the pattern on which to split. Now,
>>where the limit of 2 came from, I have no idea.

>
>
> Oh. Yes I do. Because I provided a list context to split, and that
> list contained only one item, Perl was smart enough to know that there
> was no need to split more than once, as everything after the first
> token would be thrown away. So it implicitly gave a limit of 2, to
> save needless work.


Thanks. I hope I remember the "-MO=Deparse" switch in the future.

> Perl is often smarter than even I give it credit for...


Doesn't this apply to people as well (sometimes) ?

I, at least, am now a little bit smarter than before.

Cheers,

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
ipog_1234@yahoo.com
Guest
Posts: n/a
 
      10-12-2005
Hi all:
I am stuck at 2 compilation errors, before trying out the bits of code
suggested in this thread.

syntax error at testread5.pm line 8, near "$_ ~"
syntax error at testread5.pm line 15, near "}"

Do you the Regex to match $username needes some tweaking.

thankyou.
ipog.

Josef Moellers wrote:
> Paul Lalli wrote:
> > Paul Lalli wrote:
> >
> >>$ perl -MO=Deparse -e'($password) = split[1]'
> >>$password = split(/[1]/, $_, 2);
> >>-e syntax OK
> >>
> >>It apparently assumes '[1]' is the pattern on which to split. Now,
> >>where the limit of 2 came from, I have no idea.

> >
> >
> > Oh. Yes I do. Because I provided a list context to split, and that
> > list contained only one item, Perl was smart enough to know that there
> > was no need to split more than once, as everything after the first
> > token would be thrown away. So it implicitly gave a limit of 2, to
> > save needless work.

>
> Thanks. I hope I remember the "-MO=Deparse" switch in the future.
>
> > Perl is often smarter than even I give it credit for...

>
> Doesn't this apply to people as well (sometimes) ?
>
> I, at least, am now a little bit smarter than before.
>
> Cheers,
>
> Josef
> --
> Josef Möllers (Pinguinpfleger bei FSC)
> If failure had no penalty success would not be a prize
> -- T. Pratchett


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-12-2005
In future replies, please reply to the actual message you're replying
to, not to some arbitrary post in the same thread.

In future replies, please place your new text *below* the text to which
you are replying.

In future replies, please trim the text to which you are replying to a
relevant amount of quoting material.

(Have you read the posting guidelines for this group yet?)

(E-Mail Removed) wrote:
> Hi all:
> I am stuck at 2 compilation errors, before trying out the bits of code
> suggested in this thread.


Why would it be 'before'? You know the code you're trying to compile
is wrong, but you're just going to ignore it until the interpreter
stops giving you errors?

>
> syntax error at testread5.pm line 8, near "$_ ~"
> syntax error at testread5.pm line 15, near "}"
>
> Do you the Regex to match $username needes some tweaking.


No, I think the two operators ~ and = in your code need to be replaced
by the single operator =~

~= is not an operator. It is the two operators ~ and =, which happen
to be sitting next to each other.

Paul Lalli

 
Reply With Quote
 
ipog_1234@yahoo.com
Guest
Posts: n/a
 
      10-12-2005
> if ( $_ =~ /^$username/ ){
> (undef,$password) = split;

}
the above regular expression "~=" was transposed to "=~" and the line
"(undef,$password) = split;" worked out well.
however, i modified it to put while stat into a single line:
#!/software/bin/perl -w
use warnings;
#use strict;
$file='/home/work/password.ini';
open(INFO, $file) or die "could not open '$file' $!";
$username="joe";
while(<INFO>){if ( $_ =~ /^$username/ ){(undef,$password) = split;}}
print "$password \n";
close(INFO);

My Question: can i put the entire open,while and close in a single line
of code.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Forcing password manager to save usernames/passwords for secure sites Jim Firefox 1 05-08-2005 12:04 PM
Usernames and Passwords on 1700 and 2600 series Northern Lights Cisco 3 08-03-2004 07:04 PM
Saving usernames but not passwords? RipVanWinkle Firefox 1 07-22-2004 04:21 AM
OT: Where does explorer put remembered usernames and passwords??? Titus A Ducksass HTML 3 12-25-2003 08:59 PM
How to secure a file of usernames and passwords? Ahmed Moustafa Java 5 08-20-2003 04:51 PM



Advertisments