Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have the following script that seems to work for me locally on my computer - from within a .htm file. But when I upload it and try to get it to work noting happens(is writen). Could yee guys help me out with some pointers.
$window->document->write("<BR> Hi.<BR> "); $window->document->write("Here is a try using a PerlScript.<BR> "); sub PrintWelcome { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(); $thisday=(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday +)[$wday]; $thismon=(January,February,March,April,May,June,July,August,Septem +ber,October,November,December)[$mon]; if ($hour < 12) { $window->document->write( "Good morning! "); } elsif ($hour < 17) { $window->document->write( "Good afternoon! "); } else { $window->document->write( "Good evening! "); }; $ampm = 'AM'; if ($hour > 12) { $hour-=12; $ampm = 'PM'; } $time = sprintf '%d:%2.2d:%2.2d',$hour,$min,$sec; $year += 1900; $datetime = $time.' '.$ampm.', '.$thisday.', '.$thismon.' '.$mday. +', '.$year; $window->document->write("Just in case you were wondering, it's<BR +> "); $window->document->write($datetime); $window->document->write("<BR> <BR> Your username is $ENV{'USERNAME'} +(it\'s in \$ENV{'USERNAME'}), the computername is $ENV{'COMPUTERNAME' +} (it\'s in \$ENV{'COMPUTERNAME'}), and your OS is $ENV{'OS'} (it\'s +in \$ENV{'OS'}).<BR> "); $window->document->write("<BR> <BR> Here is everything in %ENV:<BR> ") +; foreach $key (sort(keys %ENV)) { $window->document->write("<BR> $key" . " = " . $ENV{$key} . "<BR> "); } my @rec = `ipconfig /all`; # print "@rec\n"; # to print out all of the command's output my $itemmac; foreach $itemmac (@rec){ #chomp $itemmac}; # to split line 12 of the above, up into fileds/one-words #my ($field1, $field2, $field3, $field4, $field5, $field6, $field7, $f +ield8, $field9, $field10, $field11, $field12, $mac) = split(/\s+/, $r +ec[13]); #print "Your MAC(Media Access Control)/Physical Address in hexadecimal + is: $mac\nThe first three bytes (e.g., 00-0A-CC) are the manufacture +r's code and can be used to identify the manufacturer.\nThe last thre +e are the unique station ID or serial number for the interface.\n"; $window->document->write("<BR> <BR>Your MAC(Media Access Control)/Phys +ical Address in hexadecimal is: $itemmac<BR> "); }; } PrintWelcome();

Replies are listed 'Best First'.
Re: remote perlscript
by liverpole (Monsignor) on Jan 21, 2007 at 16:21 UTC
    Here's a number of suggestions:

    Start out by adding use strict; and use warnings; at the top of your code.  That will cause quite a few error messages to be displayed when you run it, so you should fix those next; add my to variables which haven't been declared (those will constitute the majority of the errors).

    I don't know what $window->document->write is, so I've changed it to print everywhere.  But if it works for you, that's fine (I've only added it so that I could debug the rest of the program).

    You can't create lists using barewords ...

    $thisday = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday +)[$wday];

    ... instead, either put quotes (" ") around the words (apostrophes (' ') will also do), or use the qw function (in which case you need to remove the commas ",").  If you put the declaration outside of the subroutine, it may look a little more clear (and you have the benefit of allowing other subroutines to use it too).  So if you do that for both @days and @months, you get:

    my @days = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ); my @months = qw( January February March April May June July August September October November December );

    It's often a good idea to chomp the results of `...`, so that the resulting lines don't have a newline at the end:

    my @rec = `ipconfig /all`;

    (though in your case, the output is HTML, so it doesn't matter so much in the grand scheme of things).

    There's no need to concatenate lots of little strings together, and, IMO, it makes it a lot harder to read.  Since quotation marks allow variable-interpolation from inside, you might consider changing:

    $datetime = $time.' '.$ampm.', '.$thisday.', '.$thismon.' '.$mday. +', '.$year;

    to:

    my $datetime = "$time $ampm, $thisday, $thismon $mday, $year";

    Applying all of these changes, and fixing indentation up a bit, results in something like:

    use strict; use warnings; my @days = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ); my @months = qw( January February March April May June July August September October November December ); sub PrintWelcome { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime +(); my $thisday = $days[$wday]; my $thismon = $months[$mon]; if ($hour < 12) { print "Good morning! "; } elsif ($hour < 17) { print "Good afternoon! "; } else { print "Good evening! "; }; my $ampm = 'AM'; if ($hour > 12) { $hour -= 12; $ampm = 'PM'; } my $time = sprintf '%d:%2.2d:%2.2d',$hour,$min,$sec; $year += 1900; my $datetime = "$time $ampm, $thisday, $thismon $mday, $year"; print "Just in case you were wondering, it's<BR> "; print $datetime; print "<BR> <BR> Your username is $ENV{'USERNAME'} "; print "(it\'s in \$ENV{'USERNAME'}), "; print "the computername is $ENV{'COMPUTERNAME'} "; print "(it\'s in \$ENV{'COMPUTERNAME'}), "; print "and your OS is $ENV{'OS'} (it\'s in \$ENV{'OS'}).<BR> "; print "<BR> <BR> Here is everything in %ENV:<BR> "; foreach my $key (sort(keys %ENV)) { print "<BR> $key" . " = " . $ENV{$key} . "<BR> "; } chomp(my @rec = `ipconfig /all`); foreach my $itemmac (@rec) { # Loop through ipconfig output, and find MAC address line if ($itemmac =~ /Physical Address.*: (\S+)/) { my $macaddr = $1; # Capture MAC address string print "<BR> <BR>Your MAC(Media Access Control)/Physical Ad +dress "; print "in hexadecimal is: $macaddr<BR> "; } } } PrintWelcome();

    which outputs HTML that seems at first glance to be fine.  I even saved it to a file, and it still looked okay when I browsed it as a webpage.

    What happens when you try the above program -- do you get the output you expect?  If so, do you still get it when you change every occurrence of print to $window->document->write?

    Update:  Looking at your code (and the output) again, I noticed that you weren't pulling out just the MAC address, but rather displaying every line from ipconfig.  Assuming that you want only the MAC address, I modified the code above to use a regular expression /Physical Address.*: (\S+)/, which does a capture on the MAC address string so you can print it when it's found.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: remote perlscript
by graff (Chancellor) on Jan 21, 2007 at 16:10 UTC
    I don't understand what you mean when you say this script "seems to work for me locally on my computer - from within a .htm file." (I've never heard of a perl script that works "from within a .htm file".) Also, you left out the part of your code that shows us what sort of object your "$window" is. Where is that coming from, and what is it?

    In any case, I think part of the problem may be that you never seem to print an http header or an initial  <html> tag.

    Have you considered using the CGI module? The documentation for that is pretty long, but it is pretty well organized, and you should be able to figure things out pretty quickly. It will help you to write code that is more coherent and easier to fix and maintain.

    For example, your script could start with:

    use strict; use CGI qw/standard/; print header, start_html( 'This is a test' ), h2( 'Hi.' ), h2( 'Here is a try using Perl/CGI.' ); # and so on...
      Thanks guys - that is a lot to think about for me. I though I could just add the # line at the top - with a path to some web based perl interperater or something and it would then work.

      For graff: I use frontpage - and from within it I can insert a script, and yes much of the above did work locally for me. I can't remember how I got it to work though, because I only just found one of my old files that was displaying the time, and added the env displaying - and it too worked so I thought I was getting somewhere.

      thanks again guys
Re: remote perlscript
by johngg (Canon) on Jan 21, 2007 at 17:26 UTC
    graff and liverpole have given you some useful pointers. I would like to draw your attention to what I think is a fault with your calculation of AM/PM. On a 24-hour clock the hours of 0 through 11 are AM and 12 through 23 are PM. Your code should perhaps read

    $ampm = 'AM'; if ($hour >= 12) { $hour -= 12; $ampm = 'PM'; }

    Also, when you are constructing your $datetime string you could construct the whole thing in one go with the sprintf. Note that %02d will do the same thing here as %2.2d.

    $datetime = sprintf '%d:%02d:%02d %s, %s, %s %d, %d', $hour, $min, $sec, $ampm, $thisday, $thismon, $mday, $year + 1900;

    This looks a little clearer to my eye, particularly the format string which makes it much easier to visualise what the $datetime string will look like.

    I hope this is of use.

    Cheers,

    JohnGG

    Update: It occurs to me that 00:23 on a 24-hour clock is 12:23AM, not 0:23AM and 12:23 on a 24-hour clock is 12:23PM, not 0:23PM so revised code should in fact be

    $ampm = 'AM'; if ($hour >= 12) { $hour -= 12; $ampm = 'PM'; } $hour += 12 unless $hour;
Re: remote perlscript
by EvanK (Chaplain) on Jan 22, 2007 at 00:49 UTC
    Keep in mind I'm just fielding a guess here, but when the OP says "a remote perlscript", I think he might actually mean PerlScript, which is an ActiveX scripting engine that implements perl (and is a part of ActiveState's perl distro).

    I've never used it myself, but if I understand the concept of it, one could embed it within an html document much like other ActiveX scripting engines like JScript or VBScript.

    Update: Noone else seems to know (or at least has not replied), so to the OP, I'll make a suggestion...do you have an activex scripting host? If you're running IIS, then you probably do, and it might be a configuration issue, in which case try the microsoft newsgroups. If you're trying to use it on apache, then it may well not work at all. Hope that at least points you in the right direction.

    __________
    The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
    - Terry Pratchett