Help with simple simple perl script... 
Author Message
 Help with simple simple perl script...

First time Linux user, first time Perl user.

I found and modified a PERL script to update my DHS.org web address whenever
my IP address changes.  I can do this manually, by typing

lynx -dump -auth=user:pass
https://members.dhs.org/nic/hosts?hostscmd=edit&hostscmdstage=2&type=...
etype=Online&ip=<ip
address>&mx=keenerb.dyn.dhs.org&offline_url=&cloak=y&domain=dyn.dhs.org&host
name=keenerb

at a BASH prompt.

Line probably wrapped like hell.  Sorry.

In the PERL script, it looks like this:

     $url1 =
https://members.dhs.org/nic/hosts?hostscmd=edit&hostscmdstage=2&type=...
etype=Online&ip=$newip&mx=keenerb.dyn.dhs.org&offline_url=&cloak=y&domain=dy
n.dhs.org&hostname=keenerb;

     'lynx -dump -auth=user:pass $url1';

$newip is a variable, and definitely contains the correct IP address.
Again, line wrapping probably screwed this up as well.  Isn't there some
sort of way to force lines to not wrap?

Anyway, the crux of the matter is that the perl script doesn't work, but the
exact same command works (echoed the entire line to console, copied and
pasted it into another command line and ran it.)

Any ideas?

Brian K



Sun, 13 Mar 2005 15:50:13 GMT  
 Help with simple simple perl script...
On Wed, 25 Sep 2002 10:50:13 -0400, "Brian Keener"

Quote:

>I found and modified a PERL

Sorry, to be pedantic, but it's Perl, or occasionally perl,
never PERL.

Quote:
> script to update my DHS.org web address
>whenever my IP address changes.  I can do this manually, by typing
>lynx -dump -auth=user:pass
>https://members.dhs.org/nic/hosts?hostscmd=edit&hostscmdstage=2&type=...
>etype=Online&ip=<ip
>address>&mx=keenerb.dyn.dhs.org&offline_url=&cloak=y&domain=dyn.dhs.org&host
>name=keenerb

>at a BASH prompt.

Well, the simple answer would be to save your thingie as a
bash shell script named, say update_ip, make it executable
by "chmod 700 update_ip" and you're done.

It's probably the best way, since you're not using Perl *AT
ALL*, just using it to make a shell wrapper anyway.
You're actually making lynx do all the work.

Quote:
>Line probably wrapped like hell.  Sorry.

OK, can't be helped.

Quote:
>In the PERL script, it looks like this:

First, if you intend to do any sort of Perl programming in
the future, get into the habits of allowing strictures
and warnings, like so:

use strict;
use warnings;

You will save yourself untold hours, nay decades
of tedious debugging if you do and never recieve
any help from competent humans if you don't.

Using strictures would mean you would have to
declare $url1 like so:

my $url1 = "https://......$newip.....keenerb";

And you need double quotes around that to interpolate
$newip properly.  Single quotes ('') do *not* interpolate
variables.

Quote:
>  $url1 =
>https://members.dhs.org/nic/hosts?hostscmd=edit&hostscmdstage=2&type=...
>etype=Online&ip=$newip&mx=keenerb.dyn.dhs.org&offline_url=&cloak=y&domain=dy
>n.dhs.org&hostname=keenerb;

>     'lynx -dump -auth=user:pass $url1';

Again, you need double quotes around the string to
interpolate/expand $url1.  

Now that you have your variable:

my $command = "lynx -dump -auth=user:pass $url1";

You need to do something with it.  If you had enabled
warnings, you would have been warned that:
Useless use of a constant in void context at script line X.

What you want to do with it is run it as a system
command and lo and behold: perldoc -q system
or perldoc -f system would have told you how:

system($command) == 0 or die "Error running $command:$?\n";

System commands should return 0 if they are successful.
If not, the error will be contained in the special variable
$?.

<SNIP>

Quote:
>Anyway, the crux of the matter is that the perl script doesn't work,

Never, never ever use the phrase "doesn't work" in a
programming newsgroup.  It is uninformative.  It says
nothing.  What happened?  Exactly what error did the
system report?  

Final code below:

#!/perl
use strict;
use warnings;

