[31103] in Perl-Users-Digest
Perl-Users Digest, Issue: 2348 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Apr 17 21:09:46 2009
Date: Fri, 17 Apr 2009 18:09:14 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 17 Apr 2009 Volume: 11 Number: 2348
Today's topics:
Re: Capture only first match in regular expression sln@netherlands.com
I'm looking for a Perl Book... I think. <someone@somewhere.nb.ca>
Re: I'm looking for a Perl Book... I think. <nat.k@gm.ml>
Re: I'm looking for a Perl Book... I think. <maustin@firstdbasource.com>
Re: perls popularity sln@netherlands.com
Re: perls popularity <nat.k@gm.ml>
Re: perls popularity sln@netherlands.com
Re: perls popularity <nat.k@gm.ml>
Reading Text File Encoding and converting to Perls inte sln@netherlands.com
Re: Reading Text File Encoding and converting to Perls sln@netherlands.com
Re: Reading Text File Encoding and converting to Perls <unclebob@tnglwood.demon.co.uk>
Re: Reading Text File Encoding and converting to Perls <ben@morrow.me.uk>
Re: Replacing text containing parenthesis <someone@example.com>
Re: Replacing text containing parenthesis <ben@morrow.me.uk>
Re: Replacing text containing parenthesis sln@netherlands.com
Re: Specifyig username and password in PERL <maustin@firstdbasource.com>
Re: What does `my' do?! sln@netherlands.com
Re: What does `my' do?! sln@netherlands.com
Re: What does `my' do?! sln@netherlands.com
Re: What does `my' do?! <xhoster@gmail.com>
Re: What does `my' do?! <ben@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 17 Apr 2009 13:10:30 -0700
From: sln@netherlands.com
Subject: Re: Capture only first match in regular expression
Message-Id: <8rnhu492q3kpr334miss6bneo7iubb0d5g@4ax.com>
On Fri, 17 Apr 2009 15:51:58 +0200, Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
>Peter Tuente wrote:
>> the default behaviour of regular expression terms is to be "greedy", so to
>> suppress this behaviour to become "not greedy" you have to apply a single
>> question mark "?" right after the desired expression(s). Sounds some kind of
>> complex, but I hope you get me ;-)
>>
>> In your case the following should be sufficient:
>>
>> # old: if($content =~ /.*(<a.*<\/a>).*/i){
>> $anchorContent = $1;
>>
>> # new:
>> if($content =~ /.*?(<a.*?<\/a>).*/i){
>------------------------^^^------------^^
>
>Your suggestion probably works. I just don't understand the point with
>
> .*? at the beginning (of any m// PATTERN)
>
>and
>
> .* at the end (of any m// PATTERN)
Your right, those aren't necessary ^^^.
At the risk of being acused of trying to inject any well-formedness
superficial checks into the regex, it could also be written as:
/(<a\s+.*?<\/a\s*>)/is
-sln
------------------------------
Date: Fri, 17 Apr 2009 20:22:02 -0300
From: "Guy" <someone@somewhere.nb.ca>
Subject: I'm looking for a Perl Book... I think.
Message-Id: <49e90f18$0$5487$9a566e8b@news.aliant.net>
Ok, I'm creating my own website on genealogy.
I'd like visitors to be able to search through my site, search for names
etc...
I'm alright with HTML and I've got enough experience with Javascript.
But I need to do some Server-Side programming.
The only experience I have here is with Perl, but many years ago.
I'm not sure if there are better ways but I like Perl, and, well, I'm not
familiar with anything else.
The only book I have on Perl is: "Perl5 for Web programming" circa 1998.
This book showed me basic Perl stuff such as:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "html stuff \n";
$temp = $ENV{'QUERY_STRING'};
@pairs = split(/&/,$temp);
open (myfile, "names.txt");
foreach $items(@pairs) {
But I see stuff like this, which appears to be related to cgi.pm, and which
I never really learned.
use CGI qw/:standard/;
$q = new CGI;
@names = $q->param;
$value = $q->param('t01');
print $q->header,
$q->start_html('hello world'),
$q->end_html;
Do I need a new book on Perl, or do I need a book on CGI.pm (if such a thing
exists). I'd like to understand this $q=new CGI stuff.
Can anyone suggest a book or type of book? Thanks for all,
Guy
------------------------------
Date: Fri, 17 Apr 2009 17:12:40 -0700
From: Nathan Keel <nat.k@gm.ml>
Subject: Re: I'm looking for a Perl Book... I think.
Message-Id: <YV8Gl.55865$ua7.40282@newsfe17.iad>
Guy wrote:
> Ok, I'm creating my own website on genealogy.
>
> I'd like visitors to be able to search through my site, search for
> names etc...
>
> I'm alright with HTML and I've got enough experience with Javascript.
>
> But I need to do some Server-Side programming.
>
> The only experience I have here is with Perl, but many years ago.
>
> I'm not sure if there are better ways but I like Perl, and, well, I'm
> not familiar with anything else.
>
> The only book I have on Perl is: "Perl5 for Web programming" circa
> 1998.
>
> This book showed me basic Perl stuff such as:
> #!/usr/bin/perl
> print "Content-type: text/html\n\n";
> print "html stuff \n";
> $temp = $ENV{'QUERY_STRING'};
> @pairs = split(/&/,$temp);
> open (myfile, "names.txt");
> foreach $items(@pairs) {
>
> But I see stuff like this, which appears to be related to cgi.pm, and
> which I never really learned.
> use CGI qw/:standard/;
> $q = new CGI;
> @names = $q->param;
> $value = $q->param('t01');
> print $q->header,
> $q->start_html('hello world'),
> $q->end_html;
>
> Do I need a new book on Perl, or do I need a book on CGI.pm (if such a
> thing
> exists). I'd like to understand this $q=new CGI stuff.
>
> Can anyone suggest a book or type of book? Thanks for all,
> Guy
Look at Learning Perl, it's the standard book. You can read the docs
for the CGI module and know the rest. Lincoln Stein wrote a book about
CGI.pm, I think? Anyway, your book seems old. Depending on the
quality of the original book (I don't know anything about it), you
might want to get a better one (not just a newer one). For a Perl 5
book to give the example you did above, I'd say it's not such a good
book regardless of the year.
------------------------------
Date: Fri, 17 Apr 2009 20:06:21 -0500
From: Michael Austin <maustin@firstdbasource.com>
Subject: Re: I'm looking for a Perl Book... I think.
Message-Id: <dz9Gl.2696$fD.1882@flpi145.ffdc.sbc.com>
Guy wrote:
> Ok, I'm creating my own website on genealogy.
>
> I'd like visitors to be able to search through my site, search for names
> etc...
>
> I'm alright with HTML and I've got enough experience with Javascript.
>
> But I need to do some Server-Side programming.
>
> The only experience I have here is with Perl, but many years ago.
>
> I'm not sure if there are better ways but I like Perl, and, well, I'm not
> familiar with anything else.
>
> The only book I have on Perl is: "Perl5 for Web programming" circa 1998.
>
> This book showed me basic Perl stuff such as:
> #!/usr/bin/perl
> print "Content-type: text/html\n\n";
> print "html stuff \n";
> $temp = $ENV{'QUERY_STRING'};
> @pairs = split(/&/,$temp);
> open (myfile, "names.txt");
> foreach $items(@pairs) {
>
> But I see stuff like this, which appears to be related to cgi.pm, and which
> I never really learned.
> use CGI qw/:standard/;
> $q = new CGI;
> @names = $q->param;
> $value = $q->param('t01');
> print $q->header,
> $q->start_html('hello world'),
> $q->end_html;
>
> Do I need a new book on Perl, or do I need a book on CGI.pm (if such a thing
> exists). I'd like to understand this $q=new CGI stuff.
>
> Can anyone suggest a book or type of book? Thanks for all,
> Guy
>
>
While perl is a very good CGI language, you might also consider PHP as
it has easier and almost as powerful a command set that integrates a lot
easier with HTML.
As for the book - the best you can do is:
google perl <whatever it is you are trying to do> example
You may see a lot of noise, but you will find that which you seek.
------------------------------
Date: Fri, 17 Apr 2009 13:41:52 -0700
From: sln@netherlands.com
Subject: Re: perls popularity
Message-Id: <42qhu4l8n49fn13iqk17g62ab5nholfok2@4ax.com>
On Fri, 17 Apr 2009 14:09:05 -0400, "darkon" <darkon.tdo@gmail.com> wrote:
>
><g3rc4n@gmail.com> wrote in message
>news:4b7b9910-8c3c-461a-a4d1-650b354bd0e9@w9g2000yqa.googlegroups.com...
>> how well is perl doing as a language, how long is it going to be
>> around for? not trolling but i've started asking myself questions on
>> the benifits in the long run of using perl of unix administration,
>> just is it as popular as i originally thought it was???? because my
>> mate said it was dying
>
>(andecdote/not-data alert)
>
>I use Perl almost every day, just not for web programming. At least not
>directly. Most of my uses are for short utility programs that I write
>quickly then then throw away, but without Perl some tasks would take me much
>longer, sometime by several orders of magnitude. It's my pocketknife,
>screwdriver, hammer, wrench set, and other assorted tools all in one. If I
>were working in a Unix environment it might not be as necessary, but since
>I'm confined to Windows, Perl is a lifesaver. (And I'm not looking for a
>job to get away from Windows, because I have job security and a good salary.
>With the economy the way it is, I'd be stupid to walk away from that just to
>work with Unix.)
I need a job, help me. I can do advanced research, I have a BS in Mechanical-E
used only 5 years till Amiga 1000, still new, 25 years paid programming since,
old but still usable.
Will work for hay.
-sln
(Seabiscuit)
------------------------------
Date: Fri, 17 Apr 2009 17:10:11 -0700
From: Nathan Keel <nat.k@gm.ml>
Subject: Re: perls popularity
Message-Id: <FT8Gl.55864$ua7.42313@newsfe17.iad>
g3rc4n@gmail.com wrote:
> how well is perl doing as a language, how long is it going to be
> around for? not trolling but i've started asking myself questions on
> the benifits in the long run of using perl of unix administration,
> just is it as popular as i originally thought it was???? because my
> mate said it was dying
For *nix sys admin tasks, I can't think of a better language to use. I
don't know of a single good *nix sys admin that doesn't use Perl more
often than anything else (unless a simple command does what you want
already). Shell scripting can be a hassle and Perl makes it a lot more
powerful and precise and makes it often easier, depending on what
you're doing. What could possibly replace Perl for such a thing and be
better? Everyone has their language of choice, but who would make an
effort to write admin scripts in Python, Ruby or PHP just to say Perl
isn't as popular? Of course it is. Look on usenet, it's one of the
more busy groups when most others are dead or dying. Only the Ruby
group is maybe on par with the traffic the Perl group gets. Use
whatever you like, anything that offers a CLI interface (Perl, Ruby,
PHP, Python, etc.) is fine, use what you know and like best. Like I
said, I don't know of any good sys admin that either doesn't know Perl
(and the one's that don't wish they did). There's always some fool
that will claim it's dying, just ignore those people.
------------------------------
Date: Fri, 17 Apr 2009 17:16:27 -0700
From: sln@netherlands.com
Subject: Re: perls popularity
Message-Id: <4q6iu4hpkqj42ed7hd81dsfplj8crqm48i@4ax.com>
On Fri, 17 Apr 2009 17:10:11 -0700, Nathan Keel <nat.k@gm.ml> wrote:
>g3rc4n@gmail.com wrote:
>
>> how well is perl doing as a language, how long is it going to be
>> around for? not trolling but i've started asking myself questions on
>> the benifits in the long run of using perl of unix administration,
>> just is it as popular as i originally thought it was???? because my
>> mate said it was dying
>
>For *nix sys admin tasks, I can't think of a better language to use. I
>don't know of a single good *nix sys admin that doesn't use Perl more
>often than anything else (unless a simple command does what you want
>already). Shell scripting can be a hassle and Perl makes it a lot more
>powerful and precise and makes it often easier, depending on what
>you're doing. What could possibly replace Perl for such a thing and be
>better? Everyone has their language of choice, but who would make an
>effort to write admin scripts in Python, Ruby or PHP just to say Perl
>isn't as popular? Of course it is. Look on usenet, it's one of the
>more busy groups when most others are dead or dying. Only the Ruby
>group is maybe on par with the traffic the Perl group gets. Use
>whatever you like, anything that offers a CLI interface (Perl, Ruby,
>PHP, Python, etc.) is fine, use what you know and like best. Like I
>said, I don't know of any good sys admin that either doesn't know Perl
>(and the one's that don't wish they did). There's always some fool
>that will claim it's dying, just ignore those people.
I'm pretty sure unix or any other admins brains can hardly make thier legs work
let alone do Perl.
This as your intro to this topic gives you a big 5-thumbs down!
-sln
------------------------------
Date: Fri, 17 Apr 2009 17:24:28 -0700
From: Nathan Keel <nat.k@gm.ml>
Subject: Re: perls popularity
Message-Id: <059Gl.55867$ua7.53077@newsfe17.iad>
sln@netherlands.com wrote:
> On Fri, 17 Apr 2009 17:10:11 -0700, Nathan Keel <nat.k@gm.ml> wrote:
>
>>g3rc4n@gmail.com wrote:
>>
>>> how well is perl doing as a language, how long is it going to be
>>> around for? not trolling but i've started asking myself questions on
>>> the benifits in the long run of using perl of unix administration,
>>> just is it as popular as i originally thought it was???? because my
>>> mate said it was dying
>>
>>For *nix sys admin tasks, I can't think of a better language to use.
>>I don't know of a single good *nix sys admin that doesn't use Perl
>>more often than anything else (unless a simple command does what you
>>want
>>already). Shell scripting can be a hassle and Perl makes it a lot
>>more powerful and precise and makes it often easier, depending on what
>>you're doing. What could possibly replace Perl for such a thing and
>>be
>>better? Everyone has their language of choice, but who would make an
>>effort to write admin scripts in Python, Ruby or PHP just to say Perl
>>isn't as popular? Of course it is. Look on usenet, it's one of the
>>more busy groups when most others are dead or dying. Only the Ruby
>>group is maybe on par with the traffic the Perl group gets. Use
>>whatever you like, anything that offers a CLI interface (Perl, Ruby,
>>PHP, Python, etc.) is fine, use what you know and like best. Like I
>>said, I don't know of any good sys admin that either doesn't know Perl
>>(and the one's that don't wish they did). There's always some fool
>>that will claim it's dying, just ignore those people.
>
> I'm pretty sure unix or any other admins brains can hardly make thier
> legs work let alone do Perl.
>
> This as your intro to this topic gives you a big 5-thumbs down!
>
> -sln
I can't make sense of your reply. Anyway, I said any *good* admin. Any
good admin will be smart and they can figure plenty of things out. If
they aren't smart, they won't be a good admin or a good coder.
Apparently your idea of what an admin is or does is different than the
rest of the world.
------------------------------
Date: Fri, 17 Apr 2009 14:27:02 -0700
From: sln@netherlands.com
Subject: Reading Text File Encoding and converting to Perls internal UTF-8 encoding
Message-Id: <gnrhu45bberbg1199lc9g00hio9lrajh2v@4ax.com>
Need help from Unicode guru's or anybody with some knowledge on the subject.
I maybe have a text (character) file I just open. But I don't know the encoding and I
can't open it with any encoding attribute.
It would appear to me that at the start of the file, there is an encoding mark (or none),
assuming a text file, a sort of BOM sequence of octets that mark what its encoding is.
Given that I might be passed a file descriptor only, I am module, and I rewind the position
to the start of the file, is there any way I can tell the encoding. If I could, and
its not utf8, I could decode() the rest of the file as octets, ie: in-place memeory decode,
create a temp file decoded, or possibly re-open it with the proper encoding.
I think that encoding is the usual 8/16/32 bit utf but with many locales (chars).
I am still sketchy where to find a list of encoding markers to be able to find out
this information. And still sketchy on the methods available for analysis and transformation.
I know Perl has a massive 'use Encode' lib, nevertheless, this is what I need to do to finalize
a module I'm working on.
Thanks for the help.
-sln
------------------------------
Date: Fri, 17 Apr 2009 16:22:47 -0700
From: sln@netherlands.com
Subject: Re: Reading Text File Encoding and converting to Perls internal UTF-8 encoding
Message-Id: <bo3iu4dshnkitml3ffjmfupqk39ski79hr@4ax.com>
On Fri, 17 Apr 2009 23:48:10 +0100, Robert Billing <unclebob@tnglwood.demon.co.uk> wrote:
>sln@netherlands.com wrote:
>
>> Given that I might be passed a file descriptor only, I am module, and I rewind the position
>> to the start of the file, is there any way I can tell the encoding. If I could, and
>> its not utf8, I could decode() the rest of the file as octets, ie: in-place memeory decode,
>> create a temp file decoded, or possibly re-open it with the proper encoding.
>
>As I understand it, and I have just written some Perl code that happily
>mixes two dozen languages in one web page, there isn't a really good way
>of doing what you want. Part of the reason for this is that given a big
>block of text encoded as plain ASCII, the same text in UTF8 is exactly,
>bit for bit, the same. It's only when you introduce "wide" characters in
>other alphabets that UTF8 does anything.
>
>In some cases it may be possible to make an intelligent guess at the
>encoding, but no more.
>
>Incidentally, and somewhat off-topic, is there anyone else for whom the
>letters UTF automatically mean 'use the force'?
I'm sorry, 'I exists and therefore I am' doesen't seem to work.
-sln
------------------------------
Date: Fri, 17 Apr 2009 23:48:10 +0100
From: Robert Billing <unclebob@tnglwood.demon.co.uk>
Subject: Re: Reading Text File Encoding and converting to Perls internal UTF-8 encoding
Message-Id: <LG7Gl.43181$cX1.7823@newsfe28.ams2>
sln@netherlands.com wrote:
> Given that I might be passed a file descriptor only, I am module, and I rewind the position
> to the start of the file, is there any way I can tell the encoding. If I could, and
> its not utf8, I could decode() the rest of the file as octets, ie: in-place memeory decode,
> create a temp file decoded, or possibly re-open it with the proper encoding.
As I understand it, and I have just written some Perl code that happily
mixes two dozen languages in one web page, there isn't a really good way
of doing what you want. Part of the reason for this is that given a big
block of text encoded as plain ASCII, the same text in UTF8 is exactly,
bit for bit, the same. It's only when you introduce "wide" characters in
other alphabets that UTF8 does anything.
In some cases it may be possible to make an intelligent guess at the
encoding, but no more.
Incidentally, and somewhat off-topic, is there anyone else for whom the
letters UTF automatically mean 'use the force'?
--
I am Robert Billing, Christian, author, inventor, traveller, cook and
animal lover. "It burned me from within. It quickened; I was with book
as a woman is with child."
Quality e-books for portable readers: http://www.alex-library.com
------------------------------
Date: Sat, 18 Apr 2009 01:58:50 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Reading Text File Encoding and converting to Perls internal UTF-8 encoding
Message-Id: <a7lnb6-8m03.ln1@osiris.mauzo.dyndns.org>
Quoth Robert Billing <unclebob@tnglwood.demon.co.uk>:
> sln@netherlands.com wrote:
>
> > Given that I might be passed a file descriptor only, I am module, and
> I rewind the position
> > to the start of the file, is there any way I can tell the encoding.
> If I could, and
> > its not utf8, I could decode() the rest of the file as octets, ie:
> in-place memeory decode,
> > create a temp file decoded, or possibly re-open it with the proper encoding.
>
> As I understand it, and I have just written some Perl code that happily
> mixes two dozen languages in one web page, there isn't a really good way
> of doing what you want. Part of the reason for this is that given a big
> block of text encoded as plain ASCII, the same text in UTF8 is exactly,
> bit for bit, the same. It's only when you introduce "wide" characters in
> other alphabets that UTF8 does anything.
In that particular case it doesn't matter: whether you decode it as
UTF-8 or ASCII you still get the same characters. What's more important
is that any piece of UTF-8 text is also valid (though probably
nonsensical) in any of the 8bit ISO-8859-X encodings, and anything in
one of those encodings is also valid in all the others.
There is the Encode::Guess module that attempts to guess the encoding of
a given string, but as you can tell from the name it's only a heuristic.
Depending on what you're decoding it may be good enough, though.
> Incidentally, and somewhat off-topic, is there anyone else for whom the
> letters UTF automatically mean 'use the force'?
Err... I doubt it? :)
Ben
------------------------------
Date: Fri, 17 Apr 2009 13:16:09 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: Replacing text containing parenthesis
Message-Id: <ds5Gl.17708$g%5.12431@newsfe23.iad>
Mark Hobley wrote:
> John W. Krahn <someone@example.com> wrote:
>
>> Run:
>>
>> echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
>> from the shell to see how the shell interpolates it.
>
> Hmmm, it appeared to be ok ...
>
> echo "s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;"
> s/$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;
You are missing the \ in front of the $ so perl interpolates that as the
variable $\ and the left parenthesis is therefore not escaped generating
the error message "Unmatched ( in regex;".
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
------------------------------
Date: Fri, 17 Apr 2009 21:40:34 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Replacing text containing parenthesis
Message-Id: <236nb6-fjv2.ln1@osiris.mauzo.dyndns.org>
Quoth markhobley@hotpop.donottypethisbit.com (Mark Hobley):
> Ben Morrow <ben@morrow.me.uk> wrote:
>
> > Using single quotes for the shell will remove one level of
> > interpolation, and make things simpler. Using alternative delimiters for
> > the s/// will help get rid of some of those backwacks, and using \Q will
> > get rid of most of the rest:
> >
> > ... -e 's!\Q\$(top_srcdir)/../config/override.m4!!g;' \;
>
> That is interesting. What is the capital Q?
See "Quote and Quote-like Operators" in perldoc perlop, and also perldoc
-f quotemeta which is the underlying implementation.
Ben
------------------------------
Date: Fri, 17 Apr 2009 13:54:38 -0700
From: sln@netherlands.com
Subject: Re: Replacing text containing parenthesis
Message-Id: <kqqhu4lvbkl7pdjqp7qsc5qcbj2halgeuc@4ax.com>
On Fri, 17 Apr 2009 17:08:02 GMT, markhobley@hotpop.donottypethisbit.com (Mark Hobley) wrote:
>I am using perl as a find and replace tool. I want to replace the following
>text with a null:
>
>$(srcdir)/config/override.m4
>
>I tried the following:
>
>find ./ -name Makefile.in -exec perl -pi -e \
>"s/\$\(top_srcdir\)\/\.\.\/config\/override\.m4//g;" {} \;
>
>Unfortunately, this gives an error:
>
>Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE top_srcdir\)/\.\./ \
>config/override\.m4/ at -e line 1, <> line 1.
>
>I guess that the brackets are being evaluated first, and being treated as part
>of the expression.
>
>How do I represent literal brackets within the expression?
>
>Mark.
You know what? I FIND it hard to believe you type this from a command shell.
And I find it hard to believe you maintain a battery of batch files to shell
to perl scripts quoted from a batch file to be shelled to Perl from a shelled
shell.
-sln
(shell shocked!)
------------------------------
Date: Fri, 17 Apr 2009 15:32:54 -0500
From: Michael Austin <maustin@firstdbasource.com>
Subject: Re: Specifyig username and password in PERL
Message-Id: <WC5Gl.5670$Lr6.4781@flpi143.ffdc.sbc.com>
tandon.sourabh@gmail.com wrote:
> Is there a way to navigate through a secure website (https) where I
> have the username and password? I know the exact URL but do not know
> how to specify the username and password in the PERL script so that
> the website allows me in.
Similar to logging into a linksys router and getting the current IP??
You will need to make the mods to use HTTPS/port 443
Here is a pretty good example:
#!/usr/bin/perl -w
#!/usr/local/bin/perl -w
## $ENV{'PATH'} = (exists($ENV{PATH}) ? "$ENV{PATH}:" : "") .
"/sbin:/usr/sbin:/
bi
## n:/usr/bin:/etc:/usr/lib:";
use Getopt::Long;
use Sys::Hostname;
use IO::Socket;
sub encode_base64 ($;$) {
my $res = '';
my $eol = $_[1];
$eol = "\n" unless defined $eol;
pos($_[0]) = 0; # ensure start at the
beginning
while ($_[0] =~ /(.{1,45})/gs) {
$res .= substr(pack('u', $1), 1);
chop($res);
}
$res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
# fix padding at the end
my $padding = (3 - length($_[0]) % 3) % 3;
$res =~ s/.{$padding}$/'=' x $padding/e if $padding;
# break encoded string into lines of no more than 76 characters each
if (length $eol) {
$res =~ s/(.{1,76})/$1$eol/g;
}
$res;
}
$0 = "findIP";
my $version = "3.5.4";
my $programd = $0;
$programd =~ s%^.*/%%;
my $program = $programd;
$program =~ s/d$//;
my $now = time;
my $hostname = hostname();
my ($peer, $server, $port);
my ($sd, $rq, $request, $reply);
my $url = "HTTP://192.168.1.1:80/Status_Router.asp";
$url =~ s%^HTTP://%%i;
$server = $url;
$server =~ s%/.*%%;
$url = "/" unless $url =~ m%/%;
$url =~ s%^[^/]*/%%;
## determine peer and port to use.
$peer = $server;
$peer =~ s%/.*%%;
$port = $peer;
$port =~ s%^.*:%%;
$port = 80 unless $port =~ /^\d+$/;
$peer =~ s%:.*$%%;
#### username not required for linksys, so anything will work here
#### you just need to enter your password.
my $auth = encode_base64("admin:mypassword");
$request = "GET ";
# $request .= "HTTP://$server";
$request .= "/$url HTTP/1.0\n";
# $request .= "Host: $server\n";
$request .= "Authorization: Basic $auth";
$request .= "User-Agent: ${program}/${version}\n";
$request .= "Connection: open\n";
$request .= "\n";
## make sure newlines are <cr><lf> for some pedantic proxy servers
($rq = $request) =~ s/\n/\r\n/g;
# printf("$rq");
local $^W = 0;
$sd = IO::Socket::INET->new(PeerAddr => $peer,
PeerPort => $port,
Proto => 'tcp');
my $skip='interipaddr';
my $DNS1='script>1';
my $DNS2='script>2';
my $DNS3='script>3';
my $result = send $sd, $rq, 0;
if ($result != length($rq)) {
printf("cannot send to 192.168.1.1:80 ($!).\n");
} else {
while ($_ = <$sd>) {
# verbose("RECEIVE:", "%s", define($_, "<undefined>"));
$reply .= $_ if defined $_;
}
# printf(" $reply\n");
if ($reply =~
/${skip}.*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*/is)
{
$WANip = $1;
}
if ($reply =~
/${DNS1}.*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*/is)
{
$DNS1 = $1;
}
if ($reply =~
/${DNS2}.*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*/is)
{
$DNS2 = $1;
}
if ($reply =~
/${DNS3}.*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*/is)
{
$DNS3 = $1;
}
printf("Current IP address is: $WANip\n");
printf("DNS 1 IP address is: $DNS1\n");
printf("DNS 2 IP address is: $DNS2\n");
printf("DNS 3 IP address is: $DNS3\n");
close($sd);
}
------------------------------
Date: Fri, 17 Apr 2009 13:24:37 -0700
From: sln@netherlands.com
Subject: Re: What does `my' do?!
Message-Id: <c1phu4htc0sfeijhrma5s2puq878mg5jg7@4ax.com>
On Thu, 16 Apr 2009 22:43:20 GMT, Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
>On 2009-04-16, sln@netherlands.com <sln@netherlands.com> wrote:
>> On Thu, 16 Apr 2009 14:15:40 GMT, Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
>> perl -wle " BEGIN{ $x=12; print $x; } print 'here'; my $x=99; print $x"
>> 12
>> here
>> 99
>> ------------
>> perl -wle " BEGIN{ $x=12; print $x; } print 'here'; my $x; print $x"
>> 12
>> here
>> Use of uninitialized value in print at -e line 1.
>
>Keep in mind that in these examples you have DIFFERENT variables, all
>named $x.
>
>Yours,
>Ilya
Yes, one is $main::x in the BEGIN, the other is lexical my().
Didn't realize until after I posted.
Besides this unusual initialization/assignment thing, the docs specify
BEGIN is wrapped in an eval when it is run, which I thought odd.
Hope you fix it.
-sln
------------------------------
Date: Fri, 17 Apr 2009 13:27:20 -0700
From: sln@netherlands.com
Subject: Re: What does `my' do?!
Message-Id: <mephu4h4cah2oa2h5vms3i6q3pninjba87@4ax.com>
On Thu, 16 Apr 2009 22:58:04 GMT, Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
>On 2009-04-16, Tad J McClellan <tadmc@seesig.invalid> wrote:
>>> perl -wle "my $x; BEGIN{ $x=12 } print $x"
>>> 12
>
>> You develop on Windows?
>
>I still manage to run OS/2 on all boxes in the house...
WOOS2 - Windows on OS/2
-sln
------------------------------
Date: Thu, 16 Apr 2009 11:53:37 -0700
From: sln@netherlands.com
Subject: Re: What does `my' do?!
Message-Id: <3jveu49mvdpi6odev4hdqsfp9opdnhcm3p@4ax.com>
On Thu, 16 Apr 2009 12:55:20 -0500, Tad J McClellan <tadmc@seesig.invalid> wrote:
>Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
>> I managed to confuse myself in a teaspoon... Consider
>>
>> perl -wle "my $x; BEGIN{ $x=12 } print $x"
>> 12
>
>
>You develop on Windows?
>
>
>> I would expect it to warn that $x is uninitialized, and print
>> nothing. The reasoning goes like that:
>>
>> AT COMPILE TIME:
>> `my' is seen; after this $x is considered as lexical
>> BEGIN is compiled and executed; $x becomes 12
>> `print' is compiled
>>
>> AT RUN TIME
>> `my' is executed. [*] Variable $x becomes undef
>> `print' is executed
>>
>> Apparently, [*]-action is not executed in this context. The question
>> is: why?
>
>
>Because $x is not being assigned to in the my(), I guess.
>
>i.e. "no initializer is given" (see below).
>
>
>> Is it documented?
>
>
>I think so, if you can divine that "created" means "at compile time"...
>
>From perlsub.pod (with my emphasis):
>
> The parameter list to my() may be assigned to if desired, which allows you
> to initialize your variables. (If no initializer is given for a
> particular variable, it is created with the undefined value.)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>and since "creation" is in my()'s compile-time component, that is when
>it is given the undef value.
>
>If we _do_ give an initializer:
>
> perl -wle 'my $x=undef; BEGIN{ $x=12 } print $x'
>
>then things match up with your expectations.
^^^^^
Not really...
perl -wle "my $x=undef; BEGIN{ $x=12 } print $x"
12
------------------
perl -wle "my $x; undef($x); BEGIN{ $x=12 } print $x"
Use of uninitialized value in print at -e line 1.
-sln
------------------------------
Date: Fri, 17 Apr 2009 17:02:46 -0700
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: What does `my' do?!
Message-Id: <49e91b33$0$16102$ed362ca5@nr5-q3a.newsreader.com>
Ben Morrow wrote:
>
> No. People seem to keep making this mistake. Named subs are *not*
> closures in Perl 5, they simply keep a ref to all the variables they
> reference.
Isn't that what a closure is?
Xho
------------------------------
Date: Sat, 18 Apr 2009 01:52:38 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: What does `my' do?!
Message-Id: <mrknb6-8m03.ln1@osiris.mauzo.dyndns.org>
Quoth Xho Jingleheimerschmidt <xhoster@gmail.com>:
> Ben Morrow wrote:
> >
> > No. People seem to keep making this mistake. Named subs are *not*
> > closures in Perl 5, they simply keep a ref to all the variables they
> > reference.
>
> Isn't that what a closure is?
Not quite. Named subs are compiled once at compile time, and only keep a
ref to the outer lexicals as they existed then. This means that code
like
use warnings;
sub mkfoo {
my ($x) = @_;
sub foo { $x }
return \&foo;
}
say mkfoo($_)->() for 1..2;
gives
Variable "$x" will not stay shared at clos line 6.
1
1
whereas if you use an anon sub you get a fresh clone of the sub with
refs to the *current* set of outer lexicals, so code like
sub mkfoo {
my ($x) = @_;
return sub { $x };
}
say mkfoo($_)->() for 1..2;
gives
1
2
as expected. This is obviously important when using closures as
callbacks and such.
Ben
------------------------------
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:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#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 2348
***************************************