[32486] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3751 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Aug 5 21:09:21 2012

Date: Sun, 5 Aug 2012 18:09:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 5 Aug 2012     Volume: 11 Number: 3751

Today's topics:
    Re: a regex for removing a password in a source listing <cal@example.invalid>
    Re: a regex for removing a password in a source listing (Tim McDaniel)
    Re: a regex for removing a password in a source listing <cal@example.invalid>
    Re: a regex for removing a password in a source listing <nospampleasebutthisisvalid3@gmx.net>
    Re: A timeout question using Net::FTP <cal@example.invalid>
        changing a password in a do FILE <cal@example.invalid>
    Re: Duplicate Attribute <klaus03@gmail.com>
    Re: using a help file to populate ftp values <cal@example.invalid>
    Re: using a help file to populate ftp values <rweikusat@mssgmbh.com>
    Re: using a help file to populate ftp values <cal@example.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 04 Aug 2012 01:02:11 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: a regex for removing a password in a source listing
Message-Id: <iIOdnRkxoZ5uVYHNnZ2dnUVZ_vmdnZ2d@supernews.com>

On 07/31/2012 10:29 PM, Uri Guttman wrote:
> use File::Slurp qw( edit_file ) ;
>
> 	edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;

Alright, well I'm really happy with this result for the night. 
Obviously, the context has gone farther than this.

$ perltidy -b slurp1.pl
$ cat ftp_passwords_1.txt
my $domain = 'www.bedrock.com';
my $username = 'barney';
my $password = 'rocks';
$ perl slurp1.pl
$ cat ftp_passwords_1.txt
my $domain = 'www.bedrock.com';
my $username = 'barney';
my rocks = 'rocks';
$ cat slurp1.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use autodie;
my %info;
my $ident         = 'ftp_passwords_1.txt';
my $identity_file = $ident;

use File::Slurp qw( edit_file );

edit_file { s/\$password/rocks/g } $ident;

$


Q3. Juergen brought up character classes as a way to approach problems I 
face.  Ben defined the following character class 
[a-hjkmnp-zA-HJKMNP-Z2-9]. I have source that approaches the same 
question through arrays.  Instead of a a 2 period ellipses, there is a 
dash.  They have to be related in an obvious way that I just don't see 
yet.  What is it?

File::Slurp install after the sig.  I've never had YAML working yet, but 
I apparently do now.  I like how the install didn't trigger a bunch of 
dependencies I know nothing about.

-- 
Cal

cpan[2]> install File::Slurp
CPAN: Storable loaded ok (v2.22)
Going to read '/home/dan/.cpan/Metadata'
   Database was generated on Mon, 02 Jul 2012 03:58:03 GMT
CPAN: LWP::UserAgent loaded ok (v6.02)
CPAN: Time::HiRes loaded ok (v1.9719)
Fetching with LWP:
ftp://ftp.mpi-inf.mpg.de/pub/perl/CPAN/authors/01mailrc.txt.gz
CPAN: YAML loaded ok (v0.81)
Going to read '/home/dan/.cpan/sources/authors/01mailrc.txt.gz'
CPAN: Compress::Zlib loaded ok (v2.024)
 ............................................................................DONE
Fetching with LWP:
ftp://ftp.mpi-inf.mpg.de/pub/perl/CPAN/modules/02packages.details.txt.gz
Going to read '/home/dan/.cpan/sources/modules/02packages.details.txt.gz'
   Database was generated on Sat, 04 Aug 2012 05:16:57 GMT
 ..............
   New CPAN.pm version (v1.9800) available.
   [Currently running version is v1.9456]
   You might want to try
     install CPAN
     reload cpan
   to both upgrade CPAN.pm and run the new version without leaving
   the current session.


 ..............................................................DONE
Fetching with LWP:
ftp://ftp.mpi-inf.mpg.de/pub/perl/CPAN/modules/03modlist.data.gz
Going to read '/home/dan/.cpan/sources/modules/03modlist.data.gz'
 ............................................................................DONE
Going to write /home/dan/.cpan/Metadata
Running install for module 'File::Slurp'
Running make for U/UR/URI/File-Slurp-9999.19.tar.gz
Fetching with LWP:
ftp://ftp.mpi-inf.mpg.de/pub/perl/CPAN/authors/id/U/UR/URI/File-Slurp-9999.19.tar.gz
CPAN: Digest::SHA loaded ok (v5.47)
Fetching with LWP:
ftp://ftp.mpi-inf.mpg.de/pub/perl/CPAN/authors/id/U/UR/URI/CHECKSUMS
Checksum for 
/home/dan/.cpan/sources/authors/id/U/UR/URI/File-Slurp-9999.19.tar.gz ok
Scanning cache /home/dan/.cpan/build for sizes
 ............................................................................DONE
