I killed my own source.

  • Follow


Net::FTP=GLOB(0x837217c)<<< 226 Transfer complete
.. .. logs index.html wsb6121022001 energy green2.m4v Video 151.wmv 
false.wmv vids images zen rev1.html luther1.html lh_1.html lh_17.html 
lh_18.html lh_19.html ceiling_2.html ceiling_2_files {word}_2.html 
{la_veta}_2.html la_veta1.html la_veta2.html la_veta3.html la_veta4.html 
la_veta5.html la_veta6.html lh_abc_2.html
$ echo 'piss on it, I'll do itagain'
 >
 > C^C
$ perl ceiling4.pl
Net::FTP>>> Net::FTP(2.77)

[blobbity bla]

(74,208,244,112,254,89).
Net::FTP=GLOB(0x9dad8d4)>>> NLST
Net::FTP=GLOB(0x9dad8d4)<<< 150 Opening BINARY mode data connection for 
file list
Net::FTP=GLOB(0x9dad8d4)<<< 226 Transfer complete
.. .. logs index.html wsb6121022001 energy green2.m4v Video 151.wmv 
false.wmv vids images zen rev1.html luther1.html lh_1.html lh_17.html 
lh_18.html lh_19.html ceiling_2.html ceiling_2_files {word}_2.html 
{la_veta}_2.html la_veta1.html la_veta2.html la_veta3.html la_veta4.html 
la_veta5.html la_veta6.html lh_abc_2.html lh_abc_3.html
$ cat lh_abc_3.html
<html>
<head>
<title>Lutherhaven Renovation</title>
</head>
<body bgcolor=white>
<h1>Basement Ceiling Materials</h1>
<img src="/images/image_62.jpg"/>

<p>caption for image_62.jpg </p>
<img src="/images/image_63.jpg"/>

<p>caption for image_63.jpg </p>
<img src="/images/image_64.jpg"/>

<p>caption for image_64.jpg </p>
<img src="/images/image_65.jpg"/>

<p>caption for image_65.jpg </p>
<img src="/images/image_66.jpg"/>

<p>caption for image_66.jpg </p>
$ cat ceiling4.pl
$ cat ceiling4.pl
$ echo "I murdered my source."
I murdered my source.
$ ls -l
total 145524
-rw-r--r-- 1 dan dan  2359350 2011-11-06 16:59 1and1.bmp
-rw-r--r-- 1 dan dan    66174 2012-01-10 20:03 
402789_2985747999782_1144491549_33319470_913147571_n.jpg
-rw-r--r-- 1 dan dan 26886706 2011-10-14 22:47 abq1.wmv
-rw-r--r-- 1 dan dan      688 2011-11-07 16:55 agd1.html
-rw-r--r-- 1 dan dan   102369 2012-01-10 02:15 agd1.jpg
-rw-r--r-- 1 dan dan      673 2011-11-12 17:21 agd2.html
-rw-r--r-- 1 dan dan    92055 2012-01-10 02:15 agd.jpg
-rw-rw-r-- 1 dan dan      143 2012-06-12 15:23 andrea1.pl
-rw-rw-r-- 1 dan dan      103 2012-06-12 15:22 andrea1.pl~
-rw-r--r-- 1 dan dan     1748 2011-10-11 21:39 beck1.txt
-rw-r--r-- 1 dan dan      896 2011-06-22 13:27 bill1.txt
-rw-r--r-- 1 dan dan     2810 2011-07-06 18:32 BOOTEX.LOG
-rw-rw-r-- 1 dan dan     2090 2012-07-04 01:09 ceiling1.pl
-rw-rw-r-- 1 dan dan     2090 2012-06-25 14:42 ceiling1.pl~
-rw-rw-r-- 1 dan dan      457 2012-06-25 14:42 ceiling_2.html
-rw-rw-r-- 1 dan dan     2107 2012-07-03 23:53 ceiling2.pl
-rw-rw-r-- 1 dan dan     2106 2012-07-03 23:52 ceiling2.pl~
-rw-rw-r-- 1 dan dan     2109 2012-07-27 18:07 ceiling3.pl
-rw-rw-r-- 1 dan dan     2107 2012-07-27 18:07 ceiling3.pl~
-rw-rw-r-- 1 dan dan        0 2012-07-28 04:08 ceiling4.pl
-rw-rw-r-- 1 dan dan     2085 2012-07-27 21:26 ceiling4.pl~
[snip]

$ cat ceiling4.pl~
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
my $domain   = 'www.merrillpjensen.com';
my $username = 'u61210220';
my $password = '';
my $word = "lh_abc";
my $ftp      = Net::FTP->new( $domain, Debug => 1, Passive => 1 )
   or die "Can't connect: $@\n";
$ftp->login( $username, $password ) or die "Couldn't login\n";
$ftp->binary();

# get files from remote root that end in html:
my @remote_files = $ftp->ls();

# print "remote files are: @remote_files\n";
my @matching = map /${word}_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 1 );

@matching = sort { $a <=> $b } @matching;
my $winner    = pop @matching;
my $newnum1   = $winner + 1;
my $html_file = "${word}_$newnum1.html";
print "html file is  $html_file\n";

# create file for html stubouts
open( my $fh, '>', $html_file )
   or die("Can't open $html_file for writing: $!");
print $fh "<html>\n";
print $fh "<head>\n";
print $fh "<title>Lutherhaven Renovation</title>\n";
print $fh "</head>\n";
print $fh "<body bgcolor=white>\n";
print $fh "<h1>Basement Ceiling Materials</h1>\n";

# get files from Desktop/images/
my $path  = '/home/dan/Desktop/upload/';
my @files = <$path*>;

# get ls from remote image directory
$ftp->cwd('/images/') or die "cwd failed $@\n";
my @list = $ftp->ls();

# main control
for my $name (@files) {
     print "name is $name\n";
     my ($ext) = $name =~ /([^.]*)$/;
     print "ext is $ext\n";

     @matching = map /image_(\d+)\.$ext$/, @list;
     print "matching is @matching\n";
     push( @matching, 1 );
     @matching = sort { $a <=> $b } @matching;
     $winner = pop @matching;
     my $newnum    = $winner + 1;
     my $new_file2 = "image_$newnum.$ext";
     print "newfile is $new_file2\n";
     $ftp->put( $name, $new_file2 ) or die "put failed $!\n";
     push( @list, $new_file2 );
     # unlink($name);

     print $fh "<img src=\"/images/$new_file2\"/>\n\n";
     print $fh "<p>caption for $new_file2 <\/p>\n";

}

close $fh;
$ftp->cdup() or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";
my @r = $ftp->ls();
print "@r\n";
$

What I tried to do was to write a source file to output.  Does argv 
contain the prog's name?

How would I switch _abc_ for a random composition of friendly letters in 
a character class?

I can take the easier route and look at script n-1, too.

Peace.
--
Cal
0
Reply cal819 (188) 7/28/2012 10:51:34 AM

Quoth Cal Dershowitz <cal@example.invalid>:
<snip>

I don't understand what any of that's supposed to be doing, or what it's
doing wrong...

> What I tried to do was to write a source file to output.  Does argv 
> contain the prog's name?

No. The program's name is in $0, and it may or may not be an absolute
path depending on how it was invoked.

> How would I switch _abc_ for a random composition of friendly letters in 
> a character class?

What do you mean?

Ben

0
Reply ben6057 (867) 7/28/2012 11:28:50 AM


On 28/07/12 11:51, Cal Dershowitz wrote:

snipped tedious console listing.  A couple of lines of explanation would 
have been better.

> $ cat ceiling4.pl
> $ cat ceiling4.pl
> $ echo "I murdered my source."
> I murdered my source.

snipped unnecessary directory listing.

> $ cat ceiling4.pl~

Snipped Perl listing which looked OK at a glance.

> What I tried to do was to write a source file to output.  Does argv
> contain the prog's name?
>
> How would I switch _abc_ for a random composition of friendly letters in
> a character class?
>
> I can take the easier route and look at script n-1, too.

Cal, this isn't a well-posed question.  In fact I have no idea what 
you're trying to do and what you ended up doing instead.

I've looked at your program (it's not runnable and I'm not going to 
invest the effort required to make it so) and I can't immediately see 
any reason why it should over-write its own source.

What's "argv" (actually ARGV in Perl) got to do with it?

A "source file"?  Source for what?  Do you mean "an HTML file"?

And I have no clue what you want to do when you talk about random 
compositions of friendly letters in a character class.  What's 
"friendly" in this context?  What character class?

If your reply looks interesting and makes clearer what you're trying to 
do I'll have a go at it.  Unless you're a bot, that is ...

-- 

Henry Law            Manchester, England


0
Reply news192 (134) 7/28/2012 9:29:10 PM

On 07/28/2012 05:28 AM, Ben Morrow wrote:
>
> Quoth Cal Dershowitz<cal@example.invalid>:
> <snip>
>
> I don't understand what any of that's supposed to be doing, or what it's
> doing wrong...

