split .ini file for passwords against usernames

Discussion in 'Perl Misc' started by ipog_1234@yahoo.com, Oct 12, 2005.

  1. Guest

    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);
    , Oct 12, 2005
    #1
    1. Advertising

  2. 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
    Josef Moellers, Oct 12, 2005
    #2
    1. Advertising

  3. Anno Siegel Guest

    <> 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.
    Anno Siegel, Oct 12, 2005
    #3
  4. <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 12, 2005
    #4
  5. Paul Lalli Guest

    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
    Paul Lalli, Oct 12, 2005
    #5
  6. Paul Lalli Guest

    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
    Paul Lalli, Oct 12, 2005
    #6
  7. 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
    Josef Moellers, Oct 12, 2005
    #7
  8. Guest

    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
    , Oct 12, 2005
    #8
  9. Paul Lalli Guest

    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?)

    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
    Paul Lalli, Oct 12, 2005
    #9
  10. Guest

    > 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.
    , Oct 12, 2005
    #10
  11. At 2005-10-12 01:20PM, <> wrote:
    > > 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.



    perl -lane 'if (/^joe/) {print $F[1]; last}' /home/work/password.ini

    --
    Glenn Jackman
    NCF Sysadmin
    Glenn Jackman, Oct 12, 2005
    #11
  12. Guest

    I tried this one liner:
    my $pass;
    $pass=`perl -lane 'if (/^joe/) {print $F[1];last}'
    /home/work/password.ini`

    and got this compilation error:
    Name "main::F" used only once: possible typo at greptest2.pm line 7.
    Use of uninitialized value in concatenation (.) or string at
    greptest2.pm line 7.

    $F should be splitting the column and print second column!!Does perl
    recogize this.??
    , Oct 12, 2005
    #12
  13. wrote:
    >> 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.


    You can put everything, from the first "use warnings;" to the very last
    ";" onto a single line. There are only a few places where perl actually
    cares about the presence or absence of newlines. You could even squeeze
    our a few more blanks to save space.
    Not that it makes the program any more readable ...

    Why would you want to do that? Do newline characters cost extra at your
    site? Can I set up a business there to sell newline characters (I could
    send you a megabyte of them at a very competitive price ;-)

    Your program code is already pretty dense and, as such, hard to read.
    Note that spaces, comment lines, and even some empty lines make your
    code much better to read and better to understand and maintain in the
    future.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Oct 13, 2005
    #13
  14. <> wrote:

    > #use strict;



    Strictures lose their effectiveness when you comment them out.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 13, 2005
    #14
  15. Eric Bohlman Guest

    wrote in news:1129152375.150772.223250
    @o13g2000cwo.googlegroups.com:

    > I tried this one liner:
    > my $pass;
    > $pass=`perl -lane 'if (/^joe/) {print $F[1];last}'
    > /home/work/password.ini`
    >
    > and got this compilation error:
    > Name "main::F" used only once: possible typo at greptest2.pm line 7.
    > Use of uninitialized value in concatenation (.) or string at
    > greptest2.pm line 7.
    >
    > $F should be splitting the column and print second column!!Does perl
    > recogize this.??


    Your problem is that backticks provide a double-quotish context, so your
    program is trying to interpolate $F[1] into the command string *before*
    executing it. You need to either escape the dollar sign with a backslash,
    or replace the backticks with qx'' (see "quotes and quote-like operators"
    in perlop).
    Eric Bohlman, Oct 14, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    IIS6 not logging Authenticated Usernames

    =?Utf-8?B?Sm9l?=, Sep 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    526
    Bruce Barker
    Sep 16, 2005
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    441
    Brian Palmer
    Aug 20, 2003
  3. Aaron Whiffin
    Replies:
    0
    Views:
    592
    Aaron Whiffin
    Sep 4, 2003
  4. Titus A Ducksass
    Replies:
    3
    Views:
    497
    brucie
    Dec 25, 2003
  5. Eric S. Johansson

    authenticating against shadow passwords

    Eric S. Johansson, Sep 12, 2003, in forum: Python
    Replies:
    0
    Views:
    299
    Eric S. Johansson
    Sep 12, 2003
Loading...

Share This Page