CPAN: Archive::Tar loaded ok (v1.54)
File-Slurp-9999.19/
File-Slurp-9999.19/t/
File-Slurp-9999.19/t/no_clobber.t
File-Slurp-9999.19/t/edit_file.t
File-Slurp-9999.19/t/write_file_win32.t
File-Slurp-9999.19/t/error.t
File-Slurp-9999.19/t/pod_coverage.t
File-Slurp-9999.19/t/perms.t
File-Slurp-9999.19/t/binmode.t
File-Slurp-9999.19/t/pod.t
File-Slurp-9999.19/t/file_object.t
File-Slurp-9999.19/t/stdin.t
File-Slurp-9999.19/t/prepend_file.t
File-Slurp-9999.19/t/read_dir.t
File-Slurp-9999.19/t/large.t
File-Slurp-9999.19/t/paragraph.t
File-Slurp-9999.19/t/error_mode.t
File-Slurp-9999.19/t/signal.t
File-Slurp-9999.19/t/TestDriver.pm
File-Slurp-9999.19/t/handle.t
File-Slurp-9999.19/t/data_list.t
File-Slurp-9999.19/t/original.t
File-Slurp-9999.19/t/append_null.t
File-Slurp-9999.19/t/inode.t
File-Slurp-9999.19/t/newline.t
File-Slurp-9999.19/t/slurp.t
File-Slurp-9999.19/t/tainted.t
File-Slurp-9999.19/t/data_scalar.t
File-Slurp-9999.19/t/pseudo.t
File-Slurp-9999.19/t/stringify.t
File-Slurp-9999.19/t/chomp.t
File-Slurp-9999.19/TODO
File-Slurp-9999.19/README
File-Slurp-9999.19/lib/
File-Slurp-9999.19/lib/File/
File-Slurp-9999.19/lib/File/Slurp.pm
File-Slurp-9999.19/MANIFEST
File-Slurp-9999.19/META.yml
File-Slurp-9999.19/Changes
File-Slurp-9999.19/extras/
File-Slurp-9999.19/extras/slurp_bench.pl
File-Slurp-9999.19/extras/slurp_article.pod
File-Slurp-9999.19/extras/FileSlurp_12.pm
File-Slurp-9999.19/Makefile.PL
CPAN: File::Temp loaded ok (v0.22)
CPAN: Parse::CPAN::Meta loaded ok (v1.4401)

   CPAN.pm: Going to build U/UR/URI/File-Slurp-9999.19.tar.gz

Checking if your kit is complete...
Looks good
Writing Makefile for File::Slurp
Writing MYMETA.yml and MYMETA.json
CPAN: Module::CoreList loaded ok (v2.50)
cp lib/File/Slurp.pm blib/lib/File/Slurp.pm
Manifying blib/man3/File::Slurp.3pm
   URI/File-Slurp-9999.19.tar.gz
   make -- OK
Running make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" 
"test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/append_null.t ....... ok
t/binmode.t ........... ok
t/chomp.t ............. ok
t/data_list.t ......... ok
t/data_scalar.t ....... ok
t/edit_file.t ......... ok
t/error.t ............. ok
t/error_mode.t ........ ok
t/file_object.t ....... ok
t/handle.t ............ ok
t/inode.t ............. ok
t/large.t ............. ok
t/newline.t ........... ok
t/no_clobber.t ........ ok
t/original.t .......... ok
t/paragraph.t ......... ok
t/perms.t ............. ok
t/pod.t ............... skipped: Test::Pod 1.14 required for testing POD
t/pod_coverage.t ...... skipped: Test::Pod::Coverage 1.04 required for 
testing POD coverage
t/prepend_file.t ...... ok
t/pseudo.t ............ ok
t/read_dir.t .......... ok
t/signal.t ............ ok
t/slurp.t ............. ok
t/stdin.t ............. ok
t/stringify.t ......... ok
t/tainted.t ........... ok
t/write_file_win32.t .. ok
All tests successful.
Files=28, Tests=296,  7 wallclock secs ( 0.19 usr  0.10 sys +  3.48 cusr 
  0.48 csys =  4.25 CPU)
Result: PASS
   URI/File-Slurp-9999.19.tar.gz
   make test -- OK