my $newip = '1.2.3.4';
my  $url1 =
"https://members.dhs.org/nic/hosts?hostscmd=edit&hostscmdstage=2&type=...
etype=Online&ip=$newip&mx=keenerb.dyn.dhs.org&offline_url=&cloak=y&domain=dy
n.dhs.org&hostname=keenerb";
my $command = "lynx -dump -auth=user:pass $url1";
system($command) == 0 or die "Error running $command:$?\n";
__END__
--
Regards, Helgi Briem
helgi AT decode DOT is

                           A: Top posting
                           Q: What is the most irritating thing on Usenet?
                                           - "Gordon" on apihna



Sun, 13 Mar 2005 16:16:09 GMT  
 Help with simple simple perl script...

Quote:

> First time Linux user, first time Perl user.

Gulp...

Quote:
> I found and modified a PERL script

... gulp, gulp.   :-)

( A secret hint:

   "Perl" is used to refer to the language proper.

   "perl" is used to refer to the implementation of the language (interpreter).

   "PERL" is not used.
)

Quote:
> to update my DHS.org web address whenever
> my IP address changes.  I can do this manually, by typing

> lynx -dump -auth=user:pass
> https://members.dhs.org/nic/hosts?hostscmd=edit

[snip long URL]

Quote:
> at a BASH prompt.
> In the PERL script, it looks like this:

>      $url1 =
> https://members.dhs.org/nic/hosts?hostscmd=edit&hostscmdstage=2&type=...
> etype=Online&ip=$newip&mx=keenerb.dyn.dhs.org&offline_url=&cloak=y&domain=dy
> n.dhs.org&hostname=keenerb;

So then, you are getting a syntax error?

Or did you try and type in code instead of copy/pasting it, and
you made a mistake (left out the quotes) in transferring it?

Have you seen the Posting Guidelines that are posted here weekly?

(your Subject was poorly chosen too)

Quote:
>      'lynx -dump -auth=user:pass $url1';

Those are not the right kind of quotes (or is that another mistake
in typing the code into your article?).

Is is the _other_ (backwards) single quote that runs external programs.

But you should not use that unless you want to capture the program's
STDOUT output:

   $lynx_said = `lynx -dump -auth=user:pass $url1`;

If you just want to run it and have the output go to whereever
the output normally goes, then use system() instead of backticks:

   system "lynx -dump -auth=user:pass $url1";

or, more safely (avoids a shell):

   system 'lynx', '-dump', '-auth=user:pass',  $url1;

You might want to also check the return value of system() to
determine whether the lynx program succeeded or failed.

Quote:
> Isn't there some
> sort of way to force lines to not wrap?

That would depend on what news client you are using.

(and you would ask it in a newsgroup about news clients.)

Quote:
> Any ideas?

I wouldn't use lynx, I'd use a Perl module instead.

You can do GET requests from within Perl using this module
(part of the libwww bundle):

   http://search.cpan.org/author/GAAS/LWP5emu-0.01_01/lib/LWP/UserAgent.pm

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sun, 13 Mar 2005 16:28:48 GMT  
 Help with simple simple perl script...


Quote:

> > First time Linux user, first time Perl user.

> Gulp...

Gulp indeed...  To make things even WORSE, I'm a former VB programmer and MS
Office VBA programmer.

I will blame all problems with my former post on Outlook Express.  Yeah,
yeah, that's the ticket.

Quote:

> > I found and modified a PERL script

> ... gulp, gulp.   :-)

:-)

Quote:

> ( A secret hint:

>    "Perl" is used to refer to the language proper.

>    "perl" is used to refer to the implementation of the language
(interpreter).

>    "PERL" is not used.
> )

Ignorance on my part.

Quote:

> > to update my DHS.org web address whenever
> > my IP address changes.  I can do this manually, by typing

> > lynx -dump -auth=user:pass
> > https://members.dhs.org/nic/hosts?hostscmd=edit

> [snip long URL]

> > at a BASH prompt.

> > In the PERL script, it looks like this:

> >      $url1 =

https://members.dhs.org/nic/hosts?hostscmd=edit&hostscmdstage=2&type=...
etype=Online&ip=$newip&mx=keenerb.dyn.dhs.org&offline_url=&cloak=y&domain=dy

Quote:
> > n.dhs.org&hostname=keenerb;

> So then, you are getting a syntax error?

No, it just runs without any discernable errors at all.

Quote:

> Or did you try and type in code instead of copy/pasting it, and
> you made a mistake (left out the quotes) in transferring it?