It's been a question I've been tossing around here, as in the C world, 
this would be UB.
Net::FTP=GLOB(0x9df1948)<<< 226 Transfer complete
.. .. logs index.html wsb6121022001 energy green2.m4v Video 151.wmv 
false.wmv vids images zen rev1.html luther1.html lh_1.html lh_17.html 
lh_18.html lh_19.html ceiling_2.html ceiling_2_files {word}_2.html 
{la_veta}_2.html la_veta1.html la_veta2.html la_veta3.html la_veta4.html 
la_veta5.html la_veta6.html lh_abc_2.html lh_abc_3.html jdwrt__2.html
$ cat ceiling5.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
my $domain   = 'www.merrillpjensen.com';
my $username = '';
my $password = '';
my $word = "jdwrt_";
my $ftp      = Net::FTP->new( $domain, Debug => 1, Passive => 1 )
   or die "Can't connect: $@\n";
$ftp->login( $username, $password ) or die "Couldn't login\n";
$ftp->binary();

# get files from remote root that end in html:
my @remote_files = $ftp->ls();

# print "remote files are: @remote_files\n";
my @matching = map /${word}_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 1 );

@matching = sort { $a <=> $b } @matching;
my $winner    = pop @matching;
my $newnum1   = $winner + 1;
my $html_file = "${word}_$newnum1.html";
print "html file is  $html_file\n";

# create file for html stubouts
open( my $fh, '>', $html_file )
   or die("Can't open $html_file for writing: $!");
print $fh "<html>\n";
print $fh "<head>\n";
print $fh "<title>Lutherhaven Renovation</title>\n";
print $fh "</head>\n";
print $fh "<body bgcolor=white>\n";
print $fh "<h1>Basement Ceiling Materials</h1>\n";

# get files from Desktop/images/
my $path  = '/home/dan/Desktop/upload/';
my @files = <$path*>;

# get ls from remote image directory
$ftp->cwd('/images/') or die "cwd failed $@\n";
my @list = $ftp->ls();

# main control
for my $name (@files) {
     print "name is $name\n";
     my ($ext) = $name =~ /([^.]*)$/;
     print "ext is $ext\n";

     @matching = map /image_(\d+)\.$ext$/, @list;
     print "matching is @matching\n";
     push( @matching, 1 );
     @matching = sort { $a <=> $b } @matching;
     $winner = pop @matching;
     my $newnum    = $winner + 1;
     my $new_file2 = "image_$newnum.$ext";
     print "newfile is $new_file2\n";
     $ftp->put( $name, $new_file2 ) or die "put failed $!\n";
     push( @list, $new_file2 );
     # unlink($name);

     print $fh "<img src=\"/images/$new_file2\"/>\n\n";
     print $fh "<p>caption for $new_file2 <\/p>\n";

}
my $sourcefile = 'ceiling5.pl';
open( my $gh, '<', $sourcefile )
   or die("Can't open $sourcefile for writing: $!");
while (<$gh>){
    chomp;
    print $fh '<p>'. $_ . '</p>';
}
close $gh;
close $fh;
$ftp->cdup() or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";
my @r = $ftp->ls();
print "@r\n";
$

It's there and kind of interesting how it got munged:

http://www.merrillpjensen.com/jdwrt__2.html
>
>> What I tried to do was to write a source file to output.  Does argv
>> contain the prog's name?
>
> No. The program's name is in $0, and it may or may not be an absolute
> path depending on how it was invoked.

I'll try that next time.
>
>> How would I switch _abc_ for a random composition of friendly letters in
>> a character class?
>
> What do you mean?

A subroutine to prepend 5 nice random letters, using the character class 
we talked about 3 weeks ago.  I still don't get how to call a subroutine 
properly, unless it's with &.

Btw, the images aren't mine.  I didn't make them but am making a free 
use of them.  Some are offensive.  Some have adult content.  I am not a 
pornographer.  Most are interesting.
-- 
Cal

0
Reply cal819 (188) 7/28/2012 9:42:02 PM

On 07/28/2012 03:29 PM, Henry Law wrote:

> Cal, this isn't a well-posed question. In fact I have no idea what
> you're trying to do and what you ended up doing instead.

I was trying verify if a person could do what I've now put in the 
subject, but my first problem was that I killed my source.  I had the 
the arrow going this way > instead of this way < in the open statement: 
  bye bye source.
>
> I've looked at your program (it's not runnable and I'm not going to
> invest the effort required to make it so) and I can't immediately see
> any reason why it should over-write its own source.
>
> What's "argv" (actually ARGV in Perl) got to do with it?
>
> A "source file"? Source for what? Do you mean "an HTML file"?
>
> And I have no clue what you want to do when you talk about random
> compositions of friendly letters in a character class. What's "friendly"
> in this context? What character class?
>
> If your reply looks interesting and makes clearer what you're trying to
> do I'll have a go at it. Unless you're a bot, that is ...
>

Not a bot, henry, JAPH.
$ cat lp3.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
my $domain     = 'www.merrillpjensen.com';
my $username   = '';
my $password   = '';
my $sourcefile = $0;

# get some ramdom letters
my $word_length = 5;
my $word        = &generate_random_string($word_length);
print "Random string: " . $word . "\n";
print "Length: " . length($word) . "\n";

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

# get files from remote root that end in html:
my @remote_files = $ftp->ls();

# print "remote files are: @remote_files\n";
my @matching = map /${word}_(\d+)\.html/, @remote_files;
print "matching is @matching\n";
push( @matching, 1 );

@matching = sort { $a <=> $b } @matching;
my $winner    = pop @matching;
my $newnum1   = $winner + 1;
my $html_file = "${word}_$newnum1.html";
print "html file is  $html_file\n";

# create file for html stubouts
open( my $fh, '>', $html_file )
   or die("Can't open $html_file for writing: $!");

print $fh "<Content-Type: text/html>\n";
print $fh "<html>\n";
print $fh "<head>\n";
print $fh "<title>" . $sourcefile . "</title>\n";
print $fh "</head>\n";
print $fh "<body bgcolor=white>\n";
print $fh "<h1>Source Listing</h1>\n";

# get files from Desktop/images/
my $path  = '/home/dan/Desktop/upload/';
my @files = <$path*>;

# get ls from remote image directory
$ftp->cwd('/images/') or die "cwd failed $@\n";
my @list = $ftp->ls();

# main control
for my $name (@files) {
     print "name is $name\n";
     my ($ext) = $name =~ /([^.]*)$/;
     print "ext is $ext\n";

     @matching = map /image_(\d+)\.$ext$/, @list;

     # print "matching is @matching\n";
     push( @matching, 1 );
     @matching = sort { $a <=> $b } @matching;
     $winner = pop @matching;
     my $newnum    = $winner + 1;
     my $new_file2 = "image_$newnum.$ext";
     print "newfile is $new_file2\n";
     $ftp->put( $name, $new_file2 ) or die "put failed $!\n";
     push( @list, $new_file2 );

     # unlink($name);

     print $fh "<img src=\"/images/$new_file2\"/>\n\n";
     print $fh "<p>caption for $new_file2 <\/p>\n";

}

open( my $gh, '<', $sourcefile )
   or die("Can't open $sourcefile for writing: $!");
while (<$gh>) {
     chomp;

     # replace things that look like diamonds with html literals

     s/</' &lt; '/;
     s/>/' &gt; '/;

     # this may look obvious, but it's not

     s/my \$password = '.*';/my \$password = 'redacted';/;
     s/my \$username = '.*';/my \$username = 'redacted';/;

     print $fh '<p>' . $_ . '</p>' . "\n";
}
close $gh;
close $fh;
$ftp->cdup() or die "cdup failed $@\n";
$ftp->put($html_file) or die "put failed $@\n";
my @r = $ftp->ls();

# print "@r\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
                                            # the random string to generate

     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;
}

$

So, this almost works, and I think my trouble is with the regex for 
substituting out the password and user name.  Sometimes it works; 
sometimes it doesn't:

http://www.merrillpjensen.com/ifeod_2.html

It seems like the wheels fall off the RE engine when I add the 
subroutine.  But I'm really proud of this so far.  I had all the time I 
wanted at night to read for the last few weeks and had only _Learning 
Perl_ to read.  No tv, 'puter, cell phone.  A very well-crafted book.
-- 
Cal

0
Reply cal819 (188) 7/29/2012 5:27:00 AM

Quoth Cal Dershowitz <cal@example.invalid>:
> On 07/28/2012 05:28 AM, Ben Morrow wrote:
> >
> > Quoth Cal Dershowitz<cal@example.invalid>:
> > <snip>
> >
> > I don't understand what any of that's supposed to be doing, or what it's
> > doing wrong...
> 
> It's been a question I've been tossing around here, as in the C world, 
> this would be UB.

I'll try once more. Please explain, *IN ENGLISH*, what you are trying to
do and what is not working. Dumping a whole lot of logs and code is not
an alternative to an explanation.

> >> How would I switch _abc_ for a random composition of friendly letters in
> >> a character class?
> >
> > What do you mean?
> 
> A subroutine to prepend 5 nice random letters, using the character class 
> we talked about 3 weeks ago.

I don't remember that discussion in detail, but picking 5 random letters
from a list isn't hard. perldoc -f rand.

> I still don't get how to call a subroutine 
> properly, unless it's with &.

    sub foo { ... }

    foo(...);

The brackets are required unless you declare the sub before using it. If
you want to be able to leave them out you can use one of

    sub foo;
    use subs qw/foo/;

to predeclare subs at the top of the file. (This is not entirely unlike
C prototypes, except for the 'prototype' part.)

> Btw, the images aren't mine.  I didn't make them but am making a free 
> use of them.  Some are offensive.  Some have adult content.  I am not a 
> pornographer.  Most are interesting.

FSVO...

Ben