Running make install
Prepending /home/dan/.cpan/build/File-Slurp-9999.19-OaaBS4/blib/arch 
/home/dan/.cpan/build/File-Slurp-9999.19-OaaBS4/blib/lib to PERL5LIB for 
'install'
Installing /usr/local/share/perl/5.12.4/File/Slurp.pm
Installing /usr/local/man/man3/File::Slurp.3pm
Appending installation info to /usr/lib/perl/5.12/perllocal.pod
   URI/File-Slurp-9999.19.tar.gz
   make install  -- OK

 
          cpan[3]>



------------------------------

Date: Sat, 4 Aug 2012 18:15:39 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: a regex for removing a password in a source listing
Message-Id: <jvjosb$qd$1@reader1.panix.com>

In article <iIOdnRkxoZ5uVYHNnZ2dnUVZ_vmdnZ2d@supernews.com>,
Cal Dershowitz  <cal@example.invalid> wrote:
>On 07/31/2012 10:29 PM, Uri Guttman wrote:
>> use File::Slurp qw( edit_file ) ;
>>
>> 	edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;
>
>Alright, well I'm really happy with this result for the night. 
>Obviously, the context has gone farther than this.
>
>$ perltidy -b slurp1.pl
>$ cat ftp_passwords_1.txt
>my $domain = 'www.bedrock.com';
>my $username = 'barney';
>my $password = 'rocks';
>$ perl slurp1.pl
>$ cat ftp_passwords_1.txt
>my $domain = 'www.bedrock.com';
>my $username = 'barney';
>my rocks = 'rocks';
>$ cat slurp1.pl
>#!/usr/bin/perl -w
>use strict;
>use 5.010;
>use Net::FTP;
>use autodie;
>my %info;
>my $ident         = 'ftp_passwords_1.txt';
>my $identity_file = $ident;
>
>use File::Slurp qw( edit_file );
>
>edit_file { s/\$password/rocks/g } $ident;

Note the difference between the substitution that was suggested to you

>> 	edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;

and the substitution you did:

>edit_file { s/\$password/rocks/g } $ident;

The suggestion substituted the right-hand side of the assignment, so
it would change something like

    my $password = 'password';

to

    my $password = 'redacted';

Your substitution was different: it changed the left-hand side

    my $password = 'rocks';

to

    my rocks = 'rocks';

which is syntactically invalid and therefore useless.

The problem with that suggestion is that the "edit_file" invocation
itself has to have the password.  Consider trying instead my
suggestion here.  Make a vow that you will always, in that config
file, express your password in the form

    my $password = 'some value goes here';

That is, you may have spaces in there, but it will be a "my"
assignment to "$password" and alone on its line.  (That's a little
stronger than my suggestion requires, but it's a good simplifying vow
anyway.)

Then try

    edit_file { s/my\s+\$password\s*=.*/my \$password = 'redacted';/ } $ident;

That way, it just blow away everything after the assignment operator,
so it doesn't care what the old password value was.

>Q3. Juergen brought up character classes as a way to approach
>problems I face.  Ben defined the following character class
>[a-hjkmnp-zA-HJKMNP-Z2-9].

You didn't describe the problems, which makes it harder to advise you
on how to solve the problems.  Looking at the character class, I'm
thinking Juergen suggested avoiding any of [iIlLoO01] in some text
such as a password, as it's hard to distinguish those characters in
some typefaces.

>I have source that approaches the same question through arrays.
>Instead of a a 2 period ellipses, there is a dash.  They have to be
>related in an obvious way that I just don't see yet.

I don't see it either, because
- you didn't post the source statement(s) so we could see how the
  hyphen is being used
- I know of no way to get a range of values in a list using a hyphen

-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: Sat, 04 Aug 2012 18:14:42 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: a regex for removing a password in a source listing
Message-Id: <H9OdnfghxZlvJ4DNnZ2dnUVZ_oOdnZ2d@supernews.com>

On 08/04/2012 12:15 PM, Tim McDaniel wrote:
> In article<iIOdnRkxoZ5uVYHNnZ2dnUVZ_vmdnZ2d@supernews.com>,
> Cal Dershowitz<cal@example.invalid>  wrote:
>> On 07/31/2012 10:29 PM, Uri Guttman wrote:
>>> use File::Slurp qw( edit_file ) ;

