ActivePerl perlscript output inconsistencies

H

Henry Law

Running ActivePerl under Win XP; I'm trying to work out how to make
Perlscript do what Perl does (why doesn't it ... answers on a postcard).
Here's a sample script:

<Job ID="tryit">
<script language=PerlScript>

use strict;
use warnings;
use vars('$WScript'); # Avoid "strict" warnings

my $arg = $WScript->{Arguments};

my $countArgs = $arg->{Count};

open OUTPUT,">>tryit.log";

for(my $i=0; $i<$countArgs; $i++) {
$WScript->Echo($arg->Item($i));
#print OUTPUT "$arg->Item($i)\n";
}
</script>
</job>

Call it "tryit.wsf" and run it with "tryit.wsf one two three" and it
puts up messages boxes with "one", "two" and "three" in them, as desired.

Now comment out the $WScript->Echo line and remove the comment on the
line below it; I expect this to write "one", "two" and "three" to the
log file. Instead when I run "tryit.wsf one two three" I get the
following log file:

Win32::OLE=HASH(0x179cea0)->Item(0)
Win32::OLE=HASH(0x179cea0)->Item(1)
Win32::OLE=HASH(0x179cea0)->Item(2)

Can anyone help me find out why replacing this "Echo" thing with a print
statement apparently changes the data printed? I'm mystified.
 
X

xhoster

Henry Law said:
$WScript->Echo($arg->Item($i));
#print OUTPUT "$arg->Item($i)\n"; ....

Now comment out the $WScript->Echo line and remove the comment on the
line below it; I expect this to write "one", "two" and "three" to the
log file. Instead when I run "tryit.wsf one two three" I get the
following log file:

Win32::OLE=HASH(0x179cea0)->Item(0)
Win32::OLE=HASH(0x179cea0)->Item(1)
Win32::OLE=HASH(0x179cea0)->Item(2)

Can anyone help me find out why replacing this "Echo" thing with a print
statement apparently changes the data printed? I'm mystified.

It is not because you changed print to Echo, it is because in the
process you changed (...) to "..." in the process.

print OUTPUT $arg->Item($i), "\n";

Xho
 
H

Henry Law

It is not because you changed print to Echo, it is because in the
process you changed (...) to "..." in the process.

print OUTPUT $arg->Item($i), "\n";

Xho, thank you for seeing what my eyes had failed to see a hundred
times. You've led me to an even more surprising conclusion, however,
which is that Perlscript and Perl don't seem to interpolate strings the
same way. Another version of the test program shows the problem:

<Job ID="tryit">
<script language=PerlScript>

use strict;
use warnings;
use vars('$WScript'); # Avoid "strict" warnings

my $arg = $WScript->{Arguments};

open OUTPUT,">tryit.log";

for(my $i=0; $i<$arg->{Count}; $i++) {
print OUTPUT $arg->Item($i),"\n";
print OUTPUT "$arg->Item($i)\n";
}
</script>
</job>

F:>tryit.wsf one two three

F:>type tryit.log
one
Win32::OLE=HASH(0x17da744)->Item(0)
two
Win32::OLE=HASH(0x17da744)->Item(1)
three
Win32::OLE=HASH(0x17da744)->Item(2)

Should those two print statements not produce the same result in Perl?
 
A

Anno Siegel

Henry Law said:
[...]

times. You've led me to an even more surprising conclusion, however,
which is that Perlscript and Perl don't seem to interpolate strings the
same way. Another version of the test program shows the problem:

<Job ID="tryit">
<script language=PerlScript>

use strict;
use warnings;
use vars('$WScript'); # Avoid "strict" warnings

my $arg = $WScript->{Arguments};

open OUTPUT,">tryit.log";

for(my $i=0; $i<$arg->{Count}; $i++) {
print OUTPUT $arg->Item($i),"\n";
print OUTPUT "$arg->Item($i)\n";
}
</script>
</job>

F:>tryit.wsf one two three

F:>type tryit.log
one
Win32::OLE=HASH(0x17da744)->Item(0)
two
Win32::OLE=HASH(0x17da744)->Item(1)
three
Win32::OLE=HASH(0x17da744)->Item(2)

Should those two print statements not produce the same result in Perl?

No, neither on Perl, nor in perlscript. Perl doesn't interpolate
function calls (including method calls) in double-quote context.

perl -le 'print sin(0); print "sin(0)"'

shows the same discrepancy.

Anno
 
H

Henry Law

Anno said:
No, neither on Perl, nor in perlscript. Perl doesn't interpolate
function calls (including method calls) in double-quote context.

perl -le 'print sin(0); print "sin(0)"'

shows the same discrepancy.

Bother, I hadn't spotted that it's a method call and not a dereference.
All is clear now; thanks.
 
J

Joe Smith

Henry said:
Xho, thank you for seeing what my eyes had failed to see a hundred
times. You've led me to an even more surprising conclusion, however,
which is that Perlscript and Perl don't seem to interpolate strings the
same way. Another version of the test program shows the problem:

<Job ID="tryit">
<script language=PerlScript>

use strict;
use warnings;
use vars('$WScript'); # Avoid "strict" warnings

my $arg = $WScript->{Arguments};

open OUTPUT,">tryit.log";

for(my $i=0; $i<$arg->{Count}; $i++) {
print OUTPUT $arg->Item($i),"\n";
print OUTPUT "$arg->Item($i)\n";
}
</script>
</job>

F:>tryit.wsf one two three

F:>type tryit.log
one
Win32::OLE=HASH(0x17da744)->Item(0)
two
Win32::OLE=HASH(0x17da744)->Item(1)
three
Win32::OLE=HASH(0x17da744)->Item(2)

Should those two print statements not produce the same result in Perl?

Nope. Simple variables are interpolated in double-quoted strings,
functions are not.

linux% cat temp
print " Inside quotes: sin(2)\n";
print "Outside quotes: ",sin(2),"\n";
linux% perl temp
Inside quotes: sin(2)
Outside quotes: 0.909297426825682

You're use of "->" is very much like a function call.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top