0
Reply ben6057 (867) 7/29/2012 1:10:31 PM

On 07/29/2012 07:10 AM, Ben Morrow wrote:
>
> Quoth Cal Dershowitz<cal@example.invalid>:
>> On 07/28/2012 05:28 AM, Ben Morrow wrote:

[snipped, re-ordered, title changed]

>> I still don't get how to call a subroutine
>> properly, unless it's with&.
>
>      sub foo { ... }
>
>      foo(...);
>
> The brackets are required unless you declare the sub before using it. If
> you want to be able to leave them out you can use one of
>
>      sub foo;
>      use subs qw/foo/;
>
> to predeclare subs at the top of the file. (This is not entirely unlike
> C prototypes, except for the 'prototype' part.)

Ok, I think programmers of a certain age are simply averse to pushing 
the limits.  Perl is already so flexible compared to what we had 20 
years ago.


> I'll try once more. Please explain, *IN ENGLISH*, what you are trying to
> do and what is not working. Dumping a whole lot of logs and code is not
> an alternative to an explanation.

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

my $html_file = "/home/dan/Desktop/previousdec2011/iypjx_2.html";

# print "html file is  $html_file\n";

open( my $gh, '+<', $html_file )
   or die("Can't open $html_file  for writing: $!");
while (<$gh>) {
     chomp;

     # this is not working
     # this may look obvious, but it's not
     #   s/my \$password = '.*';/my \$password = 'redacted';/;
     #  s/my \$username = '.*';/my \$username = 'redacted';/;

     if (/\$password/) {
         print "matched\n";
         $_ =~ s/'.*'/'redacted'/;
     }

}
close $gh;
$

When I run this on these ftp upload files that have my password in it, I 
get "matched" 3 times like I would suspect, but I get no effective 
substitution.  Tried several things now.   As you see it, there's 
nothing that writes the line to the file.  When I add my best guess, I 
get very exotic output, which I would show but for the insistence that I 
not do so.

Hasn't anyone else used a a script to erase his passwords so that he can 
share his source without torpedoing his security?
-- 
Cal
-- 
Cal
0
Reply cal819 (188) 7/30/2012 9:26:02 PM

Am 30.07.2012 23:26, schrieb Cal Dershowitz:
> On 07/29/2012 07:10 AM, Ben Morrow wrote:
>> The brackets are required unless you declare the sub before using it. If
>> you want to be able to leave them out you can use one of
>>
>>      sub foo;
>>      use subs qw/foo/;
>>
>> to predeclare subs at the top of the file. (This is not entirely unlike
>> C prototypes, except for the 'prototype' part.)
> 
> Ok, I think programmers of a certain age are simply averse to pushing 
> the limits.  Perl is already so flexible compared to what we had 20 
> years ago.

Sorry, I don't understand - what does it have to do with pushing
something to the limits? Usually you do NOT need to predeclare a sub.

perl -wE'foo(); sub foo{ say q(I am foo) }'


> $ cat quine2.pl
> #!/usr/bin/perl -w
> use strict;
> use 5.010;
> 
> my $html_file = "/home/dan/Desktop/previousdec2011/iypjx_2.html";
> 
> # print "html file is  $html_file\n";
> 
> open( my $gh, '+<', $html_file )
>    or die("Can't open $html_file  for writing: $!");

ah, you open the file in rw mode.

> while (<$gh>) {

This is while (defined($_ = <$gh>)), i.e. you now have a new value in $_
corresponding to a line. This is a copy! So modifying $_ doesn't do
anything to the file!

>      chomp;
> 
>      # this is not working
>      # this may look obvious, but it's not
>      #   s/my \$password = '.*';/my \$password = 'redacted';/;
>      #  s/my \$username = '.*';/my \$username = 'redacted';/;
> 
>      if (/\$password/) {
>          print "matched\n";
>          $_ =~ s/'.*'/'redacted'/;

Are you sure about this replacement? Replacing anyhing between the first
' of the line and the last ' of the line?

>      }
> 
> }
> close $gh;
> $
> 
> When I run this on these ftp upload files that have my password in it, I 
> get "matched" 3 times like I would suspect, but I get no effective 
> substitution.  Tried several things now.   As you see it, there's 
> nothing that writes the line to the file.  When I add my best guess, I 
> get very exotic output, which I would show but for the insistence that I 
> not do so.

You are not writing to the file. So the replacement doesn't have any effect.

> Hasn't anyone else used a a script to erase his passwords so that he can 
> share his source without torpedoing his security?

What about perl's in-place edit? See perldoc perlrun!

Just run
perl -pi -e 's/password/redacted/g' /path/to/iypjx_2.html

or maybe
perl -pi -e "s/'password'/'redacted'/g" /path/to/iypjx_2.html
if you only want to replace the password if it is between single quotes.

(you can also run perl -pi.bak -e ... to get a backup file with .bak
extension)

- Wolf

0
Reply NoSpamPleaseButThisIsValid3 (39) 7/30/2012 9:46:53 PM

Quoth Cal Dershowitz <cal@example.invalid>:
> On 07/29/2012 07:10 AM, Ben Morrow wrote:
> 
> > I'll try once more. Please explain, *IN ENGLISH*, what you are trying to
> > do and what is not working. Dumping a whole lot of logs and code is not
> > an alternative to an explanation.
> 
> $ cat quine2.pl

You have omitted the part where you explain in English what you are
trying to do. It may be possible to divine from your code, but that is
not the same as an explanation. An explanation looks something like

    I am trying to open a Perl source file and copy it to another file
    with all the passwords removed.

The code you posted before was doing a great number of things, and it
wasn't at all clear which of those things wasn't working.

> #!/usr/bin/perl -w
> use strict;
> use 5.010;
> 
> my $html_file = "/home/dan/Desktop/previousdec2011/iypjx_2.html";
> 
> # print "html file is  $html_file\n";
> 
> open( my $gh, '+<', $html_file )
>    or die("Can't open $html_file  for writing: $!");
> while (<$gh>) {
>      chomp;
> 
>      # this is not working
>      # this may look obvious, but it's not
>      #   s/my \$password = '.*';/my \$password = 'redacted';/;
>      #  s/my \$username = '.*';/my \$username = 'redacted';/;
> 
>      if (/\$password/) {
>          print "matched\n";
>          $_ =~ s/'.*'/'redacted'/;
>      }
> 
> }
> close $gh;
> $
> 
> When I run this on these ftp upload files that have my password in it, I 
> get "matched" 3 times like I would suspect, but I get no effective 
> substitution.  Tried several things now.   As you see it, there's 
> nothing that writes the line to the file.  When I add my best guess, I 
> get very exotic output, which I would show but for the insistence that I 
> not do so.

You could at least show us what your best guess was, since I believe the
substitution is working perfectly. You are, I hope, aware that
attempting to overwrite a file while you are reading it is likely to
produce confusing results? The normal procedure is to write a new file
(you can create a secure temporary file with File::Temp) and rename it
over the top.

This

    #!/usr/bin/perl

    use warnings;
    use strict;

    while (<DATA>) {
        if (/\$password/) {
            s/'.*'/'redacted'/;
        }
        print;
    }

    __DATA__
    use warnings;
    use strict;
    use Net::FTP;

    my $password = 'foo';

    my $F = Net::FTP->new(...);

works for me: that is, it prints the contents of the DATA section with
the password assignment replaced.

> Hasn't anyone else used a a script to erase his passwords so that he can 
> share his source without torpedoing his security?

It's not a common thing to want to do, because it's generally a bad idea
to put passwords directly in the script file in the first place. The
best thing to do, if you need a password, is to prompt for it (you can
use Term::ReadKey to prompt without echoing); if you must keep it in a
file, make it a separate config file so the script can be shared with
others/checked into a VCS/whatever without revealing your passwords.

Ben

0
Reply ben6057 (867) 7/30/2012 11:00:50 PM

On 07/30/2012 03:46 PM, Wolf Behrenhoff wrote:
> Am 30.07.2012 23:26, schrieb Cal Dershowitz:

> ah, you open the file in rw mode.