snip
>> edit_file { s/\$password/rocks/g } $ident;
>
> Note the difference between the substitution that was suggested to you
>
>>> 	edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;
>
> and the substitution you did:
>
>> edit_file { s/\$password/rocks/g } $ident;
>
> The suggestion substituted the right-hand side of the assignment, so
> it would change something like
>
>      my $password = 'password';
>
> to
>
>      my $password = 'redacted';
>
> Your substitution was different: it changed the left-hand side
>
>      my $password = 'rocks';
>
> to
>
>      my rocks = 'rocks';
>
> which is syntactically invalid and therefore useless.
>
> The problem with that suggestion is that the "edit_file" invocation
> itself has to have the password.  Consider trying instead my
> suggestion here.  Make a vow that you will always, in that config
> file, express your password in the form
>
>      my $password = 'some value goes here';

My ex-wife and the mormons who made me promise to cut my throat before 
talking about their silliness in funny underwear--with hats--can tell 
you how my mileage with vows goes.:)

These password files have changed 30 times during this thread, so I'd 
like to focus on the representation that I want to go with.  I've had 
partial success with this but am mystified why I seem not to match here:

$ perl slurp1.pl
$ cat slurp1.pl
#!/usr/bin/perl -w
use strict;
use 5.010;

my $ident         = 'ftp_passwords_3.txt';
my $identity_file = $ident;

use File::Slurp qw( edit_file );

     edit_file { s/^\s*(password)/\t$1 => 'redacted',\n/ } $ident;
     edit_file { s/^\s*(username)/\t$1 => 'redacted',\n/ } $ident;

$ cat ftp_passwords_3.txt

     {
         my_ftp => {
             domain   => 'www.dinosaurs.com',
             username => 'barney',
             password => 'rocks',
             frobnitz => 17,
         },

         lutherhaven => {
             domain   => 'www.lh.com',
             username => 'fred',
             password => 'boulders',
             frobnitz => 42,
         },
     }

$

Also it just slaughters the white space when it does match.

>
> That is, you may have spaces in there, but it will be a "my"
> assignment to "$password" and alone on its line.  (That's a little
> stronger than my suggestion requires, but it's a good simplifying vow
> anyway.)
>
> Then try
>
>      edit_file { s/my\s+\$password\s*=.*/my \$password = 'redacted';/ } $ident;
>
> That way, it just blow away everything after the assignment operator,
> so it doesn't care what the old password value was.

I kept ftp stuff like this in perl syntax in my identity files, because 
the way I got the data out of there was copying and pasting.  I think I 
have a great method with do FILE now.
>
>> Q3. Juergen brought up character classes as a way to approach
>> problems I face.  Ben defined the following character class
>> [a-hjkmnp-zA-HJKMNP-Z2-9].
>
> You didn't describe the problems, which makes it harder to advise you
> on how to solve the problems.  Looking at the character class, I'm
> thinking Juergen suggested avoiding any of [iIlLoO01] in some text
> such as a password, as it's hard to distinguish those characters in
> some typefaces.

ok.
>
>> I have source that approaches the same question through arrays.
>> Instead of a a 2 period ellipses, there is a dash.  They have to be
>> related in an obvious way that I just don't see yet.
>
> I don't see it either, because
> - you didn't post the source statement(s) so we could see how the
>    hyphen is being used
> - I know of no way to get a range of values in a list using a hyphen
>

Proper arrays are lists, so this is counterexample:

$ perl change_pass1.pl
Random string: nmlbpoet
Length: 8

[^^^ I like this output]
$ cat change_pass1.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use File::Slurp;
my $ident = 'ftp_passwords_3.txt';

my $word_length = 8;
my $word        = &generate_random_string($word_length);
print "Random string: " . $word . "\n";
print "Length: " . length($word) . "\n";

###########################################################
# Written by Guy Malachi http://guymal.com
# 18 August, 2002
###########################################################

# This function generates random strings of a given length
sub generate_random_string {
     my $length_of_randomstring = shift;    # the length of

     my @chars = ( 'a' .. 'z' );
     my $random_string;
     foreach ( 1 .. $length_of_randomstring ) {

         # rand @chars will generate a random
         # number between 0 and scalar @chars
         $random_string .= $chars[ rand @chars ];
     }
     return $random_string;
}

One can see how Mr Malachi built up his lists.  That's a lot of single 
quoting if what I want is [a-hjkmnp-zA-HJKMNP-Z2-9].

What I'm reaching for looks kind of like this:

$ cat change_pass2.pl
 ...
     my @chars = ( 'a' .. 'z' );
     my @caps = [A-Z];
 ...
, except not exactly like this, because perl.exe doesn't think it's perl:

