[33033] in Perl-Users-Digest
Perl-Users Digest, Issue: 4309 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Nov 15 21:09:19 2014
Date: Sat, 15 Nov 2014 18:09:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 15 Nov 2014 Volume: 11 Number: 4309
Today's topics:
Not sure why array does not contain anything when print <mikaelpetterson@hotmail.com>
Re: Not sure why array does not contain anything when p <rweikusat@mobileactivedefense.com>
Re: Not sure why array does not contain anything when p <gamo@telecable.es>
Re: Not sure why array does not contain anything when p <gravitalsun@hotmail.foo>
Re: Not sure why array does not contain anything when p <mikaelpetterson@hotmail.com>
Re: Not sure why array does not contain anything when p (Seymour J.)
Re: Not sure why array does not contain anything when p <gamo@telecable.es>
Re: Not sure why array does not contain anything when p <rweikusat@mobileactivedefense.com>
Re: Not sure why array does not contain anything when p <gravitalsun@hotmail.foo>
Re: Not sure why array does not contain anything when p <rweikusat@mobileactivedefense.com>
Re: quote <gravitalsun@hotmail.foo>
Re: quote <gamo@telecable.es>
Re: quote <justin.1410@purestblue.com>
ssh-keygen -t rsa in perl <mikaelpetterson@hotmail.com>
Re: ssh-keygen -t rsa in perl <news@todbe.com>
Re: ssh-keygen -t rsa in perl <rweikusat@mobileactivedefense.com>
Re: ssh-keygen -t rsa in perl <news@todbe.com>
Re: ssh-keygen -t rsa in perl <gravitalsun@hotmail.foo>
Re: ssh-keygen -t rsa in perl <gamo@telecable.es>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 12 Nov 2014 07:28:37 -0800 (PST)
From: mike <mikaelpetterson@hotmail.com>
Subject: Not sure why array does not contain anything when printing
Message-Id: <6d944c0b-ded4-49c1-a3f4-bbad16799db0@googlegroups.com>
Hi,
I read lines from a file using the following sub.
sub read_strings_from_file {
print "read strings from file \n";
my $file = shift;
if( -f $file){
open (FILE, $file) or die "Couldn't open $file: $!";
my @strings = ();
while (my $line = <FILE>){
chomp $line;
next if $line =~ /^#/;
push (@strings, "$line\n");
}
close (FILE);
}
print "@strings";
return @strings; # return array
}
If I add a print after this line:
next if $line =~ /^#/;
I can see that it contains the lines that I have in a file.
These are the lines in $file:
ssh://userid@companydomain.se:1223/my-project-string
But when I try to print the content of the array it is empty.
Any ideas?
//mike
------------------------------
Date: Wed, 12 Nov 2014 15:43:04 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Not sure why array does not contain anything when printing
Message-Id: <87vbmk4e7r.fsf@doppelsaurus.mobileactivedefense.com>
mike <mikaelpetterson@hotmail.com> writes:
> I read lines from a file using the following sub.
>
> sub read_strings_from_file {
> print "read strings from file \n";
> my $file = shift;
> if( -f $file){
> open (FILE, $file) or die "Couldn't open $file: $!";
> my @strings = ();
> while (my $line = <FILE>){
> chomp $line;
> next if $line =~ /^#/;
> push (@strings, "$line\n");
> }
> close (FILE);
> }
> print "@strings";
> return @strings; # return array
> }
>
[...]
>when I try to print the content of the array it is empty.
Saving the subroutine in a file named w.pl and running the Perl compiler
on that with 'helpful checks' enabled yields:
[rw@doppelsaurus]/tmp#perl -cw -Mstrict w.pl
Possible unintended interpolation of @strings in string at w.pl line 14.
Global symbol "@strings" requires explicit package name at w.pl line 14.
Global symbol "@strings" requires explicit package name at w.pl line 15.
w.pl had compilation errors.
The while-loop accesses the lexical variable @strings declared in the
containing scope ('if (-f $file) { ... }') but that doesn't exist for
the print and return statements outside of this scope: They work with a
package-global @strings.
IMNEHO, micro-manageing the lifetime of lexical variables is messy and
error-prone (see above) and unless there's a specific reason for the
contrary, lexicals declared in subroutines should 'belong' to the
subroutine and not to inner parts of it. In case this become unwieldy,
splitting the subroutine would be the sensible course of action.
------------------------------
Date: Thu, 13 Nov 2014 00:12:06 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Not sure why array does not contain anything when printing
Message-Id: <m40pg6$omc$1@speranza.aioe.org>
El 12/11/14 a las 16:28, mike escribió:
> Hi,
>
> I read lines from a file using the following sub.
>
> sub read_strings_from_file {
> print "read strings from file \n";
> my $file = shift;
here you must put the
my @strings;
> if( -f $file){
if (-r $file && -f $file){
> open (FILE, "<", $file) or die "Couldn't open $file: $!";
note here the explicit input mode "<"
> my @strings = ();
to delete
> while (my $line = <FILE>){
> chomp $line;
why?
> next if $line =~ /^#/;
> push (@strings, "$line\n");
why you chomp before and after put a new line?
> }
> close (FILE);
> }
> print "@strings";
print "@strings" if @strings > 0;
> return @strings; # return array
return a list or an empty list, if the file has size of 0 chars.
> }
>
> Any ideas?
>
> //mike
>
--
http://www.telecable.es/personales/gamo/
------------------------------
Date: Thu, 13 Nov 2014 01:50:03 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Not sure why array does not contain anything when printing
Message-Id: <m40rnb$1nq5$1@news.ntua.gr>
>
> But when I try to print the content of the array it is empty.
>
> Any ideas?
>
> //mike
>
# try this for fun !
use strict;
print read_strings_from_file(\*DATA); # or:
#print read_strings_from_file('/tmp/file.txt');
sub read_strings_from_file {
my $fh;
my @lines;
'GLOB' eq ref $_[0] ? $fh=$_[0] : open $fh, $_[0] or return;
@lines = grep ! /^\s*(#.*)?$/ , <$fh>; close $fh;
@lines
}
__DATA__
line01
line02
#line03
line04
------------------------------
Date: Wed, 12 Nov 2014 22:34:08 -0800 (PST)
From: mike <mikaelpetterson@hotmail.com>
Subject: Re: Not sure why array does not contain anything when printing
Message-Id: <f5286c70-2831-45ee-93e9-85238d4c48e3@googlegroups.com>
Den onsdagen den 12:e november 2014 kl. 16:43:10 UTC+1 skrev Rainer Weikusat:
> mike <mikaelpetterson@hotmail.com> writes:
> > I read lines from a file using the following sub.
> >
> > sub read_strings_from_file {
> > print "read strings from file \n";
> > my $file = shift;
> > if( -f $file){
> > open (FILE, $file) or die "Couldn't open $file: $!";
> > my @strings = ();
> > while (my $line = <FILE>){
> > chomp $line;
> > next if $line =~ /^#/;
> > push (@strings, "$line\n");
> > }
> > close (FILE);
> > }
> > print "@strings";
> > return @strings; # return array
> > }
> >
>
> [...]
>
> >when I try to print the content of the array it is empty.
>
> Saving the subroutine in a file named w.pl and running the Perl compiler
> on that with 'helpful checks' enabled yields:
>
> [rw@doppelsaurus]/tmp#perl -cw -Mstrict w.pl
> Possible unintended interpolation of @strings in string at w.pl line 14.
> Global symbol "@strings" requires explicit package name at w.pl line 14.
> Global symbol "@strings" requires explicit package name at w.pl line 15.
> w.pl had compilation errors.
>
> The while-loop accesses the lexical variable @strings declared in the
> containing scope ('if (-f $file) { ... }') but that doesn't exist for
> the print and return statements outside of this scope: They work with a
> package-global @strings.
>
> IMNEHO, micro-manageing the lifetime of lexical variables is messy and
> error-prone (see above) and unless there's a specific reason for the
> contrary, lexicals declared in subroutines should 'belong' to the
> subroutine and not to inner parts of it. In case this become unwieldy,
> splitting the subroutine would be the sensible course of action.
Thanks. Yes I missed scoping.
------------------------------
Date: Thu, 13 Nov 2014 09:46:16 -0500
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: Not sure why array does not contain anything when printing
Message-Id: <5464c438$18$fuzhry+tra$mr2ice@news.patriot.net>
In <87vbmk4e7r.fsf@doppelsaurus.mobileactivedefense.com>, on
11/12/2014
at 03:43 PM, Rainer Weikusat <rweikusat@mobileactivedefense.com>
said:
>IMNEHO, micro-manageing the lifetime of lexical variables is messy
>and error-prone (see above) and unless there's a specific reason
>for the contrary, lexicals declared in subroutines should 'belong'
>to the subroutine and not to inner parts of it. In case this
>become unwieldy,
That would break a huge amount of code, and would be highly unnatural
for those used to block structured languages. But better error
messages might help.
--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>
Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to spamtrap@library.lspace.org
------------------------------
Date: Thu, 13 Nov 2014 18:01:46 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Not sure why array does not contain anything when printing
Message-Id: <m42o5n$66s$1@speranza.aioe.org>
El 13/11/14 a las 00:12, gamo escribió:
> El 12/11/14 a las 16:28, mike escribió:
>> Hi,
>>
>> I read lines from a file using the following sub.
>>
>> sub read_strings_from_file {
>> print "read strings from file \n";
>> my $file = shift;
> here you must put the
> my @strings;
>> if( -f $file){
> if (-r $file && -f $file){
>> open (FILE, "<", $file) or die "Couldn't open $file: $!";
> note here the explicit input mode "<"
>
>> my @strings = ();
> to delete
>
>
>> while (my $line = <FILE>){
>> chomp $line;
> why?
>
>> next if $line =~ /^#/;
>> push (@strings, "$line\n");
> why you chomp before and after put a new line?
>> }
>> close (FILE);
>> }
>> print "@strings";
> print "@strings" if @strings > 0;
>> return @strings; # return array
> return a list or an empty list, if the file has size of 0 chars.
>
You could check also with -s $file and take an appropiate course
of response if the $file is empty.
>> }
>>
>
>> Any ideas?
>>
>> //mike
>>
>
>
--
http://www.telecable.es/personales/gamo/
------------------------------
Date: Thu, 13 Nov 2014 18:56:34 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Not sure why array does not contain anything when printing
Message-Id: <87tx230w0t.fsf@doppelsaurus.mobileactivedefense.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
[....]
> sub read_strings_from_file {
> my $fh;
> my @lines;
> 'GLOB' eq ref $_[0] ? $fh=$_[0] : open $fh, $_[0] or return;
> @lines = grep ! /^\s*(#.*)?$/ , <$fh>; close $fh;
> @lines
> }
This is actually buggy because if you use it in list context and the
open fails, the caller ends up with a list containing a single undef
instead of an empty list.
sub read_strings_from_file {
local @ARGV = ref($_[0]) ? "<&".${$_[0]} : $_[0];
grep ! /^\s*(#.*)?$/ , <>;
}
NB: This is probably buggy in some other way. It's included here for
amusement.
------------------------------
Date: Fri, 14 Nov 2014 00:09:05 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: Not sure why array does not contain anything when printing
Message-Id: <m43a63$2rb2$1@news.ntua.gr>
On 13/11/2014 20:56, Rainer Weikusat wrote:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>
> [....]
>
>> sub read_strings_from_file {
>> my $fh;
>> my @lines;
>> 'GLOB' eq ref $_[0] ? $fh=$_[0] : open $fh, $_[0] or return;
>> @lines = grep ! /^\s*(#.*)?$/ , <$fh>; close $fh;
>> @lines
>> }
>
> This is actually buggy because if you use it in list context and the
> open fails, the caller ends up with a list containing a single undef
> instead of an empty list.
>
> sub read_strings_from_file {
> local @ARGV = ref($_[0]) ? "<&".${$_[0]} : $_[0];
> grep ! /^\s*(#.*)?$/ , <>;
> }
>
> NB: This is probably buggy in some other way. It's included here for
> amusement.
>
technical speaking it returns an empty list not undef.
I like your "in place" read with the "<&".${$_[0]} cool !
------------------------------
Date: Thu, 13 Nov 2014 22:32:30 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Not sure why array does not contain anything when printing
Message-Id: <87d28q20ld.fsf@doppelsaurus.mobileactivedefense.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
> On 13/11/2014 20:56, Rainer Weikusat wrote:
>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>
>> [....]
>>
>>> sub read_strings_from_file {
>>> my $fh;
>>> my @lines;
>>> 'GLOB' eq ref $_[0] ? $fh=$_[0] : open $fh, $_[0] or return;
[...]
>> This is actually buggy because if you use it in list context and the
>> open fails, the caller ends up with a list containing a single undef
>> instead of an empty list.
[...]
> technical speaking it returns an empty list not undef.
You're right and I was wrong.
------------------------------
Date: Wed, 12 Nov 2014 16:36:27 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: quote
Message-Id: <m3vr8d$2cr5$1@news.ntua.gr>
On 12/11/2014 16:08, George Mpouras wrote:
> I want to quote only the " e.g.
>
> 124"456# --> 124\"456#
> 124\"456# --> 124\\\"456#
> 124\\"456# --> 124\\\"456#
> 124\\\"456# --> 124\\\\\"456#
>
this looks good
while(<DATA>) {
chomp $_;
my $quoted = $_;
$quoted =~s/(")/ quotemeta $^N /eg;
print "$_ --> $quoted\n";
}
__DATA__
124"456#
124\"456#
124\\"456#
124\\\"456#
------------------------------
Date: Wed, 12 Nov 2014 15:57:47 +0100
From: gamo <gamo@telecable.es>
Subject: Re: quote
Message-Id: <m3vsh8$7ei$1@speranza.aioe.org>
El 12/11/14 a las 15:08, George Mpouras escribió:
> I want to quote only the " e.g.
>
> 124"456# --> 124\"456#
> 124\"456# --> 124\\\"456#
> 124\\"456# --> 124\\\"456#
> 124\\\"456# --> 124\\\\\"456#
>
perl -E 'say "1\"2"; '
--
http://www.telecable.es/personales/gamo/
------------------------------
Date: Wed, 12 Nov 2014 16:02:17 +0000
From: Justin C <justin.1410@purestblue.com>
Subject: Re: quote
Message-Id: <9tccjb-5r5.ln1@zem.masonsmusic.co.uk>
On 2014-11-12, George Mpouras <gravitalsun@hotmail.foo> wrote:
> On 12/11/2014 16:08, George Mpouras wrote:
>> I want to quote only the " e.g.
>>
>> 124"456# --> 124\"456#
>> 124\"456# --> 124\\\"456#
>> 124\\"456# --> 124\\\"456#
>> 124\\\"456# --> 124\\\\\"456#
>>
>
>
> this looks good
No it doesn't it's hideous. It might do what you want but it's
very ugly code;
>
>
> while(<DATA>) {
> chomp $_;
Don't "chomp $_", just "chomp"
$_ is the default variable it doesn't need naming.
Works with print/say, split and matching, and $deity knows what
else. Here are a couple of examples:
while (<DATA>) { # set the default var to a line from __DATA__
chomp; # remove any trailing new line
if (/ish/) { # match default var against string 'ish'
say; # print the default var with a new line
}
}
__DATA__
English
French
Spanish
German
also:
while (<DATA>) {
chomp;
my @letters = split//;
say for (@letters)
}
So, in your case:
while (<DATA>) {
chomp;
my $quoted = $_;
.
.
}
Though for your purpose I'd go with (preferring named vars over
the default one to avoid something over-writing $_ before I use
again):
while (my $line = <DATA>) {
chomp $line;
my $quoted = $line;
.
.
Justin.
--
Justin C, by the sea.
------------------------------
Date: Fri, 14 Nov 2014 00:00:09 -0800 (PST)
From: mike <mikaelpetterson@hotmail.com>
Subject: ssh-keygen -t rsa in perl
Message-Id: <7c70bde7-5d4f-4065-949c-918f32872a85@googlegroups.com>
Hi,
Is there anything similar in perl? Is there a perl module so that I can generate a public key?
//mike
------------------------------
Date: Fri, 14 Nov 2014 03:18:10 -0800
From: "$Bill" <news@todbe.com>
Subject: Re: ssh-keygen -t rsa in perl
Message-Id: <m44odh$qth$1@dont-email.me>
On 11/14/2014 00:00, mike wrote:
> Hi,
>
>
> Is there anything similar in perl? Is there a perl module so that I can generate a public key?
I don't know what that does specifically, but maybe try:
use Crypt::RSA;
my $key = new Crypt::RSA or die "New Crypt::RSA: $! ($^E)";
my ($pub, $pri) = $key->keygen(Size => 1024, Verbosity => 0,
Identity => $RSA_ID, Password => $RSA_Password, Filename => $keyfile) or
die "keygen: " . $key->errstr();
------------------------------
Date: Fri, 14 Nov 2014 15:55:18 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: ssh-keygen -t rsa in perl
Message-Id: <87sihlsro9.fsf@doppelsaurus.mobileactivedefense.com>
"$Bill" <news@todbe.com> writes:
> On 11/14/2014 00:00, mike wrote:
>> Hi,
>>
>>
>> Is there anything similar in perl? Is there a perl module so that I can generate a public key?
>
> I don't know what that does specifically,
It creates a RSA key pair which can be used for ssh authentication.
------------------------------
Date: Fri, 14 Nov 2014 18:04:53 -0800
From: "$Bill" <news@todbe.com>
Subject: Re: ssh-keygen -t rsa in perl
Message-Id: <m46cc4$nod$1@dont-email.me>
On 11/14/2014 07:55, Rainer Weikusat wrote:
> "$Bill" <news@todbe.com> writes:
>> On 11/14/2014 00:00, mike wrote:
>>> Hi,
>>>
>>>
>>> Is there anything similar in perl? Is there a perl module so that I can generate a public key?
>>
>> I don't know what that does specifically,
>
> It creates a RSA key pair which can be used for ssh authentication.
Thanks - that would have been my 1st guess - my example should handle that.
------------------------------
Date: Sat, 15 Nov 2014 12:23:54 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: ssh-keygen -t rsa in perl
Message-Id: <m479jr$1ss3$1@news.ntua.gr>
On 14/11/2014 17:55, Rainer Weikusat wrote:
> "$Bill" <news@todbe.com> writes:
>> On 11/14/2014 00:00, mike wrote:
>>> Hi,
>>>
>>>
>>> Is there anything similar in perl? Is there a perl module so that I can generate a public key?
>>
>> I don't know what that does specifically,
>
> It creates a RSA key pair which can be used for ssh authentication.
>
what about the more "modern" Crypt::DSA ?
------------------------------
Date: Sat, 15 Nov 2014 11:36:40 +0100
From: gamo <gamo@telecable.es>
Subject: Re: ssh-keygen -t rsa in perl
Message-Id: <m47abm$o00$1@speranza.aioe.org>
El 14/11/14 a las 16:55, Rainer Weikusat escribió:
> "$Bill" <news@todbe.com> writes:
>> On 11/14/2014 00:00, mike wrote:
>>> Hi,
>>>
>>>
>>> Is there anything similar in perl? Is there a perl module so that I can generate a public key?
>>
>> I don't know what that does specifically,
>
> It creates a RSA key pair which can be used for ssh authentication.
>
Yes, and that solve part of the problem. He needs to know more
about how that keys are stored (by the source of ssh-keygen) to
complete the hack. At first saw, the keys are base64 and can
contain anything with the key and type of generator.
--
http://www.telecable.es/personales/gamo/
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 4309
***************************************