Thx for your comment, Wolf, I didn't have any luck with it at all.  For 
the small thing I'm doing, I just to decided to read the file into an 
array, manipulate the array, and then write it to a file.
>
>> while (<$gh>) {
>
> This is while (defined($_ =<$gh>)), i.e. you now have a new value in $_
> corresponding to a line. This is a copy! So modifying $_ doesn't do
> anything to the file!

I think I solve that here:

#!/usr/bin/perl -w
use strict;
use 5.010;

my $source_file = "/home/dan/Desktop/source/marni1.pl";
my (@lines);
open( my $gh, '<', $source_file )
   or die("Can't open $source_file  for writing: $!");
chomp( @lines = <$gh> );
close $gh;
my $name = 'post1.pl';
open( my $fh, '>', $name )
   or die("Can't open $name  for writing: $!");
foreach (@lines) {

     if (/\$password|\$username/) {
         print "matched\n";
         s/'.*'/'redacted'/;
     }
     $_ .= "\n";
     print $fh $_;
}


>
>>       chomp;
>>
>>       # this is not working
>>       # this may look obvious, but it's not
>>       #   s/my \$password = '.*';/my \$password = 'redacted';/;
>>       #  s/my \$username = '.*';/my \$username = 'redacted';/;
>>
>>       if (/\$password/) {
>>           print "matched\n";
>>           $_ =~ s/'.*'/'redacted'/;
>
> Are you sure about this replacement? Replacing anyhing between the first
> ' of the line and the last ' of the line?

It works in the context I'm using i, but I'd feel better if the regex 
checked for and equals sign being between them.  (a little ambitious for me)

> What about perl's in-place edit? See perldoc perlrun!
>
> Just run
> perl -pi -e 's/password/redacted/g' /path/to/iypjx_2.html
>
> or maybe
> perl -pi -e "s/'password'/'redacted'/g" /path/to/iypjx_2.html
> if you only want to replace the password if it is between single quotes.
>
> (you can also run perl -pi.bak -e ... to get a backup file with .bak
> extension)
>
> - Wolf
>

Those again, are clever and a little bit beyond my game, but I wouldn't 
see how they wouldn't do bad things to this line, which is very typical 
in an ftp program:

$ftp->login($username, $password)       or die "Couldn't login\n";
0
Reply cal819 (188) 8/1/2012 4:12:27 AM

>>>>> "WB" == Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net> writes:

  >> while (<$gh>) {

  WB> This is while (defined($_ = <$gh>)), i.e. you now have a new value in $_
  WB> corresponding to a line. This is a copy! So modifying $_ doesn't do
  WB> anything to the file!

wow. the OP had a massively odd view of rw files.


  WB> What about perl's in-place edit? See perldoc perlrun!

  WB> Just run
  WB> perl -pi -e 's/password/redacted/g' /path/to/iypjx_2.html

  WB> or maybe
  WB> perl -pi -e "s/'password'/'redacted'/g" /path/to/iypjx_2.html
  WB> if you only want to replace the password if it is between single quotes.

since he is doing this in a script and not the command line, edit_file
or edit_file_lines from File::Slurp with do that easily

use File::Slurp qw( edit_file ) ;

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

done.

to the OP: next time stop with the nonsense, the quine, the logs, and
just say you want to edit a file in place. pretty simple concept but you
said everything BUT that.

uri
0
Reply uri (200) 8/1/2012 4:29:35 AM

>>>>> "CD" == Cal Dershowitz <cal@example.invalid> writes:

  CD> my $source_file = "/home/dan/Desktop/source/marni1.pl";
  CD> my (@lines);
  CD> open( my $gh, '<', $source_file )
  CD>   or die("Can't open $source_file  for writing: $!");
  CD> chomp( @lines = <$gh> );

why chomp all the lines just to add a newline back later???
  CD> close $gh;
  CD> my $name = 'post1.pl';
  CD> open( my $fh, '>', $name )
  CD>   or die("Can't open $name  for writing: $!");
  CD> foreach (@lines) {

  CD>     if (/\$password|\$username/) {
  CD>         print "matched\n";
  CD>         s/'.*'/'redacted'/;
  CD>     }
  CD>     $_ .= "\n";
  CD>     print $fh $_;
  CD> }

see my other post for clean one line solution. 

uri
0
Reply uri (200) 8/1/2012 4:30:57 AM

On 07/30/2012 05:00 PM, Ben Morrow wrote:
>
> Quoth Cal Dershowitz<cal@example.invalid>:
>> On 07/29/2012 07:10 AM, Ben Morrow wrote:
>>
>>> I'll try once more. Please explain, *IN ENGLISH*, what you are trying to
>>> do and what is not working. Dumping a whole lot of logs and code is not
>>> an alternative to an explanation.
>>
>> $ cat quine2.pl
>
> You have omitted the part where you explain in English what you are
> trying to do. It may be possible to divine from your code, but that is
> not the same as an explanation. An explanation looks something like
>
>      I am trying to open a Perl source file and copy it to another file
>      with all the passwords removed.

That's one little tool in a chain I'm trying build.  To hear you say it 
helped me figure out what I had to do.  I'm not trying to be a jerk 
about not describing the subject as well as I can, but if I knew what I 
was talking about, then I wouldn't have to ask questions.
>> When I run this on these ftp upload files that have my password in it, I
>> get "matched" 3 times like I would suspect, but I get no effective
>> substitution.  Tried several things now.   As you see it, there's
>> nothing that writes the line to the file.  When I add my best guess, I
>> get very exotic output, which I would show but for the insistence that I
>> not do so.
>
> You could at least show us what your best guess was, since I believe the
> substitution is working perfectly. You are, I hope, aware that
> attempting to overwrite a file while you are reading it is likely to
> produce confusing results? The normal procedure is to write a new file
> (you can create a secure temporary file with File::Temp) and rename it
> over the top.
>
> This
>
>      #!/usr/bin/perl
>
>      use warnings;
>      use strict;
>
>      while (<DATA>) {
>          if (/\$password/) {
>              s/'.*'/'redacted'/;
>          }
>          print;
>      }
>
>      __DATA__
>      use warnings;
>      use strict;
>      use Net::FTP;
>
>      my $password = 'foo';
>
>      my $F = Net::FTP->new(...);
>
> works for me: that is, it prints the contents of the DATA section with
> the password assignment replaced.

I certainly appreciate that you take the time to entertain yourself with 
my computer problems, ben.  That part was solid, but somehow I seemed to 
get tripped up in the same material tonight.  (see below)
>
>> Hasn't anyone else used a a script to erase his passwords so that he can
>> share his source without torpedoing his security?
>
> It's not a common thing to want to do, because it's generally a bad idea
> to put passwords directly in the script file in the first place. The
> best thing to do, if you need a password, is to prompt for it (you can
> use Term::ReadKey to prompt without echoing); if you must keep it in a
> file, make it a separate config file so the script can be shared with
> others/checked into a VCS/whatever without revealing your passwords.
>
> Ben
>

$ perl safe_post4.pl
String found where operator expected at safe_post4.pl line 17, near 
"$word =~ m/'(\w+)'"
	(Missing operator before '(\w+)'?)
syntax error at safe_post4.pl line 17, near "$word =~ m/'(\w+)'"
Unmatched right curly bracket at safe_post4.pl line 21, at end of line
syntax error at safe_post4.pl line 21, near "}"
Execution of safe_post4.pl aborted due to compilation errors.
$ cat safe_post4.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
my ($domain, $username, $password);
my $identity_file = 'ftp_passwords_1.txt';
my (@lines, $word);
open( my $gh, '<', $identity_file )
   or die("Can't open $identity_file  for writing: $!");
chomp(@lines = <$gh>);
close $gh;
print "@lines\n";

foreach (@lines) {
     if (/\$domain\) {
         print "matched\n";
         $word =~ m/'(\w+)'/;
         $domain = $word;
     }
print "domain is $domain\n";
}

$ cat ftp_passwords_1.txt
my $domain = 'www.bedrock.com';
my $username = 'barney';
my $password = 'rocks';
$

This looks like perl to me.  Perl.exe disagrees.  What gives?
-- 
Cal
0
Reply cal819 (188) 8/1/2012 6:00:21 AM

Am 01.08.2012 08:00, schrieb Cal Dershowitz:
>      if (/\$domain\) {
>          print "matched\n";
>          $word =~ m/

This is the whole match! From the first / right after the if to the
second one which only appears 2 lines later.

You simply forgot to end the match with / and used the \ instead.


0
Reply NoSpamPleaseButThisIsValid3 (39) 8/1/2012 10:11:10 AM

On 08/01/2012 04:11 AM, Wolf Behrenhoff wrote:
> Am 01.08.2012 08:00, schrieb Cal Dershowitz:
>>       if (/\$domain\) {
>>           print "matched\n";
>>           $word =~ m/
>
> This is the whole match! From the first / right after the if to the
> second one which only appears 2 lines later.
>
> You simply forgot to end the match with / and used the \ instead.
>
>

Thanks, wolf.  I get times where I can't see the difference between the 
forward and backslash.  I call it "Dual-Boot Fatigue Syndrome."

$ perl safe_post4.pl
matched
my $domain = 'www.bedrock.com';
word is www.bedrock.com
matched
my $username = 'barney';
word is barney
matched
my $password = 'rocks';
word is rocks
$ cat safe_post4.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
my ( $domain, $username, $password );
my $identity_file = 'ftp_passwords_1.txt';
my ( @lines, $word );
open( my $gh, '<', $identity_file )
   or die("Can't open $identity_file  for writing: $!");
chomp( @lines = <$gh> );
close $gh;

foreach (@lines) {
     if (/\$domain/) {
         print "matched\n";
         print $_ . "\n";
         m/'(.*)'/;
         $domain = $1;
         print "word is $domain\n";
     }
}

foreach (@lines) {
     if (/\$username/) {
         print "matched\n";
         print $_ . "\n";
         m/'(.*)'/;
         $username = $1;
         print "word is $username\n";
     }
}

foreach (@lines) {
     if (/\$password/) {
         print "matched\n";
         print $_ . "\n";
         m/'(.*)'/;
         $password = $1;
         print "word is $password\n";
     }
}

$

This works, but I wouldn't be too wild about seeing it included in all 
my ftp scripts.

Q1)  How could one read the word $domain or anything with a dollar sign 
on it in the help file and then declare it at top scope in the script?
0
Reply cal819 (188) 8/2/2012 2:14:36 AM

On 07/31/2012 10:29 PM, Uri Guttman wrote:
>>>>>> "WB" == Wolf Behrenhoff<NoSpamPleaseButThisIsValid3@gmx.net>  writes:
>
>    >>  while (<$gh>) {
>
>    WB>  This is while (defined($_ =<$gh>)), i.e. you now have a new value in $_
>    WB>  corresponding to a line. This is a copy! So modifying $_ doesn't do
>    WB>  anything to the file!
>
> wow. the OP had a massively odd view of rw files.

I don't think I have enough of an idea about them for it to be odd.  Of 
course, when I saw the output, I knew I'd just put my data through a 
chipper, where the resulting chips may have been the sum total of input 
and output, but like I say, it was a chipper.
>
>
>    WB>  What about perl's in-place edit? See perldoc perlrun!
>
>    WB>  Just run
>    WB>  perl -pi -e 's/password/redacted/g' /path/to/iypjx_2.html
>
>    WB>  or maybe
>    WB>  perl -pi -e "s/'password'/'redacted'/g" /path/to/iypjx_2.html
>    WB>  if you only want to replace the password if it is between single quotes.
>
> since he is doing this in a script and not the command line, edit_file
> or edit_file_lines from File::Slurp with do that easily
>
> use File::Slurp qw( edit_file ) ;
>
> 	edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;
>
> done.

....in a way that unnecessarily ruins this line:

$ftp->login($username, $password)       or die "Couldn't login\n";
>
> to the OP: next time stop with the nonsense, the quine, the logs, and
> just say you want to edit a file in place. pretty simple concept but you
> said everything BUT that.
>
> uri

Well, uri, I just came down from the mountains, and I've decided to turn 
over a few new leafs.  Maybe one of them could be with you.  Would you 
like me to try File::Slurp and see if it can work into my own meaningful 
projects?

If I did without your permission, you could say, "I never asked you to 
touch this software."

I hope not to sound ironic, combative, all that.  Cheers,
-- 
Cal
0
Reply cal819 (188) 8/2/2012 2:53:54 AM

Cal Dershowitz <cal@example.invalid> wrote:
>Q1)  How could one read the word $domain or anything with a dollar sign 
>on it in the help file and then declare it at top scope in the script?

Are you looking for a template system?

jue
0
Reply jurgenex (445) 8/2/2012 3:17:49 AM

>>>>> "CD" == Cal Dershowitz <cal@example.invalid> writes:

  CD> On 07/31/2012 10:29 PM, Uri Guttman wrote:
  >> since he is doing this in a script and not the command line, edit_file
  >> or edit_file_lines from File::Slurp with do that easily
  >> 
  >> use File::Slurp qw( edit_file ) ;
  >> 
  >> edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;
  >> 
  >> done.

  CD> ...in a way that unnecessarily ruins this line:

  CD> $ftp->login($username, $password)       or die "Couldn't login\n";

huh??

  >> to the OP: next time stop with the nonsense, the quine, the logs, and
  >> just say you want to edit a file in place. pretty simple concept but you
  >> said everything BUT that.

  CD> Well, uri, I just came down from the mountains, and I've decided to
  CD> turn over a few new leafs.  Maybe one of them could be with you.
  CD> Would you like me to try File::Slurp and see if it can work into my
  CD> own meaningful projects?

huh??

you can use the module any way you want and anywhere you want. it is on
cpan for the world to enjoy.

  CD> If I did without your permission, you could say, "I never asked you to
  CD> touch this software."

well, you can USE it but you can't modify it on cpan as i own it. if you
come up with useful patches that i approve, i may add them. or you could
fork the module and make your own version.

  CD> I hope not to sound ironic, combative, all that.  Cheers,

no. but you do sound like you need to learn about cpan.

uri
0
Reply uri (200) 8/2/2012 5:44:58 AM

Quoth Cal Dershowitz <cal@example.invalid>:
> 
> $ perl safe_post4.pl
> matched
> my $domain = 'www.bedrock.com';
> word is www.bedrock.com
> matched
> my $username = 'barney';
> word is barney
> matched
> my $password = 'rocks';

I hope this isn't your real password...

> word is rocks
> $ cat safe_post4.pl
> #!/usr/bin/perl -w
> use strict;
> use 5.010;
> use Net::FTP;
> my ( $domain, $username, $password );
> my $identity_file = 'ftp_passwords_1.txt';
> my ( @lines, $word );
> open( my $gh, '<', $identity_file )
>    or die("Can't open $identity_file  for writing: $!");

*Reading*. If you use 'autodie' it will get this right for you.

> chomp( @lines = <$gh> );
> close $gh;
> 
> foreach (@lines) {
>      if (/\$domain/) {
>          print "matched\n";
>          print $_ . "\n";
>          m/'(.*)'/;
>          $domain = $1;
>          print "word is $domain\n";
>      }
> }
> 
> foreach (@lines) {

Why are you iterating over the lines multiple times? You can do several
matches in one loop.

Why have you made your configuration file format look like Perl? Perl is
not a particularly easy language to parse. I would suggest something
simpler; perhaps

    domain: ftp.foo.com
    username: cal
    password: xxxx

which you could then read into a hash something like this:

    my %info;
    {
        open my $INFO, "<", ...;
        while (<$INFO>) {
            chomp;
            my ($k, $v) = /^(\w+):\s+(.*)$/ 
                or die "Incorrect format for password file: [$_]";
            $info{$k} = $v;
        }
        # no need to close, perl will do this for you when $INFO goes
        # out of scope
    }

(There are certainly more concise ways of doing this--I might use map
and File::Slurp--but this is simple and straightforward.)

You could also use one of the existing config file parsers, perhaps
YAML::XS or Config::Simple, though for something this trivial it
probably isn't worth it.

> Q1)  How could one read the word $domain or anything with a dollar sign 
> on it in the help file and then declare it at top scope in the script?

What is 'the help file'? This file you've put your passwords in?

You can't, at least not without trickery of some sort. The whole point
of lexical ('my') variables is that they have to be declared where you
can see them, in the file (and scope) you're looking at. You could
require a file which set some globals, but that's not a good idea for
all the usual reasons.

Ben

0
Reply ben6057 (867) 8/2/2012 5:53:40 AM

Ben Morrow <ben@morrow.me.uk> writes:

[...]

>
> Why have you made your configuration file format look like Perl? Perl is
> not a particularly easy language to parse.

Except if the file also needs to be parsed by code not written in Perl,
the usual 'good reason' for that would be that perl can already parse
Perl: Configuration files using Perl syntax can be read with do FILE.


0
Reply rweikusat (2679) 8/2/2012 10:47:10 AM

In article <87r4rponj9.fsf@stemsystems.com>,
Uri Guttman  <uri@stemsystems.com> wrote:
>>>>>> "CD" == Cal Dershowitz <cal@example.invalid> writes:
>
>  CD> On 07/31/2012 10:29 PM, Uri Guttman wrote:
>  >> since he is doing this in a script and not the command line, edit_file
>  >> or edit_file_lines from File::Slurp with do that easily
>  >> 
>  >> use File::Slurp qw( edit_file ) ;
>  >> 
>  >> edit_file { s/'password'/'redacted'/g } '/path/to/iypjx_2.html' ;
>  >> 
>  >> done.
>
>  CD> ...in a way that unnecessarily ruins this line:
>
>  CD> $ftp->login($username, $password)       or die "Couldn't login\n";
>
>huh??

To expand on the "huh??" (which was my initial reaction too): in

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

(1) he didn't mean the literal word

        password

He meant whatever password or other text that you're trying to redact.
"whatevrUCh0se4APassw0rd", or whatever your password is, would not
match the text $password in your ftp->login line.

(2) Even if that line

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

were literally there, it can't modify

>  CD> $ftp->login($username, $password)       or die "Couldn't login\n";

Note that there are quotation marks in s/'password'/ -- those
quotation marks are part of the pattern being searched for and
substituted for, Since there are no quotation marks around $password
in

>  CD> $ftp->login($username, $password)       or die "Couldn't login\n";

the s/// wouldn't touch it.

(3) If you're going to do substitution to source code, there's a risk
of false positives no matter what you do -- the substitution code may
not bright enough to know the context; really, only a full Perl parser
could do a perfect job.  Metaphorically, it's inherently sinful, so
don't be surprised if you get hit by lightning.

If we were talking about multi-user systems, login information does
not belong in scripts, if for no other reason than that any user has
to be able to read the script (barring some work behind the scenes if
Linux) to be able to run it and can therefore find the login and
password.

When I needed it on a multi-user system, I had the public script read
it from a separate file.  The public script was in the source-code
repository, so anyone could see it.  The login info file was readable
and writable only by me and lived only on the system where I ran the
script.

Or at least you can assign it at the top

    my $username = 'Fred';
    my $password = 'Bloggs';

and use $username and $password throughout.  Then you don't have to
have the inherent riskiness of source substitution, and don't have to
bother with writing a program to do it -- you just delete them by
hand.

(Note to others: yeah, I consider

    use constant USERNAME => 'Fred';
    use constant PASSWORD => 'Bloggs';

to be better.  I just didn't want to get into "but here's how you'd
have to use it if you want its value in a quoted string, and there are
contexts where you need to write PASSWORD() instead of just PASSWORD",
and all that.)

>  >> to the OP: next time stop with the nonsense, the quine, the
>  >> logs, and just say you want to edit a file in place. pretty
>  >> simple concept but you said everything BUT that.
>
>  CD> Well, uri, I just came down from the mountains, and I've
>  CD> decided to turn over a few new leafs.  Maybe one of them could
>  CD> be with you.  Would you like me to try File::Slurp and see if
>  CD> it can work into my own meaningful projects?
>
>huh??

To expand on that "huh??": your reply there has nothing to do with his
comment, which echoed the comments others made.  (I would have
remarked the same if others hadn't done it first and better.)

To expand on his point there:

As I recall, you posted programs, possibly not runnable, and logs, and
other stuff.  Someone asked you to simply explain what the problem was
in plain English, and in reply (as I recall) you posted other code but
with none of the requested explanation.

I echo what other people have written.  When asking for help, please
start by just explaining what you want in plain English, along the
lines of

"I would like to take a script and remove a hard-coded password from
it so I can show it to other people."

"I am trying to run this code but it outputs

    Dingleberries detected, line 17 of frobotz.pl

and dies.  I am not using the dingleberry operator or Moose::Dingleberry."

"I am trying to connect to a Gopher server, but the connection doesn't
work.  I've connected to the Gopher server using the gopher command,
and the username and password work there."

Or whatever the problem is.

Below that, if you want to post code,
- Please give just enough code to exhibit the problem.
- Please provide runnable code.
- Please explain how to run the code -- what data file(s), what
  command-line arguments, whatever.

The idea is that someone can read the English and know whether they
can likely help, and at least what sorts of things to look for.  Then
they can read the small extract of code much easier that a large block
of code that has irrelevant details.  Then, if they like, they are
able to copy and paste the code onto their own machine to see if they
can reproduce your problem, and they can try editing the code to see
if they can fix it.

-- 
Tim McDaniel, tmcd@panix.com
0
Reply tmcd1 (189) 8/2/2012 4:03:42 PM

>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:

  BM> which you could then read into a hash something like this:

  BM>     my %info;
  BM>     {
  BM>         open my $INFO, "<", ...;
  BM>         while (<$INFO>) {
  BM>             chomp;
  BM>             my ($k, $v) = /^(\w+):\s+(.*)$/ 
  BM>                 or die "Incorrect format for password file: [$_]";
  BM>             $info{$k} = $v;
  BM>         }
  BM>         # no need to close, perl will do this for you when $INFO goes
  BM>         # out of scope
  BM>     }

  BM> (There are certainly more concise ways of doing this--I might use map
  BM> and File::Slurp--but this is simple and straightforward.)

my fave way to slurp and parse that style of config:

use File::Slurp ;
my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;

and that isn't any more complex than your longer loop and uses the same
regex (i changed \s+ to \s*). not even a need for map!

uri

0
Reply uri (200) 8/2/2012 7:35:53 PM

In article <87lihxnl2e.fsf@stemsystems.com>,
Uri Guttman  <uri@stemsystems.com> wrote:
>>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
>
>  BM> which you could then read into a hash something like this:
>
>  BM>     my %info;
>  BM>     {
>  BM>         open my $INFO, "<", ...;
>  BM>         while (<$INFO>) {
>  BM>             chomp;
>  BM>             my ($k, $v) = /^(\w+):\s+(.*)$/ 
>  BM>                 or die "Incorrect format for password file: [$_]";
>  BM>             $info{$k} = $v;
>  BM>         }
>  BM>         # no need to close, perl will do this for you when $INFO goes
>  BM>         # out of scope
>  BM>     }
>
>  BM> (There are certainly more concise ways of doing this--I might use map
>  BM> and File::Slurp--but this is simple and straightforward.)
>
>my fave way to slurp and parse that style of config:
>
>use File::Slurp ;
>my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;
>
>and that isn't any more complex than your longer loop and uses the
>same regex (i changed \s+ to \s*). not even a need for map!

I'm confused.  How does that work?  Isn't the right-hand side of =~
supposed to be a regex and the left-hand side a scalar?

-- 
Tim McDaniel, tmcd@panix.com
0
Reply tmcd1 (189) 8/2/2012 8:08:18 PM

Quoth Uri Guttman <uri@stemsystems.com>:
> >>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
> 
>   BM> which you could then read into a hash something like this:
> 
>   BM>     my %info;
>   BM>     {
>   BM>         open my $INFO, "<", ...;
>   BM>         while (<$INFO>) {
>   BM>             chomp;
>   BM>             my ($k, $v) = /^(\w+):\s+(.*)$/ 
>   BM>                 or die "Incorrect format for password file: [$_]";
>   BM>             $info{$k} = $v;
>   BM>         }
>   BM>         # no need to close, perl will do this for you when $INFO goes
>   BM>         # out of scope
>   BM>     }
> 
>   BM> (There are certainly more concise ways of doing this--I might use map
>   BM> and File::Slurp--but this is simple and straightforward.)
> 
> my fave way to slurp and parse that style of config:
> 
> use File::Slurp ;
> my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;

(Don't you mean read_file(...) =~ /.../g ?)

> and that isn't any more complex than your longer loop and uses the same
> regex (i changed \s+ to \s*). not even a need for map!

I agree. However, I have noticed here in the past that people who are
not familiar with Perl tend to find statements like that which do
several things at once confusing. Once you get used to the idea that, in
Perl, a statement has to be taken apart rather carefully to see what
each piece does, it is no more complex, but I suspect a beginner would
read that line as

    my %conf = OHMYGODWHATDOESTHATDO ($config_file);

:)

Ben

0
Reply ben6057 (867) 8/2/2012 9:04:46 PM

On 08/01/2012 09:17 PM, J�rgen Exner wrote:
> Cal Dershowitz<cal@example.invalid>  wrote:
>> Q1)  How could one read the word $domain or anything with a dollar sign
>> on it in the help file and then declare it at top scope in the script?
>
> Are you looking for a template system?

I tried to make something happen with Html::Template, but I'd say I came 
pretty close to a pratfall there.  Since you asked the question, I've 
googled and read up on it.

Are you /is anyone/ familiar with the badger book?  Now that I'm 
finished with _Learning Perl_ for the second time, I think I need to 
repopulate my reading.  Today, I customized ben's %info hash to be able 
to successfully open an ftp object, so I think I'm King Shit, while this 
might be a very modest pre-requisite for this material.

My basic question is whether you think it's too hard for me to use 
effectively.  It sounds wonderful:  I'd love to have the capability.
-- 
Cal

0
Reply cal819 (188) 8/3/2012 12:51:14 AM

On 08/02/2012 04:47 AM, Rainer Weikusat wrote:
> Ben Morrow<ben@morrow.me.uk>  writes:
>
> [...]
>
>>
>> Why have you made your configuration file format look like Perl? Perl is
>> not a particularly easy language to parse.
>
> Except if the file also needs to be parsed by code not written in Perl,
> the usual 'good reason' for that would be that perl can already parse
> Perl: Configuration files using Perl syntax can be read with do FILE.
>
>

I don't understand.  Can you elaborate?
-- 
Cal
0
Reply cal819 (188) 8/3/2012 12:56:13 AM

On 08/01/2012 11:53 PM, Ben Morrow wrote:
>
> Quoth Cal Dershowitz<cal@example.invalid>:
>>
>> $ perl safe_post4.pl
>> matched
>> my $domain = 'www.bedrock.com';
>> word is www.bedrock.com
>> matched
>> my $username = 'barney';
>> word is barney
>> matched
>> my $password = 'rocks';
>
> I hope this isn't your real password...

Yeah, I figure I have to keep a secret from fred, and since he and I 
work with 'boulders' all day, I decided to be less obvious.
>
>> word is rocks
>> $ cat safe_post4.pl
>> #!/usr/bin/perl -w
>> use strict;
>> use 5.010;
>> use Net::FTP;
>> my ( $domain, $username, $password );
>> my $identity_file = 'ftp_passwords_1.txt';
>> my ( @lines, $word );
>> open( my $gh, '<', $identity_file )
>>     or die("Can't open $identity_file  for writing: $!");
>
> *Reading*. If you use 'autodie' it will get this right for you.

autodie works on file open and closes but not others?
>
>> chomp( @lines =<$gh>  );
>> close $gh;
>>
>> foreach (@lines) {
>>       if (/\$domain/) {
>>           print "matched\n";
>>           print $_ . "\n";
>>           m/'(.*)'/;
>>           $domain = $1;
>>           print "word is $domain\n";
>>       }
>> }
>>
>> foreach (@lines) {
>
> Why are you iterating over the lines multiple times? You can do several
> matches in one loop.

I couldn't figure out how.
>
> Why have you made your configuration file format look like Perl? Perl is
> not a particularly easy language to parse. I would suggest something
> simpler; perhaps
>
>      domain: ftp.foo.com
>      username: cal
>      password: xxxx
>
> which you could then read into a hash something like this:
>
>      my %info;
>      {
>          open my $INFO, "<", ...;
>          while (<$INFO>) {
>              chomp;
>              my ($k, $v) = /^(\w+):\s+(.*)$/
>                  or die "Incorrect format for password file: [$_]";
>              $info{$k} = $v;
>          }
>          # no need to close, perl will do this for you when $INFO goes
>          # out of scope
>      }

This works great, ben, and it's definitely the way forward for my ftp 
projects:
$ perl safe_post5.pl
Net::FTP>>> Net::FTP(2.77)
....
Net::FTP=GLOB(0x970a83c)<<< 226 Transfer complete
ARRAY(0x974c398)$ cat safe_post5.pl
#!/usr/bin/perl -w
use strict;
use 5.010;
use Net::FTP;
use autodie;
my %info;
my $identity_file = 'my_ftp.txt';

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

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

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";

print scalar ( $ftp->ls());
$

I got the syntax for printing a hash from _Perl Cookbook_.  I've got my 
own format for my internet and other identities, but better to ask what 
others do, because I might benefit from their experience and techniques.

Q2)  Why didn't my little print statement at the end give me a scalar 
that I could associate with success?
>
> (There are certainly more concise ways of doing this--I might use map
> and File::Slurp--but this is simple and straightforward.)
>
> You could also use one of the existing config file parsers, perhaps
> YAML::XS or Config::Simple, though for something this trivial it
> probably isn't worth it.

I see that YAML is a section in _Intermediate Perl_.
>
>> Q1)  How could one read the word $domain or anything with a dollar sign
>> on it in the help file and then declare it at top scope in the script?
>
> What is 'the help file'? This file you've put your passwords in?
>
> You can't, at least not without trickery of some sort. The whole point
> of lexical ('my') variables is that they have to be declared where you
> can see them, in the file (and scope) you're looking at. You could
> require a file which set some globals, but that's not a good idea for
> all the usual reasons.

But I can declare them global, given that I have them in a hash already, 
without their dollar signs?
-- 
Cal

0
Reply cal819 (188) 8/3/2012 1:16:15 AM

Quoth Cal Dershowitz <cal@example.invalid>:
> On 08/01/2012 11:53 PM, Ben Morrow wrote:
> > Quoth Cal Dershowitz<cal@example.invalid>:
> >>
> >> open( my $gh, '<', $identity_file )
> >>     or die("Can't open $identity_file  for writing: $!");
> >
> > *Reading*. If you use 'autodie' it will get this right for you.
> 
> autodie works on file open and closes but not others?

Read the documentation. By default it affects all the builtin functions
which indicate failure through their return value (except system, for
technical reasons), but you can ask for something else.

> >> foreach (@lines) {
> >>       if (/\$domain/) {
> >>           print "matched\n";
> >>           print $_ . "\n";
> >>           m/'(.*)'/;
> >>           $domain = $1;
> >>           print "word is $domain\n";
> >>       }
> >> }
> >>
> >> foreach (@lines) {
> >
> > Why are you iterating over the lines multiple times? You can do several
> > matches in one loop.
> 
> I couldn't figure out how.

Take the 'if' statement out of each loop, and put them one after another
in one loop. Obviously if you alter $_ in one 'if' that will carry
through to the next...

What did you try?

> # print "@{[ %info ]}\n";

Data::Dumper is useful for this sort of thing. (Also Data::Dump, from
CPAN, which IMHO produces output which is easier to understand.)

> print scalar ( $ftp->ls());
> $
> 
> I got the syntax for printing a hash from _Perl Cookbook_.  I've got my 
> own format for my internet and other identities, but better to ask what 
> others do, because I might benefit from their experience and techniques.
> 
> Q2)  Why didn't my little print statement at the end give me a scalar 
> that I could associate with success?

What did it give you? Something like 'ARRAY(0xdeadbeef)'? That's what
you get if you stringify an array ref, which is what ->ls returns in
scalar context if it's successful. Again, Data::Dump(er) is useful for
printing out this sort of thing.

> > You can't, at least not without trickery of some sort. The whole point
> > of lexical ('my') variables is that they have to be declared where you
> > can see them, in the file (and scope) you're looking at. You could
> > require a file which set some globals, but that's not a good idea for
> > all the usual reasons.
> 
> But I can declare them global, given that I have them in a hash already, 
> without their dollar signs?

....No? I'm not sure what you mean; can you give an example?

Ben

0
Reply ben6057 (867) 8/3/2012 2:10:12 AM

In article <MM6dnXZvy9YyvIbNnZ2dnUVZ_rqdnZ2d@supernews.com>,
Cal Dershowitz  <cal@example.invalid> wrote:
>On 08/02/2012 04:47 AM, Rainer Weikusat wrote:
>> Ben Morrow<ben@morrow.me.uk>  writes:
>>
>> [...]
>>
>>>
>>> Why have you made your configuration file format look like Perl?
>>> Perl is not a particularly easy language to parse.
>>
>> Except if the file also needs to be parsed by code not written in
>> Perl, the usual 'good reason' for that would be that perl can
>> already parse Perl: Configuration files using Perl syntax can be
>> read with do FILE.
>
>I don't understand.  Can you elaborate?

English prepositions and conjunctions can be tricky.
s/Except if/Unless/.  That is,

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,
            ...
        },
    }

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.

-- 
Tim McDaniel, tmcd@panix.com
0
Reply tmcd1 (189) 8/3/2012 2:20:48 AM

Quoth tmcd@panix.com:
> In article <MM6dnXZvy9YyvIbNnZ2dnUVZ_rqdnZ2d@supernews.com>,
> Cal Dershowitz  <cal@example.invalid> wrote:
> >On 08/02/2012 04:47 AM, Rainer Weikusat wrote:
> >>
> >> Except if the file also needs to be parsed by code not written in
> >> Perl, the usual 'good reason' for that would be that perl can
> >> already parse Perl: Configuration files using Perl syntax can be
> >> read with do FILE.
> >
> >I don't understand.  Can you elaborate?
> 
> English prepositions and conjunctions can be tricky.
> s/Except if/Unless/.  That is,
> 
> 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
          ^^^^
          perl

Since we're being picky... :)

Ben

0
Reply ben6057 (867) 8/3/2012 5:13:11 AM

>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:

  BM> Quoth Uri Guttman <uri@stemsystems.com>:
  >> >>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
  >> 
  BM> which you could then read into a hash something like this:
  >> 
  BM> my %info;
  BM> {
  BM> open my $INFO, "<", ...;
  BM> while (<$INFO>) {
  BM> chomp;
  BM> my ($k, $v) = /^(\w+):\s+(.*)$/ 
  BM> or die "Incorrect format for password file: [$_]";
  BM> $info{$k} = $v;
  BM> }
  BM> # no need to close, perl will do this for you when $INFO goes
  BM> # out of scope
  BM> }
  >> 
  BM> (There are certainly more concise ways of doing this--I might use map
  BM> and File::Slurp--but this is simple and straightforward.)
  >> 
  >> my fave way to slurp and parse that style of config:
  >> 
  >> use File::Slurp ;
  >> my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;

  BM> (Don't you mean read_file(...) =~ /.../g ?)

yes. brain fart. i have written that on the fly on blackboards
correctly. so i screwed up on usenet. :)

  >> and that isn't any more complex than your longer loop and uses the same
  >> regex (i changed \s+ to \s*). not even a need for map!

  BM> I agree. However, I have noticed here in the past that people who are
  BM> not familiar with Perl tend to find statements like that which do
  BM> several things at once confusing. Once you get used to the idea that, in
  BM> Perl, a statement has to be taken apart rather carefully to see what
  BM> each piece does, it is no more complex, but I suspect a beginner would
  BM> read that line as

  BM>     my %conf = OHMYGODWHATDOESTHATDO ($config_file);

yeah but i deal with all sorts of perl types. i support some who would
not get that line but like file::slurp in general. others will yell to
use a config file module (which one??). my point is to show how nicely
perl's parts work together. parsing keys/values and assigning directly
to a hash is just beautiful. i just made the string being parsed the
output of read_file().

uri
0
Reply uri (200) 8/3/2012 5:23:00 AM

>>>>> "TM" == Tim McDaniel <tmcd@panix.com> writes:

  TM> In article <87lihxnl2e.fsf@stemsystems.com>,
  TM> Uri Guttman  <uri@stemsystems.com> wrote:
  >>>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
  >> 
  BM> which you could then read into a hash something like this:
  >> 
  BM> my %info;
  BM> {
  BM> open my $INFO, "<", ...;
  BM> while (<$INFO>) {
  BM> chomp;
  BM> my ($k, $v) = /^(\w+):\s+(.*)$/ 
  BM> or die "Incorrect format for password file: [$_]";
  BM> $info{$k} = $v;
  BM> }
  BM> # no need to close, perl will do this for you when $INFO goes
  BM> # out of scope
  BM> }
  >> 
  BM> (There are certainly more concise ways of doing this--I might use map
  BM> and File::Slurp--but this is simple and straightforward.)
  >> 
  >> my fave way to slurp and parse that style of config:
  >> 
  >> use File::Slurp ;
  >> my %conf = /^(\w+):\s*(.*)$/ =~ read_file( $config_file ) ;
  >> 
  >> and that isn't any more complex than your longer loop and uses the
  >> same regex (i changed \s+ to \s*). not even a need for map!

  TM> I'm confused.  How does that work?  Isn't the right-hand side of =~
  TM> supposed to be a regex and the left-hand side a scalar?

yep. see my reply to ben. 

uri
0
Reply uri (200) 8/3/2012 5:23:44 AM

tmcd@panix.com (Tim McDaniel) writes:

[...]

> English prepositions and conjunctions can be tricky.
> s/Except if/Unless/.  That is,

That's a Germanism. My bad. The German equivalent of 'unless' is
'ausser wenn' and the/a literal translation of that is 'except if'
(Possibly, 'except when'. So far, I haven't managed to figure out the
precise differences of if, whether and when in conditional clause :-).

OTOH, this has offered the Wilbur(?) guy a nice opportunity for some
additional rants. After all, my English is decidedly homegrown and
this comes accross as 'more or less retarded' to a native speaker who
doesn't really consider the circumstances (can't even talk =>
imbecile).