$ perl change_pass2.pl
Bareword "A" not allowed while "strict subs" in use at change_pass2.pl 
line 19.
Bareword "Z" not allowed while "strict subs" in use at change_pass2.pl 
line 19.
Execution of change_pass2.pl aborted due to compilation errors.
$

Thanks for your comment, tim, cheers.
-- 
Cal


------------------------------

Date: Sun, 05 Aug 2012 10:39:34 +0200
From: Wolf Behrenhoff <nospampleasebutthisisvalid3@gmx.net>
Subject: Re: a regex for removing a password in a source listing
Message-Id: <501e3146$0$6558$9b4e6d93@newsspool4.arcor-online.net>

On 05.08.2012 02:14, Cal Dershowitz wrote:
> Proper arrays are lists, so this is counterexample:

No. In Perl, lists and arrays are different. An array is not a list.
perldoc -q "What is the difference between a list and an array"

> One can see how Mr Malachi built up his lists.  That's a lot of single 
> quoting if what I want is [a-hjkmnp-zA-HJKMNP-Z2-9].
> 
> What I'm reaching for looks kind of like this:
> 
> $ cat change_pass2.pl
> ...
>      my @chars = ( 'a' .. 'z' );
>      my @caps = [A-Z];
> ...
> , except not exactly like this, because perl.exe doesn't think it's perl:

[ ... ] creates a reference to an array! Only when used in a regexp, the
[ ... ] is used to describe a character class.

You can avoid a bit of single quoting with the help of qw.

what about
my @chars = (
	2 .. 9,
	map { ($_, uc $_) }
        qw(j k m n), 'a' .. 'h', 'p' .. 'z' );

Or you put all allowed chars into a string and split it.



------------------------------

Date: Sun, 05 Aug 2012 17:04:16 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: A timeout question using Net::FTP
Message-Id: <QO-dnSRYWIhtZoPNnZ2dnUVZ_hGdnZ2d@supernews.com>

On 07/28/2012 05:23 AM, Ben Morrow wrote:
>
> Quoth Trudge<trudge@gmail.com>:
>> I have a script to download files. It checks the remote and local
>> directories, and downloads any new files from the remote location.
>>
>> Here is part of my code:
>> my $ftp = Net::FTP->new
>> (
>> 	"ftp.xxx.yy",
>> 	Timeout =>  3600,
>> 	Debug =>  0
>> ) or die "Could not connect: $@\n";
>>
>> My question is, what units are the Timeout option in? The docs don't
>> make it clear, and I'm guessing the units are seconds. Anyone have a
>> definitive answer?
>
> The timeout is passed to IO::Select, so it's in seconds, possibly
> fractional.

Idunno, Ben:


$ perltidy safe_post11.pl
$ perl safe_post11.pl my_ftp
 ..........
success is 22
$ cat safe_post11.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use diagnostics;

$| = 1; # Disable output buffering

## usage: perl safe_post11.pl server_name
my $ident = 'my_ftp.txt';
my ($config, $domain);
$config = do($ident);
unless ($config) {
     die("read error: $!") if $!;
     die("parse error: $@") if $@;
}

$domain = $config->{$ARGV[0]};
die("unknown domain: $ARGV[0]") unless $domain;

# printf("server\t%s\nuser\t%s\npass\t%s\n",
  #      $domain->{domain},
   #     $domain->{username},
    #    $domain->{password});

my $ftp      = Net::FTP->new( $domain->{domain},  Timeout => 5 ) or die 
"Can't connect: $@\n";
$ftp->login( $domain->{username}, $domain->{password} )
    or die "Couldn't login\n";

for (1..10) {
         print '.';
         sleep 1;
}
print "\n";

my @r = $ftp->ls();
my $success = scalar(@r);
print "success is $success\n";

# close $ftp;
$

cpan says 120 is the default, but haven't I changed that?
-- 
Cal


------------------------------

Date: Sun, 05 Aug 2012 16:16:23 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: changing a password in a do FILE
Message-Id: <cq6dnbLv19QmbYPNnZ2dnUVZ_tWdnZ2d@supernews.com>

On 08/05/2012 02:39 AM, Wolf Behrenhoff wrote:
> On 05.08.2012 02:14, Cal Dershowitz wrote:
>> Proper arrays are lists, so this is counterexample:
>
> No. In Perl, lists and arrays are different. An array is not a list.
> perldoc -q "What is the difference between a list and an array"

Alright, well it could make my head explode deciding what arrays are in 
the syntaxes I deal with, and how these then are different to how 
mathematicians treat arrays, then physicists, so in the interest of 
moving along....
> [ ... ] creates a reference to an array! Only when used in a regexp, the
> [ ... ] is used to describe a character class.

