[22085] in Perl-Users-Digest
Perl-Users Digest, Issue: 4307 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Dec 23 18:10:46 2002
Date: Mon, 23 Dec 2002 15:10:10 -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 Mon, 23 Dec 2002 Volume: 10 Number: 4307
Today's topics:
Re: snmp and perl <someone@somewhere.nl>
Re: Tied Filehandles <goldbb2@earthlink.net>
Re: using Perl inside directories and extensions questi (Tad McClellan)
Using XML Simple to Define variables (Tunez)
Re: Win32::OLE MSGraph <lance@augustmail.com>
Re: Win32::OLE MSGraph <goldbb2@earthlink.net>
Re: Win32::OLE MSGraph (Jay Tilton)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 23 Dec 2002 19:08:09 +0100
From: "Stefan" <someone@somewhere.nl>
Subject: Re: snmp and perl
Message-Id: <3e0751b3$0$2244$e4fe514c@dreader6.news.xs4all.nl>
"Bastian Zacher" <bastian.zacher@aspect-onlin.de> schreef in bericht
news:au6pa5$1853$2@news.f.de.plusline.net...
> Hello NG,
>
> got serious problems with snmpget called in this perlscript (NET::SNMP):
>
>
> #!/usr/bin/perl
>
> use Net::SNMP;
>
> $ENV{'MIBS'} = "ALL";
> $ENV{'MIBFILES'} =
> "CISCO-MEMORY-POOL-MIB.my:CISCO-FIREWALL-MIB.my:
> CISCO-PRODUCTS-MIB.my:CISCO-PROCESS-MIB-V1SMI.my";
>
> my $curr_conn = "1.3.6.1.4.1.9.9.147.1.2.2.2.1.5.40.6";
>
> my ($session, $error) = Net::SNMP->session(
> -hostname => shift || '192.168.1.1',
> -community => shift || 'public',
> );
>
> die "session() error: $error" unless $session;
>
> my $result = $session->get_request($curr_conn);
>
> die "Fehler: ". $session->error unless(defined $result);
>
> $session->close;
>
> print "Connections: ". $result->{$curr_conn} . "\n";
>
> These are the results:
>
> Use of uninitialized value in concatenation (.) or string at
> ./pixconn.pl line 25.
> Connections:
>
> Calling snmpget manually, everything's ok:
>
> snmpget -c public 192.168.1.1 .1.3.6.1.4.1.9.9.147.1.2.2.2.1.5.40.6
>
> Does anybody know, what's wrong ? Using other OID's, both the scipt
> and the cmd doing their job correctly.
>
>
> Thanx a lot,
>
> Bastian
Either $result and/or $curr_conn and/or $result->{$curr_con} are not
defined. Add a couple print's in your script to display the variables
values.
Another think you might want to try is to switch the print and the
close();
print "Connections: ". $result->{$curr_conn} . "\n";
$session->close;
Good luck,
Stefan
------------------------------
Date: Mon, 23 Dec 2002 16:29:28 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Tied Filehandles
Message-Id: <3E078038.1F841771@earthlink.net>
Andrew Hamm wrote:
>
> Andrew Hamm wrote:
> >
> > Bewwwwwdiful. Thanks. So Tie::Handle maps PRINT and PRINTF to WRITE.
> > Nice. I was hoping for that.
>
> LOL - because if you don't laugh you cry...
>
> It works fine in the general case, and even with:
>
> tie *STDERR, 'TimeStampFH', '>> junk';
>
> this will work:
>
> print STDERR "this is a test\n";
>
> however,
>
> warn "this is a test\n";
>
> does not. Output goes to original stderr, not to the target file, and
> it is not garnished either way. The closest to getting delivery into
> the file is:
>
> perl -MTimeStampFH -e '
> open EFD, ">> junk";
> tie *EFD, "TimeStampFH", ">> junk";
> *STDERR = *EFD;
> warn "this\nis\nit\n";
> '
>
> which delivers to junk but does not garnish. As such, it appears to be
> merely a replacement of STDERR. It appears that warn is not invoking
> the ties. Since warn is most likely C code, perhaps it's accessing
> fileno(STDERR) and writing directly to that. Unfortunately I don't
> have the knowledge of the source of Perl to go looking easily at that
> or to get an understanding.
I would suggest that you try upgrading to 5.8 -- I believe that this
should allow warn(), carp(), etc, to properly send their data to the
tied handle.
Or, if you don't want to upgrade, try this:
use IPC::Open2;
open( local(*JUNK), ">", "junk" ) or die horribly;
open2( ">&JUNK", *STDERR, $^X, "-e", q{
my $state = 1;
$| = 1;
while( sysread( STDIN, $_, 8192 ) ) {
my $stamp = localtime() . " ";
my $bol = $state ? qr/^/m : qr/(?<=\n)/;
$state = chomp;
s/$bol/$stamp/g;
$\ = $state ? "\n" : "";
print;
}
} );
close(JUNK);
[untested]
This doesn't use tie, and thus *should* work on 5.6.
--
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);
------------------------------
Date: Mon, 23 Dec 2002 10:24:14 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: using Perl inside directories and extensions questions
Message-Id: <slrnb0ee5e.d9m.tadmc@magna.augustmail.com>
Patrick Smith <patrick@ocg6.marine.usf.edu> wrote:
> 1) Is there a way to run the same Perl script on all files in a
> directory?
Yes.
# untested
my $dir = '/some/dir';
opendir DIR, $dir or die "could not open '$dir' directory $!";
while ( my $file = readdir DIR ) {
next unless -f "$dir/$file"; # skip all but plain files
do_something_to_file( "$dir/$file" );
}
closedir DIR;
> Is it best done inside Perl, or from a shell script that
> calls the Perl script?
It is nearly always better to do things in native Perl rather
than rely on external programs.
Native Perl will, in general, be faster and more portable.
> 2) Is there a way from inside a Perl script to change the extension on
> an input file to create a new file name with a different extension for
> output? (Input file read in on command line and output file specified
> inside the Perl script)
>
> Eg. Data.in from command line becomes Data.out inside Perl script.
Yes again.
# untested again
my $in = $ARGV[0];
(my $out = $in) =~ s/\.[^.]+$/.out/; # replace extension with ".out"
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 23 Dec 2002 13:53:29 -0800
From: vmontuoro@request.com.au (Tunez)
Subject: Using XML Simple to Define variables
Message-Id: <3c80bfeb.0212231353.5d162f4d@posting.google.com>
I'm having a few problems defining variables from an XML file when the
tags have no value in them, it returns the following error
"Use of uninitialized value in concatenation (.) or string at
xml-read.pl line 34."
This message makes perfect sense because the variable is not there,
what should I do????
Confused :(
###########xml-read.pl#############
use XML::Simple;
use Data::Dumper;
$file = 'XMLOUT.xml';
$xs1 = XML::Simple->new();
$doc = $xs1->XMLin($file,
keyattr => ['fnn'],
forcearray => ['CPE_PROCESS'],
);
print Dumper($doc);
$message1 = $doc->{CPE_PROCESS}->{16}->{CPERestarted}->{content};
$message2 = $doc->{CPE_PROCESS}->{17}->{CPERestarted}->{content};
print "$message1\n$message2\n";
########################################
############XMLOUT.xml##################
<?xml version='1.0' standalone='yes'?>
<data>
<CPE_PROCESS fnn="16">
<CPERestarted success="1">message</CPERestarted>
</CPE_PROCESS>
<CPE_PROCESS fnn="17">
<CPERestarted success="1"></CPERestarted>
</CPE_PROCESS>
</data>
########################################
------------------------------
Date: Mon, 23 Dec 2002 15:53:38 -0600
From: Lance Hoffmeyer <lance@augustmail.com>
Subject: Re: Win32::OLE MSGraph
Message-Id: <Xns92EDA1AF71FD3lanceaugustmailcom@216.166.71.236>
I used the suggestions made in the previous email.
When I simply had if ($shape->{type}=msoEmbeddedOLEObject)
I got an error concerning barewords so I changed to the
following:
Now, I get
Win32::OLE(0.1502) error 0x80020003: "Member not found"
in METHOD/PROPERTYGET "" at test.pl line 35
Can't call method "Range" on an undefined value at test.pl line 35.
I am assuming the undefined value is $shape?
The foreach statement should take you to Slide3 in test.ppt, correct?
I am positive there is a shape type 7 (embedded OLE Object).
Any help on why this is not finding it? Just a line graph. I have
run a macro called Object_Types_on_This_Slide() and I know it exists.
I have included the original VBA code that works in Excel at the very
bottom
Lance
my $ppt=Win32::OLE->new('PowerPoint.Application','Quit');
$ppt->{Visible} = 1;
my $p1 = $ppt->Presentations->Open({FileName=>"b:\\perl\\test.ppt"});
foreach my $shape (in $p1->Slides(3)->Shapes) {
if ($shape->{Type} == 7){ #line 35
$shape->Application->DataSheet->Range("AS1:AS4") = $array;
}
};
Lance Hoffmeyer <lance@augustmail.com> wrote:
: I am trying to copy some data from Excel into a PPT Datasheet. I am
: having problems at the end with the foreach statements at the end.
: Can anyone help?
If you have a working VB code snippet that can alter the chart's
source data, coercing it into Perl syntax is almost incidental.
It's causing me a lot of grief but then I am pretty inexperienced
in VBA!
On the other hand, if you can get it to work in VB, you could use
Win32::OLE to run that VB code directly.
Thought about it. But then, I wouldn't raise my blood pressure or
learn as much :)
On the other other hand, why involve any code at all? The whole point
of OLE is to automate this kind of thing for you. Just link the
chart's source data to the data on the Excel sheet.
Don't want any links between Excel and PPT. That is the way I am
used to doing things. I am grabbing numbers from MS Word and pasting
them in Excel and PPT. The Excel step could be left out.
Word -> PPT
Word -> Excel
Comments on a couple of troublesome bits from the original code:
: foreach (my $Shape->$p1->ActivePresentation->Slides(3)->{Shapes}){
Shapes is a collection. The in() function is useful for iterating
across the members of a collection.
use Win32::OLE 'in'; # in() is not imported by default
...
foreach my $shape ( in $p1->Slides(3)->Shapes ) {
# do stuff
}
: If ($Shape->{Type}="msoEmbeddedOLEObject")
Two things conceptually wrong there ("If" is just a mistake).
1. That's an assignment. You should want a comparison.
2. msoEmbeddedOLEObject is not a string, but the name of a numerical
constant. The Win32::OLE::Const module can create these named
constants for you.
use Win32::OLE::Const 'Microsoft Office';
...
if( $shape->{Type} == msoEmbeddedOLEObject ) {
# do stuff
}
# Dim oPPTApp As PowerPoint.Application
# Dim oPPTShape As PowerPoint.Shape
# Dim rngNewRange As Excel.Range
# Dim oGraph As Object
# Set oPPTApp = CreateObject("PowerPoint.Application")
# oPPTApp.Visible = msoTrue
#oPPTApp.Presentations.Open "C:\test\Presentation1.ppt"
# Set rngNewRange = ActiveSheet.Range("AS1:AS4")
# rngNewRange.Select
# rngNewRange.Copy
# With oPPTApp.ActivePresentation.Slides(3)
# For Each oPPTShape In .Shapes
# If oPPTShape.Type = msoEmbeddedOLEObject Then
# If oPPTShape.OLEFormat.ProgId = "MSGraph.Chart.8" Then
#Set oGraph = oPPTShape.OLEFormat.Object
#oGraph.Application.DataSheet.Range("AS1:AS4").Paste
#End If
#End If
# Next oPPTShape
# End With
#End Sub
------------------------------
Date: Mon, 23 Dec 2002 17:26:11 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Win32::OLE MSGraph
Message-Id: <3E078D83.7CAA32BD@earthlink.net>
Lance Hoffmeyer wrote:
>
> I used the suggestions made in the previous email.
>
> When I simply had if ($shape->{type}=msoEmbeddedOLEObject)
> I got an error concerning barewords so I changed to the
> following:
>
Err, there's nothing here. Unless you're refering to the code down at
the bottom of the message.
Anyway, the reason you got an error about barewords is because you
didn't tell perl that you needed that set of constants. You should have
done:
use Win32::OLE::Const 'Microsoft Office';
...
if( $shape->{Type} == msoEmbeddedOLEObject ) {
# do stuff
}
> Now, I get
>
> Win32::OLE(0.1502) error 0x80020003: "Member not found"
> in METHOD/PROPERTYGET "" at test.pl line 35
This is probably because you used 'type' instead of 'Type'. (I'm not
sure on this, though).
> Can't call method "Range" on an undefined value at test.pl line 35.
This is because ($shape->Application->DataSheet) returned an undef
value.
> I am assuming the undefined value is $shape?
No, it's the thing just before the ->Range(...) method call (yes, that
entire expression to the left of the ->).
> The foreach statement should take you to Slide3 in test.ppt, correct?
> I am positive there is a shape type 7 (embedded OLE Object).
> Any help on why this is not finding it? Just a line graph. I have
> run a macro called Object_Types_on_This_Slide() and I know it exists.
> I have included the original VBA code that works in Excel at the very
> bottom
[snip]
> # Dim oPPTApp As PowerPoint.Application
> # Dim oPPTShape As PowerPoint.Shape
> # Dim rngNewRange As Excel.Range
> # Dim oGraph As Object
> # Set oPPTApp = CreateObject("PowerPoint.Application")
use Win32::OLE 'in';
use Win32::OLE::Const 'Microsoft Office';
my $oPPTApp = Win32::OLE->new('PowerPoint.Application','Quit');
> # oPPTApp.Visible = msoTrue
$oPPTApp->{Visible} = msoTrue;
> #oPPTApp.Presentations.Open "C:\test\Presentation1.ppt"
$oPPTApp->Presentations->Open("C:\\test\\Presentation1.ppt");
> # Set rngNewRange = ActiveSheet.Range("AS1:AS4")
my $rngNewRange = $oPPTApp->ActiveWorkbook->
ActiveSheet->Range("AS1:AS4")
> # rngNewRange.Select
$rngNewRange->Select;
> # rngNewRange.Copy
$rngNewRange->Copy;
> # With oPPTApp.ActivePresentation.Slides(3)
> # For Each oPPTShape In .Shapes
my $slides3 = $OPPTApp->ActivePresentation->Slides(3);
foreach my $oPPTShape (in $slides3->{Shapes}) {
> # If oPPTShape.Type = msoEmbeddedOLEObject Then
next if $oPPTShape->{Type} != msoEmbeddedOLEObject();
> # If oPPTShape.OLEFormat.ProgId = "MSGraph.Chart.8" Then
next if $oPPTShape->OLEFormat->{ProgId} ne "MSGraph.Chart.8";
> #Set oGraph = oPPTShape.OLEFormat.Object
my $oGraph = $oPPTShape->OLEFormat->{Object};
> #oGraph.Application.DataSheet.Range("AS1:AS4").Paste
$oGraph->Application->DataSheet->Range("AS1:AS4")->Paste;
}
[untested]
--
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);
------------------------------
Date: Mon, 23 Dec 2002 22:53:31 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Win32::OLE MSGraph
Message-Id: <3e078a2e.139319194@news.erols.com>
Lance Hoffmeyer <lance@augustmail.com> wrote:
: When I simply had if ($shape->{type}=msoEmbeddedOLEObject)
Again, that's an assignment. You should want a comparison.
: Win32::OLE(0.1502) error 0x80020003: "Member not found"
: in METHOD/PROPERTYGET "" at test.pl line 35
: Can't call method "Range" on an undefined value at test.pl line 35.
:
: I am assuming the undefined value is $shape?
You could test $shape for defined-ness instead of guessing.
: The foreach statement should take you to Slide3 in test.ppt, correct?
: I am positive there is a shape type 7 (embedded OLE Object).
: Any help on why this is not finding it?
How do you know it's not? The code never tests the return from any of
the methods for defined-ness before invoking a method on that return.
: I have included the original VBA code that works in Excel at the very
: bottom
If you have working code already, why are you busting your arse
writing it all over again?
#!perl
use warnings;
use strict;
use Win32::OLE;
my $app = Win32::OLE->new('Excel.Application', 'Quit');
$app->Workbooks->Open({FileName=>'c:\temp\test.xls'});
$app->Run('test.xls!foo'); # foo => name of the Excel VBA sub
------------------------------
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.
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 V10 Issue 4307
***************************************