0
Reply rweikusat (2679) 8/3/2012 12:23:05 PM

On 08/03/2012 06:23 AM, Rainer Weikusat wrote:
> tmcd@panix.com (Tim McDaniel) writes:
>
> [...]
>
>> English prepositions and conjunctions can be tricky.
>> s/Except if/Unless/.  That is,
>
> That's a Germanism. My bad. The German equivalent of 'unless' is
> 'ausser wenn' and the/a literal translation of that is 'except if'
> (Possibly, 'except when'. So far, I haven't managed to figure out the
> precise differences of if, whether and when in conditional clause :-).

That part makes perfect sense in particular when one considers that 
"unless" is the logical negation of the if clause, that which is 
"outside of it.'

Did you perchance mean to include a second colon here:

Perl: Configuration
> 
> OTOH, this has offered the Wilbur(?) guy a nice opportunity for some
> additional rants. After all, my English is decidedly homegrown and
> this comes accross as 'more or less retarded' to a native speaker who
> doesn't really consider the circumstances (can't even talk =>
> imbecile).
>

I don't know wilbur, but I do know we grow our morons big around here, 
and give them megaphones if they can add parochial, intolerant, 
strident, revisionist and delusional to it.

Cheers,
-- 
Cal
0
Reply cal819 (188) 8/4/2012 3:50:10 AM

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
0
Reply cal819 (188) 8/4/2012 5:54:59 AM

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]>