I see.
>
> You can avoid a bit of single quoting with the help of qw.
>
> what about
> my @chars = (
> 	2 .. 9,
> 	map { ($_, uc $_) }
>          qw(j k m n), 'a' .. 'h', 'p' .. 'z' );
>
> Or you put all allowed chars into a string and split it.
>

Thx, wolf, that got me over the hump.
$ perl change_pass4.pl
abcdefghjkmnpqrstuvwxyz
a b c d e f g h j k m n p q r s t u v w x y z
ABCDEFGHJKMNPQRSTUVWXYZ
A B C D E F G H J K M N P Q R S T U V W X Y Z
2 3 4 5 6 7 8 9
Random string: yqudhsB8
Length: 8
matched
             password => 'diamonds',

word is diamonds
matched
             password => 'dino',

word is dino
$ cat ftp_passwords_3.txt

     {
         my_ftp => {
             domain   => 'www.dinosaurs.com',
             username => 'betty',
             password => 'yqudhsB8',
             frobnitz => 17,
         },

         lutherhaven => {
             domain   => 'www.lh.com',
             username => 'wilma',
             password => 'yqudhsB8',
             frobnitz => 42,
         },
     }

$ cat change_pass4.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use File::Slurp;
use autodie;
my $ident = 'ftp_passwords_3.txt';
my $word_length = 8;
my $word        = &generate_random_string($word_length);
print "Random string: " . $word . "\n";
print "Length: " . length($word) . "\n";

my @lines = read_file( $ident) ;

open( my $gh, '>', $ident );
foreach (@lines) {
     if (/password/) {
         print "matched\n";
         print $_ . "\n";
         m/'(.*)'/;
         print "word is $1\n";
         s/'.*'/'$word'/;
         print $gh $_;
     }
     else {
print $gh $_;
}		
}
close $gh;



