Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

passing lexical filehandles

by girarde (Hermit)
on Jan 15, 2014 at 16:27 UTC ( [id://1070700]=perlquestion: print w/replies, xml ) Need Help??

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

I have a subroutine that I would like to run against two data sources and print to different lexical filehandles depending on the original entry point (the routine recurses through Active Directory trees).
open($eis_fh, ">", "EIS.txt") or die "no handle to write EIS users"; Roundup($EIS_O, $eis_fh); sub Roundup { my ($root, $fh) = (@_); foreach (in $root) { my $thing = $_; if ($thing->{class} eq 'organizationalUnit') { my $thong; foreach (in $thing) { $thong = $_; Roundup($thong); } } elsif ($thing->{class} eq 'user') { my $string; $string .= $thing->{employeeID}; $string .= "\t" ; $string .= $thing->{sAMAccountName} ; $string .= "\t" ; $string .= $thing->{department} ; $string .= "\t" ; $string .= $thing->{o} ; $string .= "\t" ; $string .= $thing->{physicalDeliveryOfficeName} ; $string .= "\n"; print { $fh } $string; } else { } } }
does not work. What am I overlooking?

Replies are listed 'Best First'.
Re: passing lexical filehandles
by tobyink (Canon) on Jan 15, 2014 at 16:35 UTC
    1. foreach (in $root): Perl doesn't have a built-in function called in, so I'm assuming you've defined one somewhere, and this makes sense.

    2. You call Roundup($thong), but the Roundup function seems to expect two arguments, not one.

    3. This bit:

      my $string; $string .= $thing->{employeeID}; $string .= "\t" ; $string .= $thing->{sAMAccountName} ; $string .= "\t" ; $string .= $thing->{department} ; $string .= "\t" ; $string .= $thing->{o} ; $string .= "\t" ; $string .= $thing->{physicalDeliveryOfficeName} ; $string .= "\n";

      Might be clearer expressed as:

      my $string = join("\t", $thing->{employeeID}, $thing->{sAMAccountName}, $thing->{department}, $thing->{o}, $thing->{physicalDeliveryOfficeName}, )."\n";

      Or even:

      my $string = join("\t", @$thing{ qw( employeeID sAMAccountName department o physicalDeliveryOfficeName ) })."\n";
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Thanks: I omitted mentioning use Win32::OLE qw(in); and I forgot that call to recurse needs both arguments as well.
Re: passing lexical filehandles
by sn1987a (Deacon) on Jan 15, 2014 at 20:25 UTC
    One more style note:
    foreach (in $root) { my $thing = $_;
    is more idiomatically written:
    foreach my $thing (in $root) {
    or more commonly:
    for my $thing (in $root) {
      Thank you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1070700]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found