0
Reply cal819 (188) 8/4/2012 7:02:11 AM

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

0
Reply rweikusat (2679) 8/4/2012 5:09:14 PM

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
0
Reply tmcd1 (189) 8/4/2012 6:15:33 PM

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

0
Reply cal819 (188) 8/4/2012 11:22:26 PM

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
0
Reply cal819 (188) 8/5/2012 12:14:39 AM

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.

0
Reply NoSpamPleaseButThisIsValid3 (39) 8/5/2012 8:39:34 AM

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
0
Reply cal819 (188) 8/5/2012 10:16:23 PM

In article <501e3146$0$6558$9b4e6d93@newsspool4.arcor-online.net>,
Wolf Behrenhoff  <nospampleasebutthisisvalid3@gmx.net> 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"

    $ perldoc -q "What is the difference between a list and an array"
    No documentation for perl FAQ keyword `What is the difference
    between a list and an array' found

    $ perl -v

    This is perl 5, version 14, subversion 2 (v5.14.2) built for
    i386-netbsd

http://search.cpan.org/~flora/perl-5.14.2/pod/perlfaq4.pod#What_is_the_difference_between_a_list_and_an_array?
works.

While it's a true statement and people should know that there's a
difference and sometimes it matters, I used Perl for many years
thinking that lists and arrays were the same and didn't get bit by it.
Even just rereading that FAQ entry, I may be still a bit fuzzy.

I think the main thing to remember from that FAQ is that comma can
mean two things

    Binary "," is the comma operator.  In scalar context it evaluates
    its left argument, throws that value away, then evaluates its
    right argument and returns that value.  This is just like C's
    comma operator.

    In list context, it's just the list argument separator, and
    inserts both its arguments into the list.  These arguments are
    also evaluated from left to right.
                         
So be careful of comma-separated things: know what context they're
being used in.

-- 
Tim McDaniel, tmcd@panix.com
0
Reply tmcd1 (189) 8/7/2012 4:01:06 PM

Quoth tmcd@panix.com:
> 
> While it's a true statement and people should know that there's a
> difference and sometimes it matters, I used Perl for many years
> thinking that lists and arrays were the same and didn't get bit by it.
> Even just rereading that FAQ entry, I may be still a bit fuzzy.

It's very simple: a list is a value, like 3 or "hello world". An array
is a variable, like $x. Just as the variable $x can hold the value 3, so
the variable @x can hold the value (1, 2, 3).

Ben

0
Reply ben6057 (867) 8/12/2012 12:22:04 PM

>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:

  BM> Quoth tmcd@panix.com:
  >> 
  >> While it's a true statement and people should know that there's a
  >> difference and sometimes it matters, I used Perl for many years
  >> thinking that lists and arrays were the same and didn't get bit by it.
  >> Even just rereading that FAQ entry, I may be still a bit fuzzy.

  BM> It's very simple: a list is a value, like 3 or "hello world". An array
  BM> is a variable, like $x. Just as the variable $x can hold the value 3, so
  BM> the variable @x can hold the value (1, 2, 3).

i have more i teach about that. lists are on the stack, are only in
expressions and can't live beyond them. arrays are allocated from the
heap and can live between statements. you can assign lists to/from
arrays and both can be sliced.

uri
0
Reply uri (200) 8/12/2012 4:46:17 PM

>>>>> Ben Morrow <ben@morrow.me.uk> writes:
>>>>> Quoth tmcd@panix.com:

 >> While it's a true statement and people should know that there's a
 >> difference and sometimes it matters, I used Perl for many years
 >> thinking that lists and arrays were the same and didn't get bit by
 >> it.  Even just rereading that FAQ entry, I may be still a bit fuzzy.

 > It's very simple: a list is a value, like 3 or "hello world". An
 > array is a variable, like $x.  Just as the variable $x can hold the
 > value 3, so the variable @x can hold the value (1, 2, 3).

	That's helpful, thanks.  Though it still doesn't seem to tell
	whether there's an array in [1, 2, 3] or not.

-- 
FSF associate member #7257	http://sf-day.org/
0
Reply oneingray (254) 8/12/2012 4:57:57 PM

Quoth Ivan Shmakov <oneingray@gmail.com>:
> >>>>> Ben Morrow <ben@morrow.me.uk> writes:
> >>>>> Quoth tmcd@panix.com:
> 
>  >> While it's a true statement and people should know that there's a
>  >> difference and sometimes it matters, I used Perl for many years
>  >> thinking that lists and arrays were the same and didn't get bit by
>  >> it.  Even just rereading that FAQ entry, I may be still a bit fuzzy.
> 
>  > It's very simple: a list is a value, like 3 or "hello world". An
>  > array is a variable, like $x.  Just as the variable $x can hold the
>  > value 3, so the variable @x can hold the value (1, 2, 3).
> 
> 	That's helpful, thanks.  Though it still doesn't seem to tell
> 	whether there's an array in [1, 2, 3] or not.

That is the confusing case, yes :). An expression like

    my $x = [1, 2, 3];

is equivalent to something like

    sub build_array {
        my @array = @_;
        return \@array;
    }

    my $x = build_array(1, 2, 3);

so there is an array there, it just hasn't got a name and is only
accessible through the reference.

Ben

0
Reply ben6057 (867) 8/12/2012 6:15:17 PM

Quoth Uri Guttman <uri@stemsystems.com>:
> >>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
> 
>   BM> Quoth tmcd@panix.com:
>   >> 
>   >> While it's a true statement and people should know that there's a
>   >> difference and sometimes it matters, I used Perl for many years
>   >> thinking that lists and arrays were the same and didn't get bit by it.
>   >> Even just rereading that FAQ entry, I may be still a bit fuzzy.
> 
>   BM> It's very simple: a list is a value, like 3 or "hello world". An array
>   BM> is a variable, like $x. Just as the variable $x can hold the value 3, so
>   BM> the variable @x can hold the value (1, 2, 3).
> 
> i have more i teach about that. lists are on the stack, are only in
> expressions and can't live beyond them. arrays are allocated from the
> heap and can live between statements. 

That's just the variable/value distinction. In a statement like

    $x + 4;

both the 4 and the result of the addition are temporaries on the stack,
and will evaporate at the end of the statement.

> you can assign lists to/from arrays and both can be sliced.

Slicing a list is different from slicing an array, in that a list slice
consumes the list and the values sliced away are no longer accesible. In
this respect it's more like a variant of 'grep' than like an array
slice.

Ben

0
Reply ben6057 (867) 8/12/2012 6:22:18 PM

>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:

  BM> Quoth Uri Guttman <uri@stemsystems.com>:

  BM> It's very simple: a list is a value, like 3 or "hello world". An array
  BM> is a variable, like $x. Just as the variable $x can hold the value 3, so
  BM> the variable @x can hold the value (1, 2, 3).
  >> 
  >> i have more i teach about that. lists are on the stack, are only in
  >> expressions and can't live beyond them. arrays are allocated from the
  >> heap and can live between statements. 

  BM> That's just the variable/value distinction. In a statement like

  BM>     $x + 4;

  BM> both the 4 and the result of the addition are temporaries on the stack,
  BM> and will evaporate at the end of the statement.

sure but it explains it better those confused about values vs
vars. showing the lifetime and where the live give a better picture. i
didn't say you were wrong but i add more when i teach that issue.

  >> you can assign lists to/from arrays and both can be sliced.

  BM> Slicing a list is different from slicing an array, in that a list slice
  BM> consumes the list and the values sliced away are no longer accesible. In
  BM> this respect it's more like a variant of 'grep' than like an array
  BM> slice.

i disagree. you can reorder sliced elements and even duplicate
them. grep can do neither as it can only return a subset list in the
same original order.

slicing is the same effective op on both lists and arrays. i dunno the
actual op tree.

uri
0
Reply uri (200) 8/13/2012 6:02:18 AM

48 Replies
210 Views

(page loaded in 1.646 seconds)

Similiar Articles:













7/28/2012 3:01:39 PM


Reply: