in reply to Unable to run or troubleshoot a script...

If I trim the cruft, use DATA (because this is a sample and I don't want to use external files), provide the user and password as constants (because this is a sample and I don't want to use STDIN), then:

#!/usr/bin/perl # read user data into a hash while ($line = <DATA>) { chomp $line; ($u, $p) = split /:/, $line; $ubase{$u} = $p; } my $uname = 'larry'; my $passwd = 'lingo'; # find command-line args in hash or exit die "cannot find user $uname\n\t" if ! defined $ubase{$uname}; die "cannot validate user password\n\t" if $passwd ne $ubase{$uname}; print "Validated user name and password\n"; __DATA__ larry:lingo tom:tonic ellie:plasma

Prints:

Validated user anme and password

as expected. Perhaps you didn't provide a correct password?

More important is to be aware of a couple of bad things this example is teaching you. First, always use strictures (use strict; use warnings;). The textbook example doesn't. Second, use the three parameter open because it's safer and clearer. Often too it's helpful to give the user the system generated error for a failed operation - die "couldn't open task data file: $!\n\t" (note the $!).


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Unable to run or troubleshoot a script...
by cgmd (Beadle) on Jun 26, 2007 at 03:37 UTC
    It appears the problem is related to my downloaded todo.users file, which, opened in the editor, shows as:
    larry:lingo tom:tonic ellie:plasma [ Read 4 lines (Converted from DOS format) ] ^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut + Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnC +ut Text ^T To Spell
    The issue relates the message at the bottom of the editor screen: Read 4 lines (Converted from DOS format). Having copied the same list of users:passwords to a fresh editor screen, the script runs as anticipated.

    In what way does the downloaded "DOS format" disrupt this file?

    Thanks!

      Most likely a difference in line end characters. "DOS" uses crlf where *nix uses lf and Mac uses cr.

      chomp (by default) removes the line end sequence used by the host OS so if you are running *nix and you have a file containing crlf line ends then you will get the cr left at the end of the line causing the password match to fail.


      DWIM is Perl's answer to Gödel
        I'm interested in the explanation by GrandFather:

        chomp (by default) removes the line end sequence used by the host OS so if you are running *nix and you have a file containing crlf line ends then you will get the cr left at the end of the line causing the password match to fail.

        This being the case, is there a simple means to convert such DOS formated files to a 'nix format? My success in doing this, so far, has been to select, copy and paste the text to an empty 'nix file.

        Thanks for the several helpful comments!