sub generate_random_string {
     my $length_of_randomstring = shift;
     my $word2                  = 'abcdefghjkmnpqrstuvwxyz';
     print "$word2\n";
     my @chars = split( //, $word2 );
     print "@chars\n";
     my $word3 = uc($word2);
     print "$word3\n";
     my @caps = split( //, $word3 );
     print "@caps\n";
     my @numbers = ( 2 .. 9 );
     print "@numbers\n";
     my $random_string;
     foreach ( 1 .. ($length_of_randomstring - 2) ) {
         $random_string .= $chars[ rand @chars ];
     }
     $random_string .= $caps[ rand @caps ];
     $random_string .= $numbers[ rand @numbers ];
     return $random_string;
}
$

As I was fiddling around with qw, I asked myself why I didn't just list 
the letters in a word, then manipulate that with the uc that wolf 
reminded me of.  I'd be interested to see how to do the same with map.

So this script "works" in the sense that it compiles and behaves as 
expected.  The obvious shortcoming is that it's going to change 
passwords for every server name.  There just has to be higher-level ways 
to do this than a regex.  This is all still pretty new to me, so I'm 
aware that the whole thing reads like it was written with crayons, but 
if I don't roll things out and print everything, then I just don't get far.

I've got the badger book coming.  We'll see how that goes.  (Less than $20.)
-- 
Cal


------------------------------

Date: Sat, 4 Aug 2012 11:26:39 -0700 (PDT)
From: Klaus <klaus03@gmail.com>
Subject: Re: Duplicate Attribute
Message-Id: <2ca9942c-bbeb-4169-8a62-2c5a0157fe0c@fj14g2000vbb.googlegroups.com>

On 7 juil, 00:38, mit...@juno.com wrote:
> I have an XML that looks like:
>
> <abc attr1="val1" attr2="val2" attr1="val3">
> </abc>
>
> Yes, this is illegal per XML rule since we have two attr1 and when I parse it through XML::Simple, I get this error message:
>
> duplicate attribute at line 14, column 291, byte 1936 at /usr/lib/perl5/XML/Parser.pm line 187
>
> However, I am wondering if there is a way to tell XML::Simple to ignore this error.

Maybe XML::Reader (version 0.44) can help out:

==========================================
use strict;
use warnings;

use XML::Reader 0.44 qw(XML::Parsepp slurp_xml);
use XML::Simple;
use Data::Dumper;

my $xml_with_dup =
  q{<abc attr1='val1' attr2='val2' attr1='val3'></abc>};

my $xml_without_dup = slurp_xml(\$xml_with_dup,
  { dupatt => '|' },
  { root => '/', branch => '*' })->[0][0];

my $ref = XMLin($xml_without_dup);

print "With    Dup = $xml_with_dup\n";
print "Without Dup = $xml_without_dup\n";
print Dumper($ref), "\n";
==========================================

This is the output:
==========================================
With    Dup = <abc attr1='val1' attr2='val2' attr1='val3'></abc>
Without Dup = <abc attr1='val1|val3' attr2='val2'></abc>
$VAR1 = {
          'attr2' => 'val2',
          'attr1' => 'val1|val3'
        };
==========================================


------------------------------

Date: Fri, 03 Aug 2012 23:55:02 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using a help file to populate ftp values
Message-Id: <48CdnWhPudSrJIHNnZ2dnUVZ_sidnZ2d@supernews.com>

On 08/02/2012 08:20 PM, Tim McDaniel wrote:
> In article<MM6dnXZvy9YyvIbNnZ2dnUVZ_rqdnZ2d@supernews.com>,
> Cal Dershowitz<cal@example.invalid>  wrote:

[snip]

> Unless the file also needs to be parsed by code not written in Perl,
> the usual "good reason" for making configuration files look like Perl
> is that Perl can already parse Perl: Configuration files using Perl
> syntax can be read with do FILE.  That is extremely easy to implement
> and is very powerful.
>
> For example, my $ORKPLACE has a config file that's Perl.  The last
> value evaluated is the config info, in the form of an anonymous hash
> along the lines of
>
>      {
>          servername1 =>  {
>              port =>  666,
>              database =>  'Fred',
>              email =>  'tmcd@panix.com',
>              frobotz =>  17,
>              env =>  {
>                  home = '...',
>                  ...
>              },
>              ...
>          },
>
>          servername2 =>  {
>              port =>  777,
>              database =>  'Bloggs',
>              email =>  'tmcd@panix.com',
>              frobotz =>  17,
>              ...
>          },
>      }

Thx for your comment, tim, this is definitely the way I want to go with 
this.  I don't understand much of you entire post at all, but I tried to 
start with the things I did.

One thing that occured to me was that if I wrote a config file in perl, 
then I could literally tidy it up:

$ perltidy -b ftp_passwords_3.txt
$ cat ftp_passwords_3.txt

     {
         my_ftp => {
             domain   => 'ftp.foo.com',
             username => 'cal',
             password => 'rocks',
             frobnitz => 17,
         },

         lutherhaven => {
             domain   => 'www.lh.com',
             username => 'fred',
             password => 'boulders',
             frobnitz => 42,
         },
     }

$



>
> Since it's Perl, I can refer to $ENV{HOME} and such to get environment
> variables; I do that a lot and it's very handy.  I could do `...` if I
> needed to run a command to get a config parameter.  There are many
> duplicated lines; some people start the file with something like, I
> think (I only saw this once):
>
>      my @common = (
>          email =>  'tmcd@panix.com',
>          frobotz =>  17,
>          ...
>      );
>
> and then define a server like
>
>          servername1 =>  {
>              @common,
>              port =>  666, ...
>          }
>
> I could define subs to generate chunks if I wanted, and call subs and
> use modules.
>

Ok, I didn't get much of this part at all, but I think I want to have 
port numbers on this stuff as well if I expect to get perl's full 
functionality for ftp.

I think the hash in this file is anonymous.  How do I recast the 
non-anonymous %info file to correctly get these data?
$ perl safe_post7.pl
Incorrect format for password file: [] at safe_post7.pl line 14, 
<$_[...]> line 1.
 ...
Can't connect: Net::FTP: Bad hostname ''
$

The early part of this script looks like this now:

#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use autodie;
my %info;
my $ident = 'ftp_passwords_3.txt';
my $identity_file = $ident;

open( my $gh, '<', $identity_file );

while (<$gh>) {
     chomp;
     my ( $k, $v ) = /^\s*(\w+):\s+(.*)$/
       or warn "Incorrect format for password file: [$_]";
     $info{$k} = $v;
}

My question is how to start out with what I have and end up having this bit:

my $ftp      = Net::FTP->new( $info{domain}, Debug => 1, Passive => 1 )
   or die "Can't connect: $@\n";
$ftp->login( $info{username}, $info{password} ) or die "Couldn't login\n";

be syntactic and behaving.  I greatly appreciate all comments.
-- 
Cal


------------------------------

Date: Sat, 04 Aug 2012 18:09:16 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: using a help file to populate ftp values
Message-Id: <87a9ya4m9v.fsf@sapphire.mobileactivedefense.com>

Cal Dershowitz <cal@example.invalid> writes:

[...]

> My question is how to start out with what I have and end up having this bit:
>
> my $ftp      = Net::FTP->new( $info{domain}, Debug => 1, Passive => 1 )
>   or die "Can't connect: $@\n";
> $ftp->login( $info{username}, $info{password} ) or die "Couldn't login\n";

Assuming there's a configuration file named /tmp/ftp-config with the
following content:

----------------
{
    my_ftp => {
	       domain   => 'ftp.foo.com',
	       username => 'cal',
	       password => 'rocks',
	       frobnitz => 17
	      },

   lutherhaven => {
		   domain   => 'www.lh.com',
		   username => 'fred',
		   password => 'boulders',
		   frobnitz => 42,
		  },
}
----------------

the sample code below can be used to parse this config file and print
the domain, username and password setting for a given 'ftp domain'
passed to the script as first command-line argument

----------------
my ($config, $domain);

$config = do('/tmp/ftp-config');
unless ($config) {
    die("read error: $!") if $!;
    die("parse error: $@") if $@;
}

$domain = $config->{$ARGV[0]};
die("unknown domain: $ARGV[0]") unless $domain;

printf("server\t%s\nuser\t%s\npass\t%s\n",
       $domain->{domain},
       $domain->{username},
       $domain->{password});
-----------------

Example:

[rw@sapphire]/tmp $perl sample.pl lutherhaven
server  www.lh.com
user    fred
pass    boulders



------------------------------

Date: Sat, 04 Aug 2012 17:22:31 -0600
From: Cal Dershowitz <cal@example.invalid>
Subject: Re: using a help file to populate ftp values
Message-Id: <WImdneNl14wqM4DNnZ2dnUVZ_gWdnZ2d@supernews.com>

On 08/04/2012 11:09 AM, Rainer Weikusat wrote:
> Cal Dershowitz<cal@example.invalid>  writes:

> Assuming there's a configuration file named /tmp/ftp-config with the
> following content:
>
> ----------------
> {
>      my_ftp =>  {
> 	       domain   =>  'ftp.foo.com',
> 	       username =>  'cal',
> 	       password =>  'rocks',
> 	       frobnitz =>  17
> 	      },
>
>     lutherhaven =>  {
> 		   domain   =>  'www.lh.com',
> 		   username =>  'fred',
> 		   password =>  'boulders',
> 		   frobnitz =>  42,
> 		  },
> }
> ----------------
>
> the sample code below can be used to parse this config file and print
> the domain, username and password setting for a given 'ftp domain'
> passed to the script as first command-line argument
>
> ----------------
> my ($config, $domain);
>
> $config = do('/tmp/ftp-config');
> unless ($config) {
>      die("read error: $!") if $!;
>      die("parse error: $@") if $@;
> }
>
> $domain = $config->{$ARGV[0]};
> die("unknown domain: $ARGV[0]") unless $domain;
>
> printf("server\t%s\nuser\t%s\npass\t%s\n",
>         $domain->{domain},
>         $domain->{username},
>         $domain->{password});
> -----------------
>
> Example:
>
> [rw@sapphire]/tmp $perl sample.pl lutherhaven
> server  www.lh.com
> user    fred
> pass    boulders
>

Wow, this is great, rainer, echt Klasse:

$ cat safe_post10.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;

## usage: perl safe_post10.pl server_name
my $ident = 'my_ftp.txt';
my ($config, $domain);
$config = do($ident);
unless ($config) {
     die("read error: $!") if $!;
     die("parse error: $@") if $@;
}

$domain = $config->{$ARGV[0]};
die("unknown domain: $ARGV[0]") unless $domain;

# printf("server\t%s\nuser\t%s\npass\t%s\n",
  #      $domain->{domain},
   #     $domain->{username},
    #    $domain->{password});

my $ftp      = Net::FTP->new( $domain->{domain}, Debug => 1, Passive => 
1 ) or die "Can't connect: $@\n";
$ftp->login( $domain->{username}, $domain->{password} )
    or die "Couldn't login\n";
close $ftp;
$

This totally works and can log in.  I've read up a little bit about do FILE

http://perldoc.perl.org/functions/do.html

, and find myself wondering what the hashes here are.  Have I defined a 
a hash in the above script?  Is there a hash that every program inherits 
that looks something like $ENV{HOME}} ?

Really happy with this result.  Thanks again.
-- 
Cal



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3751
***************************************


home help back first fref pref prev next nref lref last post