I re-typed the entire script, pretty much, copied a few blocks of code in
from various sources.  My mistake was replacing ` with '.  I assumed it was
a codepage mixup, I've seen those a lot in Windows with SQL scripts.  Yes, I
know this isn't Windows...

Quote:

> Have you seen the Posting Guidelines that are posted here weekly?

Just now.  I DID search for a faq for the group on google, but the first
page or so was not very helpful.

Quote:

> (your Subject was poorly chosen too)

I couldn't really come up with a good "summary" at the moment.
"Problem executing shell command from within Linux script" would probably do
well, I guess...

Quote:

> >      'lynx -dump -auth=user:pass $url1';

> Those are not the right kind of quotes (or is that another mistake
> in typing the code into your article?).

Mistake in my typing.  I found that out moments before I read this post,
thanks to Google.
I _NEVER_ would have thought that could be the problem...

Quote:

> Is is the _other_ (backwards) single quote that runs external programs.

> But you should not use that unless you want to capture the program's
> STDOUT output:

>    $lynx_said = `lynx -dump -auth=user:pass $url1`;

Yes, I would like to be able to do this.

- Show quoted text -

Quote:

> If you just want to run it and have the output go to whereever
> the output normally goes, then use system() instead of backticks:

>    system "lynx -dump -auth=user:pass $url1";

> or, more safely (avoids a shell):

>    system 'lynx', '-dump', '-auth=user:pass',  $url1;

> You might want to also check the return value of system() to
> determine whether the lynx program succeeded or failed.

> > Isn't there some
> > sort of way to force lines to not wrap?

> That would depend on what news client you are using.

> (and you would ask it in a newsgroup about news clients.)

> > Any ideas?

> I wouldn't use lynx, I'd use a Perl module instead.

> You can do GET requests from within Perl using this module
> (part of the libwww bundle):

>    http://search.cpan.org/author/GAAS/LWP5emu-0.01_01/lib/LWP/UserAgent.pm

OK, I'll take a look.

Quote:

> --
>     Tad McClellan                          SGML consulting

>     Fort Worth, Texas

Brian K


Sun, 13 Mar 2005 17:09:39 GMT  
 Help with simple simple perl script...
On Sep 25, Helgi Briem inscribed on the eternal scroll:

Quote:
> system($command) == 0 or die "Error running $command:$?\n";

That's certainly more didactic than the often-seen (and
often-leads-to-confusion)  "system... and die..." construction.
Thanks.

Quote:
> System commands should return 0 if they are successful.

Indeed.


Sun, 13 Mar 2005 18:02:45 GMT  
 Help with simple simple perl script...


<snip>

In addition to this, for some reason Lynx would still not properly launch.

I ended up doing:

`lynx -auth=user:pass -dump 'https://etcetcetc'`

It looks like maybe Perl was interpreting some of the characters in the URL
as escape/control characters of some sort?

I'll have to look those up...

Brian K

Quote:
> I re-typed the entire script, pretty much, copied a few blocks of code in
> from various sources.  My mistake was replacing ` with '.  I assumed it
was
> a codepage mixup, I've seen those a lot in Windows with SQL scripts.  Yes,
I
> know this isn't Windows...

<snip>
Quote:

> Brian K



Sun, 13 Mar 2005 18:17:56 GMT  
 Help with simple simple perl script...
On Wed, 25 Sep 2002 19:02:45 +0200, "Alan J. Flavell"

Quote:

>On Sep 25, Helgi Briem inscribed on the eternal scroll:

>> system($command) == 0 or die "Error running $command:$?\n";

>That's certainly more didactic than the often-seen (and
>often-leads-to-confusion)  "system... and die..." construction.
>Thanks.

You're welcome.
Sometimes I use the form:

my $error = system($command);
if ($error) { die "Error running $command:$?\n"; }

I have seen many ways of expressing this.  Those
two appear to cause least confusion.

There is also:
!system($command) or die "Error running $command:$?\n";
which I found confusing when I first encountered it,
because it seemed to be saying:
do NOT run system($command).

--
Regards, Helgi Briem
helgi AT decode DOT is

                           A: Top posting
                           Q: What is the most irritating thing on Usenet?
                                           - "Gordon" on apihna



Mon, 14 Mar 2005 10:11:45 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. guidance for the benighted? (simple perl script help)

2. Perl idiot needs help with simple script

3. Help me with a simple cgi/perl script

4. Please help with simple perl script

5. HELP : simple perl script!

6. Help with simple perl script

7. Need help on very simple Perl script

8. Help needed with CGI script (a very simple problem)

9. help with simple script

10. Need a Simple but Unique CGI script - please help

11. Simple Script Help

12. Help with a simple form - sending script

 

 
Powered by phpBB® Forum Software